Spring Boot Test cannot resolve repository dependency

Eray Tuncer :

I have one UserRepository which is a CRUD repository as shown:

@Repository
public interface UserRepository extends CrudRepository<User, Long> {

}

one UserController like this:

@RestController
@RequestMapping("/api")
public class UserController {

    @Autowired
    private UserRepository repository;
    @Autowired
    private UserResourceAssembler assembler;

and one WebMvcTest class to test my UserController:

@RunWith(SpringRunner.class)
@WebMvcTest(UserController.class)
public class UserControllerTest {

  @Autowired
  private MockMvc mvc;

  @Test
  public void getAllEmployeesAPI() throws Exception
  {
    mvc.perform( MockMvcRequestBuilders
        .get("/api/users")
        .accept(MediaType.APPLICATION_JSON))
        .andDo(print())
        .andExpect(status().isOk());
  }

}

When I run the server everything is fine. However I get this error when I run maven-test:

java.lang.IllegalStateException: Failed to load ApplicationContext Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'repository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Cannot create inner bean '(inner bean)#7ba1cdbe' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#7ba1cdbe': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Cannot create inner bean '(inner bean)#7ba1cdbe' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#7ba1cdbe': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#7ba1cdbe': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available

WonChul Heo :

If you have the same structure now, i recommend @SpringBootTest.

@WebMvcTest does not load database-related beans into the application context.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Manually resolve a dependency in Spring [Boot]

Cannot resolve external dependency org.springframework.boot:spring-boot-starter: because no repositories are defined

NestJS test cannot resolve PinoLogger dependency

Spring JPA repository cannot resolve property of entity

Cannot start tomcat server when having "spring-boot-starter-test" dependency in classpath

Spring Boot Integration test mock external dependency

@Value "Could not resolve placeholder" in Spring Boot Test

Spring boot Test multiple classes for API and repository

How autowire a repository in an integration test with Spring Boot?

Mockito Spring Boot - JUnit Test Case for Repository

Cannot resolve table "'xx"" in spring boot application

@TransactionAttribute cannot resolve symbol in spring boot

Spring boot cannot resolve view page

Spring boot + thymeleaf in IntelliJ: cannot resolve vars

Spring boot thymeleaf cannot resolve arabic messages

Cannot resolve dependency in maven

Cannot resolve symbol repository

How to write integration/system test with Spring Boot to test service with repository

Spring boot controller test - mock repository but test actual service

Spring - resolve dependency at runtime

Cannot find the Repository bean in Spring boot / Spring Data MongoDB

Spring boot REST: cannot test validation of @RequestParam

Cannot load context properties in a Spring Boot test

SpringApplicationConfiguration cannot be resolved in a Spring Boot test

No vanilla JUnit dependency in spring-boot-starter-test?

Mock autowired dependency in JUnit 5 test for Spring Boot 2 app

Dependency Injection to Aspect method in Spring Boot Test (JUnit)

Why is the entity manager not null in the spring boot test parent but it is null in the repository?

Cannot resolve Gradle Android dependency