Test web application with Spring Boot
Test web application with Spring Boot
geeksforgeeks.org/testing-in-spring-boot
In this article, we will be discussing Testing in Spring Boot. There are so many different
testing approaches in Spring Boot used for deploying the application server. Testing in
Spring Boot is an important feature of software development which ensures that your
application behaves as per the requirement and meets all the testing criteria. Spring Boot
provides a robust testing framework that supports Unit Testing, Integration Testing, and
End-to-End Testing.
1. Unit Tests:
The Focus is on testing individual components or units of the code in isolation.
Use tools like JUnit and Mockito for the writing unit tests.
2. Integration Tests:
Test the entire application from the end to end simulating real user scenarios.
Involve testing application’s behavior through its external interfaces.
Use tools like Selenium for the web applications.
@SpringBootTest:
Indicates that the annotated class is the Spring Boot test.
The Loads the complete application context.
@SpringBootTest
public class MySpringBootTest {
// Test methods go here
}
@RunWith(SpringRunner.class):
The Specifies the class to run the tests.
The SpringRunner is the alias for SpringJUnit4ClassRunner.
1/6
@RunWith(SpringRunner.class)
@SpringBootTest
public class MySpringBootTest {
// Test methods go here
}
@MockBean:
The Mocks a Spring Bean for testing purposes.
Useful for the isolating the unit of the code being tested.
@SpringBootTest
public class MyServiceTest {
@Autowired
private MyService myService;
@MockBean
private ExternalService externalService;
// Test methods go here
}
@Test:
@Test
public void myUnitTest() {
// Test logic goes here
}
<dependencies>
<!-- Spring Boot Starter Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2/6
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
@RestController
public class MyController {
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}
@RunWith(SpringRunner.class)
@SpringBootTest
public class MyControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void testHelloEndpoint() throws Exception {
mockMvc.perform(get("/hello"))
.andExpect(status().isOk())
.andExpect(content().string("Hello, World!"));
}
}
Explanation:
3/6
For testing, you might want to have a separate the application-test.properties file. The
Spring Boot automatically picks up properties from this file during the tests.
@TestConfiguration
public class TestConfig {
@Bean
public MyService myService() {
return new MyService();
}
}
3. @MockBean (Mocking)
The @MockBean is used to mock a bean of the specific type, making it convenient to test
components that depend on bean.
@RunWith(SpringRunner.class)
@SpringBootTest
public class MyServiceTest {
@Autowired
private MyService myService;
@MockBean
private ExternalService externalService;
@Test
public void testServiceMethod() {
// Define behavior for the mocked external service
when(externalService.getData()).thenReturn("Mocked Data");
// Now test the method from MyService that uses externalService
String result = myService.processData();
assertEquals("Processed: Mocked Data", result);
}
}
4/6
@RunWith(SpringRunner.class)
@WebMvcTest(MyController.class)
public class MyControllerWebMvcTest {
@Autowired
private MockMvc mockMvc;
@Test
public void testHelloEndpoint() throws Exception {
mockMvc.perform(get("/hello"))
.andExpect(status().isOk())
.andExpect(content().string("Hello, World!"));
}
}
@RunWith(SpringRunner.class)
@DataJpaTest
public class MyRepositoryTest {
@Autowired
private MyRepository myRepository;
@Test
public void testRepositoryMethod() {
MyEntity entity = new MyEntity();
entity.setData("Test Data");
myRepository.save(entity);
MyEntity savedEntity = myRepository.findById(entity.getId()).orElse(null);
assertNotNull(savedEntity);
assertEquals("Test Data", savedEntity.getData());
}
}
Additional Annotations
@AutoConfigureMockMvc: Used with the @SpringBootTest to automatically
configure a MockMvc instance.
@DirtiesContext: Indicates that the ApplicationContext associated with test is dirty
and should be closed after the test.
@Transactional: Used to indicate that a test-managed transaction should be used.
5/6
6/6