In this tutorial, we will discuss how to Mock Objects and Stub Methods with Mockito, and we will see various ways to stubbing with common scenarios and examples. Mocking and stubbing are the basis of writing clean and simple unit tests. So, let’s begin! Visit our Mockito introductory level tutorial if you are a beginner…Read More How to Mock Objects and Stub Methods with Mockito?
In this tutorial, you will learn how to use TestRestTemplate to get a list of objects. If you are also interested to learn how to send an HTTP POST request, then please have a look at the “TestRestTemplate HTTP Post Example” tutorial. Maven Dependency To use TestRestTemplate in your Spring Boot project, you should have the…Read More Get List of Objects with TestRestTemplate
This tutorial will teach you how to use TestRestTemplate to send HTTP Post requests. If you are also interested to learn how to send an HTTP GET request, then please have a look at the “Get List of Objects with TestRestTemplate” tutorial. What is TestRestTemplate? TestRestTemplate is HTTP Client that you use for Spring Boot…Read More TestRestTemplate HTTP Post Example
In this tutorial, you will learn about @SpringBootTest annotation. You will use this annotation to write Integration Tests for your Spring Boot application. @SpringBootTest annotation is used to create a Spring Application Context that will be used during the test. It will make Spring Framework scan your application classes and look for different annotations. Depending…Read More @SpringBootTest Annotation Example
This article will explore the JUnit 5 Assertions API, go in-depth with examples of building fundamental assertions, and look at customizing the error message that appears when an assertion fails. We’ll also discuss how to execute many assertions simultaneously as an assertion group in our last section. So, let’s begin! What are JUnit Assertions? As…Read More An Overview of JUnit 5 Assertions with Examples
When we do integration testing of our Spring Boot application we sometimes need to make it load alternative configuration. For example, we might need to run and test our application on a different port number, make it connect to a database as a different user or even make it connect to a different database. In…Read More @TestPropertySource Annotation Example
In this tutorial, you will learn how to use @MockBean annotation in your Unit tests. @MockBean annotation is used to create and place mocks into Spring Application Contexts. If Spring Application Context does already have a bean of the same type then this bean will be replaced with a mock object. Otherwise, a new bean(mock)…Read More How to use @MockBean Annotation
In this blog post, you will learn the difference between @SpringBootTest and @WebMvcTest annotations. The main difference between @SpringBootTest and @WebMvcTest annotations is that @SpringBootTest annotation will start the full application context. Whereas @WebMvcTest annotation will make Spring Framework create application context with a limited number of beans(only those related to the Web Layer). You…Read More Difference Between @SpringBootTest and @WebMvcTest
In this blog post, you will learn a couple of ways you can disable Spring Security Configuration for your @WebMvcTest. Disable Security Filters for @AutoConfigureMockMvc One of the ways you can disable Spring Security filters in your tests, is to use the @AutoConfigureMockMvc annotation. @AutoConfigureMockMvc annotation can be applied to a test class to enable…Read More Disable Spring Security Configuration for @WebMvcTest
This tutorial will teach you how to enable Mockito framework in your Spring Boot project and in addition to that, you will also learn how to use @Mock and @InjectMocks annotations to create mock objects and inject those mock objects into the class under test. If you are very new to Mockito, please consider reading…Read More Getting Started with Mockito @Mock and @InjectMocks
In this tutorial, I am going to share with you how to make your JUnit tests run in order. And if you are interested to learn more about testing your RESTful Web Services with JUnit and Rest Assured check this list of tutorials: Building and Testing RESTful Web Services. By default, test classes and methods will…Read More How to Run JUnit Tests in Order
In this tutorial, you will learn how to write a JUnit Test that validates if the method under the test throws the correct exception. This tutorial will have examples for JUnit 5 and JUnit 4 as well. Let’s begin with JUnit 5 version first. JUnit 5. “assertThrows()” Assertion To test for exceptions in JUnit 5…Read More Test for Exception in JUnit 5 and JUnit 4
In this tutorial, you will learn about a way or a pattern to structure your code inside of a unit test method. If you follow this pattern consistently, then it will be easier to read and understand code inside of your test methods. This pattern is widely used by many developers and is know as Arrange, Act, Assert(AAA).…Read More How to Structure Unit Test Method?
In this lesson, you will learn about annotation that will help you to disable the test. Usually, it is not a good idea to disable a test. If the test is failing for some reason and you are not sure why it is failing, do not disable it. Try to figure out why it is…Read More How to Disable JUnit Test Method
The @DisplayName annotation is used to provide a custom name for your JUnit 5 test method. Let’s have a look how it is used in Java code. Below, is a very simple code example of a JUnit 5 test method. Notice how the @DisplayName annotation is used to provide a custom name for the text…Read More @DisplayName Annotation in JUnit 5