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
If you have the same structure now, i recommend @SpringBootTest.
@WebMvcTest does not load database-related beans into the application context.
@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})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With