NoscuchMethodError我们试图在SpringBootIntegrationTest中使用@SpyBean或@MockBean

时间:2018-11-23 06:46:51

标签: spring-boot mockito integration-testing powermockito

我正在尝试在SpringBoot IntegrationTest中使用SpyBean。我的测试课如下

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
@EnableAutoConfiguration
public class MyTest {

    @SpyBean
    private MyService myService;

    @Test
    public void test() {
    }
}

我的build.gradle测试如下

testCompile group: 'org.mockito', name: 'mockito-all', version: '1.10.19'
testCompile group: 'org.powermock', name: 'powermock-api-mockito', version: '1.7.4'
testCompile group: 'org.powermock', name: 'powermock-module-junit4', version: '1.7.4'
testCompile "org.powermock:powermock-classloading-xstream:1.7.4"

当我尝试运行测试时,它发出以下异常

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MyService' Initialization of bean failed; nested exception is java.lang.NoSuchMethodError: org.mockito.MockSettings.verificationStartedListeners([Lorg/mockito/listeners/VerificationStartedListener;)Lorg/mockito/MockSettings;
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:584)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:273)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1239)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1166)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593)
... 80 more
 Caused by: java.lang.NoSuchMethodError: org.mockito.MockSettings.verificationStartedListeners([Lorg/mockito/listeners/VerificationStartedListener;)Lorg/mockito/MockSettings;
at org.springframework.boot.test.mock.mockito.SpyDefinition.createSpy(SpyDefinition.java:100)
at org.springframework.boot.test.mock.mockito.MockitoPostProcessor.createSpyIfNecessary(MockitoPostProcessor.java:355)
at org.springframework.boot.test.mock.mockito.MockitoPostProcessor$SpyPostProcessor.postProcessAfterInitialization(MockitoPostProcessor.java:489)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:434)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1749)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:576)
... 89 more

1 个答案:

答案 0 :(得分:0)

要使用@MockBean@SpyBean,Spring Boot 2.1.0需要Mockito2。您的依赖项配置已将Mockito降级为1.10。

我认为您有两种选择:

  1. 避免使用Powermock,以便您可以使用@MockBean@SpyBean所需的Mockito的最新版本
  2. 避免使用@MockBean@SpyBean来满足Powermock对旧版本Mockito的需求