SpringBoot@MockBean和@WebMvcTest不起作用



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(){
}
}

我得到这个错误

应用程序无法启动


描述:

组件需要名为"entityManagerFactory"的bean,该bean可以找不到。

行动:

考虑在您的配置

2018-11-26 21:58:32.343错误7385-[主要]o.s.test.context.TestContextManager:在时捕获异常允许TestExecutionListener[org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@5c7bfdc1]准备测试实例[com.webservices.example.librarydatabaseservice.controller.ControllerTest@74f827ad]

java.lang.IollegalStateException:未能在加载ApplicationContextorg.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContext LoaderDelicate.java:125)~[spring-test-5.1.2.RELEASE.jar:5.1.2.REASE]org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)~[spring-test-5.1.2.RELEASE.jar:5.1.2.REASE]org.springframework.boot.test.mok.mokito.MokitoTestExecutionListener.postProcessFields(MockitoTestExecution_Listener.java:99)~[spring-boot-test-2.1.0.RELEASE.jar:2.1.0.RELEASE]org.springframework.boot.test.mok.mokito.MokitoTestExecutionListener.injectFields(MockitoTestExecution_Listener.java:79)~[spring-boot-test-2.1.0.RELEASE.jar:2.1.0.RELEASE]org.springframework.boot.test.mok.mokito.MokitoTestExecutionListener.prepareTestInstance(MockitoTestExecution_Listener.java:54)~[spring-boot-test-2.1.0.RELEASE.jar:2.1.0.RELEASE]org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246)~[spring-test-5.1.2.RELEASE.jar:5.1.2.REASE]org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestorg.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)~[spring-test-5.1.2.RELEASE.jar:5.1.2.REASE]org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)~[junit-4.12.jar:4.12]在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)~[spring-test-5.1.2.RELEASE.jar:5.1.2.REASE]org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)~[spring-test-5.1.2.RELEASE.jar:5.1.2.REASE]org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)~[spring-test-5.1.2.RELEASE.jar:5.1.2.REASE]org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)~[junit-4.12.jar:4.12]在org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)~[junit-4.12.jar:4.12]在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)~[junit-4.12.jar:4.12]在org.junit.runners.ParentRunner.access$000(ParentRunner:58)~[junit-4.12.jar:4.12]在org.junit.runners.ParentRunner$2.eevaluate(ParentRunner.java:268)~[junit-4.12.jar:4.12]在org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)~[spring-test-5.1.2.RELEASE.jar:5.1.2.REASE]org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)~[spring-test-5.1.2.RELEASE.jar:5.1.2.REASE]org.junit.runners.ParentRunner.run(ParentRunner:363)~[junit-4.12.jar:4.12]在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)~[spring-test-5.1.2.RELEASE.jar:5.1.2.REASE]org.junit.runner.JUnitCore.run(JUnitCores.java:137)~[junit-4.12.jar:4.12]在com.intellij.junit4.JUnit4DeaTestRunner.startRunnerWithArgs(JUnit4DieTestRunner.java:68)~[junit-rt.jar:na]在com.intellij.rt.execution.junit.IideaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)~[junit-rt.jar:na]在com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)~[junit-rt.jar:na]在com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)~[junit-rt.jar:na]原因:org.springframework.beans.factory.BeanCreationException:错误正在创建名为"bookRepository"的bean:无法创建内部bean类型为"(内豆)#3f363135"设置时使用[org.springframework.orm.jpa.SharedEntityManagerCreator]bean属性'entityManager';嵌套异常为org.springframework.beans.factory.BeanCreationException:错误正在创建名为"(内部bean)#3f363135"的bean:无法解析设置构造函数时引用bean"entityManagerFactory"论点嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:否名为"entityManagerFactory"的bean可在org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolve.java:327)~[spring-beans-2.1.2.REASE.jar:5.1.2.REASE]org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNenecessary(BeanDefinitionValueResolve.java:131)~[spring-beans-2.1.2.REASE.jar:5.1.2.REASE]org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1648)~[spring-beans-2.1.2.REASE.jar:5.1.2.REASE]org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1400)~[spring-beans-2.1.2.REASE.jar:5.1.2.REASE]org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:575)~[spring-beans-2.1.2.REASE.jar:5.1.2.REASE]org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)~[spring-beans-2.1.2.REASE.jar:5.1.2.REASE]org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)~[spring-beans-2.1.2.REASE.jar:5.1.2.REASE]org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)~[spring-beans-2.1.2.REASE.jar:5.1.2.REASE]org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)~[spring-beans-2.1.2.REASE.jar:5.1.2.REASE]org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)~[spring-beans-2.1.2.REASE.jar:5.1.2.REASE]org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:827)~[spring-beans-2.1.2.REASE.jar:5.1.2.REASE]org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863)~[spring-context-5.1.2.RELEASE.jar:5.1.2.REASE]org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)~[spring-context-5.1.2.RELEASE.jar:5.1.2.REASE]org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]org.springframework.boot.SpringApplication.run(SpringApplication.java:316)~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:127)~[spring-boot-test-2.1.0.RELEASE.jar:2.1.0.RELEASE]org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContext LoaderDelicate.java:99)~[spring-test-5.1.2.RELEASE.jar:5.1.2.REASE]org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContext LoaderDelicate.java:117)~[spring-test-5.1.2.RELEASE.jar:5.1.2.REASE]。。。25个通用框架忽略原因:org.springframework.beans.factory.BeanCreationException:错误正在创建名为"(内部bean)#3f363135"的bean:无法解析设置构造函数时引用bean"entityManagerFactory"论点嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:否名为"entityManagerFactory"的bean可在org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolve.java:378)~[spring-beans-2.1.2.REASE.jar:5.1.2.REASE]org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNenecessary(BeanDefinitionValueResolve.java:110)~[spring-beans-2.1.2.REASE.jar:5.1.2.REASE]org.springframework.beans.factory.support.ConstrutorResolver.resolveConstructorArguments(ConstructorResolver.java:660)~[spring-beans-2.1.2.REASE.jar:5.1.2.REASE]org.springframework.beans.factory.support.ConstrutorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:478)~[spring-beans-2.1.2.REASE.jar:5.1.2.REASE]org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory:java:1288)~[spring-beans-2.1.2.REASE.jar:5.1.2.REASE]org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory:java:1127)~[spring-beans-2.1.2.REASE.jar:5.1.2.REASE]org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538)~[spring-beans-2.1.2.REASE.jar:5.1.2.REASE]org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)~[spring-beans-2.1.2.REASE.jar:5.1.2.REASE]org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolve.java:312)~[spring-beans-2.1.2.REASE.jar:5.1.2.REASE]。。。43个公共框架忽略原因:org.springframework.beans.factory.NoSuchBeanDefinitionException:否名为"entityManagerFactory"的bean可在org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:772)~[spring-beans-2.1.2.REASE.jar:5.1.2.REASE]org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1212)~[spring-beans-2.1.2.REASE.jar:5.1.2.REASE]org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory:java:294)~[spring-beans-2.1.2.REASE.jar:5.1.2.REASE]org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)~[spring-beans-2.1.2.REASE.jar:5.1.2.REASE]org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolve.java:367)~[spring-beans-2.1.2.REASE.jar:5.1.2.REASE]。。。51个通用框架省略

它说entityFactoryManager有问题,但在一种模拟方式中它工作,而在另一种方式中它不工作。。。

完整代码Github回购

我使用的是SpringBoot版本:2.1.0.RELEASE

您的测试正在用entityManagerFactorybean加载JPA。在您的主应用程序类中,我认为您已经编写了@EnableJpaRepositories,如果您的存储库位于主应用程序的基本包下,则不需要它。删除此注释,因为您的测试类应该只用于测试web层。

尝试将配置类添加到项目中,如下所示:

@Configuration
@EnableJpaRepositories(basePackages = "com.webservices.example.librarydatabaseservice.repository")
public class RepositoryConfiguration {
}

这个问题与你的考试无关。Spring在启动和加载服务时遇到了一个问题。在Mockito版本的测试中,您不需要它加载所有类并模拟服务/存储库,这样就掩盖了问题。在测试的另一个版本中,SpringRunner需要加载applicationContext,但由于缺少JPA配置而失败。

相关内容

  • 没有找到相关文章

最新更新