Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot Test cannot resolve repository dependency

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

like image 607
Eray Tuncer Avatar asked Dec 27 '25 14:12

Eray Tuncer


2 Answers

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

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

like image 53
WonChul Heo Avatar answered Dec 31 '25 19:12

WonChul Heo


@WebMvcTest loads just the Web layer. If it contains some dependencies, you need to load your application context as well. You could narrow the dependencies used only in your controller with the @ContextConfiguration(classes = {YouTestConfiguration.class})

like image 36
androberz Avatar answered Dec 31 '25 17:12

androberz