Testing Spring Data JPA with @DataJpaTest

This article shows how to use @DataJpaTest to test the Spring Data JPA application. Technologies used: Spring Boot 3.1.2 Spring Data JPA (Hibernate 6 is the default JPA implementation) H2 in-memory database Maven Java 17 JUnit 5 Table of contents: 1. Project Directory 2. Spring Data JPA – Entity and Repository 3. @DataJpaTest 3.1 Disable …

Read more

Spring Boot Testcontainers Example

This article shows how to test the Spring Boot REST endpoints using TestRestTemplate and Testcontainers (PostgreSQL container). Technologies used : Spring Boot 3.1.2 (Spring Web MVC, Spring Data JPA and Spring Test) Testcontainers 1.19.0 PostgreSQL 15, Alpine Linux base image postgres:15-alpine Java 17 JUnt 5 Tables of contents: 1. Project Structure 2. Project Dependencies 3. …

Read more

Spring @TestPropertySource example

In Spring integration tests, we can use @TestPropertySource to override the application properties which default loaded from application.properties or application.yml. Table of contents 1. Service Class 2. Test with the @TestPropertySource 3. @TestPropertySource Inline Properties 4. Download Source Code 5. References P.S Tested with Spring Boot 3.1.2 1. Service Class 1.1 We have a service …

Read more

Spring Boot – Unable to find a @SpringBootConfiguration

Terminal Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=…) with your test The Spring Boot tests @SpringBootTest or @DataJpaTest need to find the @SpringBootConfiguration application class to launch the entire application and do the tests. And if Spring Boot can’t find the SpringBootConfiguration, it throws errors and stops the tests. A …

Read more

Spring Boot JobRunr examples

This article shows how to use Spring Boot to create REST endpoints to run the background jobs managed by the JobRunr. Tested with: Spring Boot 2.3.12.RELEASE JobRunr 3.1.2 Maven 3 1. Directory Structure. A standard Maven project structure. 2. Project Dependencies. We need a single jobrunr-spring-boot-starter to integrate Spring Boot web and JobRunr. pom.xml <!– …

Read more

Spring REST Integration Test Example

In this article, we will show you how to test the Spring Boot REST application. Normally, we use the MockMvc or TestRestTemplate for the integration test. Technologies used : Spring Boot 2.1.2.RELEASE Spring 5.1.4.RELEASE Maven 3 Java 8 pom.xml <!– spring integration test –> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!– spring integration test for security–> …

Read more

Spring Test – How to test a JSON Array in jsonPath

In Spring, we can use Hamcrest APIs like hasItem and hasSize to test a JSON Array. Review the following example : package com.mkyong; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import static org.hamcrest.Matchers.*; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.times; import …

Read more

Spring + Mockito – Unable to mock save method?

Try to mock a repository save() method, but it is always returning null? P.S Tested with Spring Boot 2 + Spring Data JPA @Test public void save_book_OK() throws Exception { Book newBook = new Book(1L, "Mockito Guide", "mkyong"); when(mockRepository.save(newBook)).thenReturn(newBook); mockMvc.perform(post("/books") .content("{json}") .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)) .andExpect(status().isCreated()); } Solution 1. Mockito uses the equals for argument matching, try …

Read more

Spring Boot – How to init a Bean for testing?

In Spring Boot, we can create a @TestConfiguration class to initialize some beans for testing class only. P.S Tested with Spring Boot 2 1. @TestConfiguration + @Import This @TestConfiguration class will not pick up by the component scanning, we need to import it manually. TestConfig.java package com.mkyong; import org.springframework.boot.test.context.TestConfiguration; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.context.annotation.Bean; import java.time.Duration; …

Read more

Mockito – How to mock repository findById thenReturn() Optional?

Try to mock a repository findById method, but no idea use thenReturn() to return an object, as it accepts an Optional? P.S Tested in Spring Boot 2 environment import static org.mockito.Mockito.*; import org.springframework.boot.test.mock.mockito.MockBean; @MockBean private BookRepository mockRepository; @Before public void init() { Book book = new Book(1L, "A Book"); //error, can’t resolve method thenReturn(book)? when(mockRepository.findById(1L)).thenReturn(book); …

Read more

JSONAssert – How to unit test JSON data

In Java, we can use JSONAssert to unit test JSON data easily. 1. Maven pom.xml <dependency> <groupId>org.skyscreamer</groupId> <artifactId>jsonassert</artifactId> <version>1.5.0</version> </dependency> 2. JSON Object To test the JSON Object, Strict or not, fields order does not matter. If the extended fields are matter, enable the strict mode. 2.1 When the strictMode is off. JSONObject actual = …

Read more

Spring Boot Test unable to autowired MockMvc

Spring Boot integeration test, but unable to @Autowired MockMvc P.S Tested with Spring Boot 2.1.2.RELEASE BookControllerTest.java package com.mkyong; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import static org.hamcrest.Matchers.is; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class BookControllerTest { @Autowired …

Read more