ARTICLE AD BOX
My learning Spring Boot project consists of the files below. My goal is to write Unit tests that test the UserRepository queries. I've tried different annotations, but never got the test to run. In particular, I never managed to have the UserRepository auto-wired into the test, while it works in the main project. I'd prefer to run the repository tests against the actual database, but testing the queries against a different (maybe in-memory) database would be ok, too. Note that I have stripped out many queries and tests for brevity. Feel free to request more details of the project.
How do I set up the UserRepositoryTest so that it can run its test(s)? Please comment on any points that I have missed.
Building the current setup with maven yields the following error messages (edited to contain only the interesting messages, I hope):
2026-04-08T12:06:42.502+02:00 INFO 82801 --- [builder] [ main] t.c.s.AnnotationConfigContextLoaderUtils : Could not detect default configuration classes for test class [org.example.webapp.UserRepositoryTests]: UserRepositoryTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration. 2026-04-08T12:06:42.561+02:00 INFO 82801 --- [builder] [ main] .b.t.c.SpringBootTestContextBootstrapper : Found @SpringBootConfiguration org.example.webapp.WebApplication for test class org.example.webapp.UserRepositoryTests [ERROR] org.example.webapp.UserRepositoryTests.userList_hasCorrectLength -- Time elapsed: 0 s <<< ERROR! org.junit.jupiter.api.extension.ParameterResolutionException: No ParameterResolver registered for parameter [org.example.webapp.repository.UserRepository userRepository] in constructor [public org.example.webapp.UserRepositoryTests(org.example.webapp.repository.UserRepository)]. [ERROR] org.example.webapp.UserRepositoryTests.userRepositoryNotNull -- Time elapsed: 0.001 s <<< ERROR! org.junit.jupiter.api.extension.ParameterResolutionException: No ParameterResolver registered for parameter [org.example.webapp.repository.UserRepository userRepository] in constructor [public org.example.webapp.UserRepositoryTests(org.example.webapp.repository.UserRepository)]. [INFO] [INFO] Results: [INFO] [ERROR] Errors: [ERROR] UserRepositoryTests.userList_hasCorrectLength ? ParameterResolution No ParameterResolver registered for parameter [org.example.webapp.repository.UserRepository userRepository] in constructor [public org.example.webapp.UserRepositoryTests(org.example.webapp.repository.UserRepository)]. [ERROR] UserRepositoryTests.userRepositoryNotNull ? ParameterResolution No ParameterResolver registered for parameter [org.example.webapp.repository.UserRepository userRepository] in constructor [public org.example.webapp.UserRepositoryTests(org.example.webapp.repository.UserRepository)].File src/main/java/org/example/webapp/WebApplication.java:
package org.example.webapp; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.persistence.autoconfigure.EntityScan; import org.springframework.context.annotation.ComponentScan; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; @SpringBootApplication @ComponentScan({ "org.example.controller", "org.example.configuration", "org.example.test" }) @EnableJpaRepositories(basePackages = "org.example.repository") @EntityScan("org.example.entity") public class WebApplication { public static void main(String[] args) { SpringApplication.run(WebApplication.class, args); } }File src/main/java/org/example/webapp/repository/UserRepository.java:
package org.example.webapp.repository; import java.util.List; import java.util.Optional; import org.example.webapp.entity.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Component; import org.springframework.stereotype.Repository; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Repository @Service @Component @Transactional public interface UserRepository extends JpaRepository<User, Integer> { @Query(value = "SELECT u FROM User u WHERE u.email = :email") Optional<User> getUserByEmail(@Param("email") String email); // More queries deleted }File src/test/java/org/example/webapp/UserRepositoryTests.java:
package org.example.webapp; import static org.assertj.core.api.Assertions.assertThat; import java.util.List; import java.util.Optional; import org.junit.jupiter.api.Test; import org.example.webapp.entity.User; import org.example.webapp.repository.UserRepository; import org.springframework.boot.data.jpa.test.autoconfigure.DataJpaTest; import org.springframework.test.context.TestPropertySource; @DataJpaTest public class UserRepositoryTests { public UserRepositoryTests(UserRepository userRepository) { super(); this.userRepository = userRepository; } @Test public void userRepositoryNotNull() { assertThat(userRepository).isNotNull(); } @Test public void userList_hasCorrectLength() { List<User> userList = userRepository.getUsers(); assertThat(userList).isNotEmpty(); } // more tests deleted // @Autowired private UserRepository userRepository; }