当前位置: 首页 > 知识库问答 >
问题:

Spring中Mockito的间谍对象

耿和韵
2023-03-14

当我试图在单元测试中窥探一个对象时,我得到了一个异常。这是我的单元测试文件

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring/applicationContext.xml" })
public class BookingSuperManTest {
    BookInfoParams bookInfoParams; 
    HttpAttributeParams httpAttributeParams;
    AbstractRequester requester;
    public void beforeStartTest(){
        bookInfoParams = Mockito.spy(new BookInfoParams());
        httpAttributeParams = Mockito.spy(new HttpAttributeParams());
    }
    @Test
    public void step1GoToHomePage() throws BookingException{
        beforeStartTest();

        requester = new Step1HomePage(bookInfoParams, httpAttributeParams);
        requester.executeRequest();

        Assert.assertNotNull(httpAttributeParams.getResponseGetRequest());
    }
}

我在assign bookInfoParams Spy链接处遇到异常:

java.lang.NoClassDefFoundError: org/mockito/cglib/proxy/MethodInterceptor
    at org.powermock.api.mockito.internal.mockmaker.PowerMockMaker.<init>(PowerMockMaker.java:43)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at java.lang.Class.newInstance(Class.java:379)
    at org.mockito.internal.configuration.plugins.PluginLoader.loadImpl(PluginLoader.java:61)
    at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:24)
    at org.mockito.internal.configuration.plugins.PluginRegistry.<init>(PluginRegistry.java:13)
    at org.mockito.internal.configuration.plugins.Plugins.<clinit>(Plugins.java:12)
    at org.mockito.internal.util.MockUtil.<clinit>(MockUtil.java:23)
    at org.mockito.internal.MockitoCore.<init>(MockitoCore.java:40)
    at org.mockito.Mockito.<clinit>(Mockito.java:1103)
    at ive.core.test.webbot.book.vietjet.BookingSuperManTest.beforeStartTest(BookingSuperManTest.java:46)
    at ive.core.test.webbot.book.vietjet.BookingSuperManTest.step1GoToHomePage(BookingSuperManTest.java:54)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:73)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:224)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.ClassNotFoundException: org.mockito.cglib.proxy.MethodInterceptor
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 44 more
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>2.0.44-beta</version>
</dependency>
<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-api-mockito</artifactId>
    <version>1.6.4</version>
</dependency>
<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-junit4</artifactId>
    <version>1.6.4</version>
</dependency>
<dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib</artifactId>
    <version>3.2.1</version>
    <scope>test</scope>
</dependency>

共有1个答案

施景同
2023-03-14

可能是依赖关系的问题。powermock-api-mockitomockito-core版本1.10.19具有编译时依赖关系,但您也定义了对版本2.0.44-beta的依赖关系。这可能是类路径问题。尝试删除对版本2.0.44-beta的显式依赖。

 类似资料:
  • 我正在为一个具有2级依赖注入的类编写一个测试用例。我对1级依赖项注入对象使用@Spy注释,我想模拟第2级注入。但是,我在第二级上不断遇到空指针异常。有没有办法将模拟注入@Spy对象?

  • 问题内容: 我有内部类的课程,如下所示: 模仿测试如下所示:build.gradle: 测试: 第一次测试正在按预期方式工作。第二个永远不会被检测为“已调用”,尽管在日志中我看到的是。有什么问题吗?:) 谢谢! 问题答案: 怎么了? 好吧,这里的问题非常微妙,当您调用时,会在实例背后创建某种装饰器,以允许监视实例上的所有方法调用。因此,您可以检查给定方法被调用了多少次, 但是在装饰器 上 却 没有

  • 我正在为类编写一个单元测试,该类如下所示: 我想编写一个简单的单元测试,它将方法存根(这样它就不会实际触发并命中数据库),但它允许我验证调用是否最终执行。Mockito似乎是这份工作的合适工具。 这似乎是一个很好的游戏计划(至少对我来说)。但当我实际编写代码时,在测试方法的第2行(行)出现以下编译器错误: 类型Mockito中的(T)不适用于参数(void)时的方法 我看到Mockito无法模拟返

  • 引用同一类中的模拟方法 测试类 当调用真正的类temp到methodA时,应该返回模拟的方法B的值。从而返回true。为什么这不正确。我遇到了同样的问题。我想在真实的类上运行测试,而不是像答案中建议的那样在模拟对象上运行测试。我想运行类methodA,并在调用它时期待模拟对象spyTemp methodB的值

  • 我知道这个问题被问了很多次,但我听了很多答案,还是没有用 我的代码出了什么问题?我错过什么了吗?

  • 我对Mockito和带有“间谍”注释的字段有点混乱。 我想测试这个类: 因此,我创建了一个测试类,如下所示: 当Mockito实例categoryServiceImpl将categoryRepository作为模拟类注入时,但它不知道如何创建boundMapper。这个类依赖于mapper,所以我重写了代码: 但是现在的问题是在Mockito注入映射器之前执行了边界映射器的创建,所以我得到了一个N