SpringBoot @MockBean 和@WebMvcTest 不起作用
SpringBoot @MockBean and @WebMvcTest does not work
Controller.java
@RestController
public class Controller {
@Autowired
private UserService userService;
@Autowired
private BookService bookService;
//Below userService implementation
@PostMapping(value = "/addNewUser", consumes = "application/json")
public void addNewUser(@RequestBody User newUser) {
userService.addNewUser(newUser);
}
@GetMapping(value = "/findUserById/{id}", produces = "application/json")
public User findUserById(@PathVariable("id") Long id) {
return userService.findUserById(id);
}
@GetMapping(value = "/findUserByName/{name}", produces = "application/json")
public User findUserByName(@PathVariable("name") String name) {
return userService.findUserByName(name);
}
UserServiceImpl.java
@Transactional
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
@Override
public void addNewUser(User newUser) {
userRepository.save(newUser);
}
Repository.java
public interface UserRepository extends CrudRepository<User, Long> {
Optional<User> findUserByName(String name);
Optional<User> findUserBySurname(String surname);
}
ControllerTest.java
//@RunWith(SpringRunner.class)
@RunWith(MockitoJUnitRunner.class)
@WebMvcTest(Controller.class)
public class ControllerTest {
// @MockBean
// private UserService userService;
//
// @MockBean
// private BookService bookService;
@Mock
private UserService userService;
@Mock
private BookService bookService;
@Autowired
private MockMvc mockMvc;
@Test
public void test(){
}
}
上面的配置可以,但是下面的不行。
@RunWith(SpringRunner.class)
//@RunWith(MockitoJUnitRunner.class)
@WebMvcTest(Controller.class)
public class ControllerTest {
@MockBean
private UserService userService;
@MockBean
private BookService bookService;
// @Mock
// private UserService userService;
//
// @Mock
// private BookService bookService;
@Autowired
private MockMvc mockMvc;
@Test
public void test(){
}
}
我收到这个错误
APPLICATION FAILED TO START
Description:
A component required a bean named 'entityManagerFactory' that could
not be found.
Action:
Consider defining a bean named 'entityManagerFactory' in your
configuration.
2018-11-26 21:58:32.343 ERROR 7385 --- [ main]
o.s.test.context.TestContextManager : Caught exception while
allowing TestExecutionListener
[org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@5c7bfdc1]
to prepare test instance
[com.webservices.example.librarydatabaseservice.controller.ControllerTest@74f827ad]
java.lang.IllegalStateException: Failed to load ApplicationContext at
org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)
~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener.postProcessFields(MockitoTestExecutionListener.java:99)
~[spring-boot-test-2.1.0.RELEASE.jar:2.1.0.RELEASE] at
org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener.injectFields(MockitoTestExecutionListener.java:79)
~[spring-boot-test-2.1.0.RELEASE.jar:2.1.0.RELEASE] at
org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener.prepareTestInstance(MockitoTestExecutionListener.java:54)
~[spring-boot-test-2.1.0.RELEASE.jar:2.1.0.RELEASE] at
org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246)
~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227) ~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
~[junit-4.12.jar:4.12] at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)
~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.junit.runners.ParentRunner.run(ParentRunner.java:290)
~[junit-4.12.jar:4.12] at
org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
~[junit-4.12.jar:4.12] at
org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
~[junit-4.12.jar:4.12] at
org.junit.runners.ParentRunner.access[=15=]0(ParentRunner.java:58)
~[junit-4.12.jar:4.12] at
org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
~[junit-4.12.jar:4.12] at
org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.junit.runners.ParentRunner.run(ParentRunner.java:363)
~[junit-4.12.jar:4.12] at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.junit.runner.JUnitCore.run(JUnitCore.java:137)
~[junit-4.12.jar:4.12] at
com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
~[junit-rt.jar:na] at
com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
~[junit-rt.jar:na] at
com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
~[junit-rt.jar:na] at
com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
~[junit-rt.jar:na] Caused by:
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'bookRepository': Cannot create inner bean
'(inner bean)#3f362135' 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)#3f362135': Cannot resolve
reference to bean 'entityManagerFactory' while setting constructor
argument; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
bean named 'entityManagerFactory' available at
org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:327)
~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:131)
~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1648)
~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1400)
~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:575)
~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=15=](AbstractBeanFactory.java:320)
~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:827)
~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863)
~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)
~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE] at
org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE] at
org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE] at
org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:127)
~[spring-boot-test-2.1.0.RELEASE.jar:2.1.0.RELEASE] at
org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117)
~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE] ... 25 common frames
omitted Caused by:
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name '(inner bean)#3f362135': Cannot resolve
reference to bean 'entityManagerFactory' while setting constructor
argument; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
bean named 'entityManagerFactory' available at
org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:378)
~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:110)
~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:660)
~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:478)
~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1288)
~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1127)
~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538)
~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:312)
~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] ... 43 common frames
omitted Caused by:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
bean named 'entityManagerFactory' available at
org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:772)
~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1212)
~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:294)
~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at
org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:367)
~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] ... 51 common frames
omitted
它说 entityFactoryManager 有问题,但在一种模拟方式中它可以工作,而在另一种方式中则不行...
完整代码Github repo
我使用的SpringBoot版本是:2.1.0.RELEASE
尝试像这样向项目添加配置 class:
@Configuration
@EnableJpaRepositories(basePackages = "com.webservices.example.librarydatabaseservice.repository")
public class RepositoryConfiguration {
}
问题与您的测试无关。 Spring 启动和加载服务时出现问题。在测试的 Mockito 版本中,您不需要它加载所有 classes 并模拟 service/repository,这样就可以掩盖问题。在另一个版本的测试中,SpringRunner 需要加载您的 applicationContext,但由于缺少 JPA 配置而失败。
您的测试正在使用 entityManagerFactory
bean 加载 JPA。在您的主应用程序 class 中,我认为您已经编写了 @EnableJpaRepositories
,如果您的存储库位于主应用程序的基础包下,则不需要这样做。删除此注释,因为您的测试 class 应该只用于测试 web 层。
Controller.java
@RestController
public class Controller {
@Autowired
private UserService userService;
@Autowired
private BookService bookService;
//Below userService implementation
@PostMapping(value = "/addNewUser", consumes = "application/json")
public void addNewUser(@RequestBody User newUser) {
userService.addNewUser(newUser);
}
@GetMapping(value = "/findUserById/{id}", produces = "application/json")
public User findUserById(@PathVariable("id") Long id) {
return userService.findUserById(id);
}
@GetMapping(value = "/findUserByName/{name}", produces = "application/json")
public User findUserByName(@PathVariable("name") String name) {
return userService.findUserByName(name);
}
UserServiceImpl.java
@Transactional
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
@Override
public void addNewUser(User newUser) {
userRepository.save(newUser);
}
Repository.java
public interface UserRepository extends CrudRepository<User, Long> {
Optional<User> findUserByName(String name);
Optional<User> findUserBySurname(String surname);
}
ControllerTest.java
//@RunWith(SpringRunner.class)
@RunWith(MockitoJUnitRunner.class)
@WebMvcTest(Controller.class)
public class ControllerTest {
// @MockBean
// private UserService userService;
//
// @MockBean
// private BookService bookService;
@Mock
private UserService userService;
@Mock
private BookService bookService;
@Autowired
private MockMvc mockMvc;
@Test
public void test(){
}
}
上面的配置可以,但是下面的不行。
@RunWith(SpringRunner.class)
//@RunWith(MockitoJUnitRunner.class)
@WebMvcTest(Controller.class)
public class ControllerTest {
@MockBean
private UserService userService;
@MockBean
private BookService bookService;
// @Mock
// private UserService userService;
//
// @Mock
// private BookService bookService;
@Autowired
private MockMvc mockMvc;
@Test
public void test(){
}
}
我收到这个错误
APPLICATION FAILED TO START
Description:
A component required a bean named 'entityManagerFactory' that could not be found.
Action:
Consider defining a bean named 'entityManagerFactory' in your configuration.
2018-11-26 21:58:32.343 ERROR 7385 --- [ main] o.s.test.context.TestContextManager : Caught exception while allowing TestExecutionListener [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@5c7bfdc1] to prepare test instance [com.webservices.example.librarydatabaseservice.controller.ControllerTest@74f827ad]
java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125) ~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108) ~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener.postProcessFields(MockitoTestExecutionListener.java:99) ~[spring-boot-test-2.1.0.RELEASE.jar:2.1.0.RELEASE] at org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener.injectFields(MockitoTestExecutionListener.java:79) ~[spring-boot-test-2.1.0.RELEASE.jar:2.1.0.RELEASE] at org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener.prepareTestInstance(MockitoTestExecutionListener.java:54) ~[spring-boot-test-2.1.0.RELEASE.jar:2.1.0.RELEASE] at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246) ~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227) ~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runReflectiveCall(SpringJUnit4ClassRunner.java:289) ~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) ~[junit-4.12.jar:4.12] at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291) ~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246) ~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97) ~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.junit.runners.ParentRunner.run(ParentRunner.java:290) ~[junit-4.12.jar:4.12] at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71) ~[junit-4.12.jar:4.12] at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) ~[junit-4.12.jar:4.12] at org.junit.runners.ParentRunner.access[=15=]0(ParentRunner.java:58) ~[junit-4.12.jar:4.12] at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268) ~[junit-4.12.jar:4.12] at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) ~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) ~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.junit.runners.ParentRunner.run(ParentRunner.java:363) ~[junit-4.12.jar:4.12] at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190) ~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.junit.runner.JUnitCore.run(JUnitCore.java:137) ~[junit-4.12.jar:4.12] at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) ~[junit-rt.jar:na] at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) ~[junit-rt.jar:na] at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) ~[junit-rt.jar:na] at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) ~[junit-rt.jar:na] Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bookRepository': Cannot create inner bean '(inner bean)#3f362135' 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)#3f362135': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:327) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:131) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1648) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1400) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:575) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=15=](AbstractBeanFactory.java:320) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:827) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE] at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:127) ~[spring-boot-test-2.1.0.RELEASE.jar:2.1.0.RELEASE] at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99) ~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117) ~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE] ... 25 common frames omitted Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#3f362135': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:378) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:110) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:660) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:478) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1288) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1127) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:312) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] ... 43 common frames omitted Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:772) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1212) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:294) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:367) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] ... 51 common frames omitted
它说 entityFactoryManager 有问题,但在一种模拟方式中它可以工作,而在另一种方式中则不行...
完整代码Github repo
我使用的SpringBoot版本是:2.1.0.RELEASE
尝试像这样向项目添加配置 class:
@Configuration
@EnableJpaRepositories(basePackages = "com.webservices.example.librarydatabaseservice.repository")
public class RepositoryConfiguration {
}
问题与您的测试无关。 Spring 启动和加载服务时出现问题。在测试的 Mockito 版本中,您不需要它加载所有 classes 并模拟 service/repository,这样就可以掩盖问题。在另一个版本的测试中,SpringRunner 需要加载您的 applicationContext,但由于缺少 JPA 配置而失败。
您的测试正在使用 entityManagerFactory
bean 加载 JPA。在您的主应用程序 class 中,我认为您已经编写了 @EnableJpaRepositories
,如果您的存储库位于主应用程序的基础包下,则不需要这样做。删除此注释,因为您的测试 class 应该只用于测试 web 层。