【发布时间】:2014-07-11 13:13:37
【问题描述】:
我正在尝试通过 Ant 任务启动我的 Junit 测试,如下所示:
<target name="TestDaoImpl">
<mkdir dir="${junit.output.dir}"/>
<junit fork="yes" printsummary="withOutAndErr">
<jvmarg line="${conf.dir}"/>
<formatter type="xml"/>
<test name="my.package.TestKSLDaoImpl" todir="${junit.output.dir}"/>
<classpath refid="My.classpath"/>
</junit>
</target>
在我的测试中,我使用的是 PowerMockito,这两种情况:
PowerMockito.whenNew(Convert.class).withAnyArguments().thenReturn(convert);
PowerMockito.mockStatic(MyService.class);
还有 Mockito:
Mockito.when(convert.getXmlKsl(folder)).thenReturn(xmlStr);
实际上,当我在 Eclipse 中运行测试时,我没有收到任何错误。 但是当我通过 Ant Task 启动它时,我得到了这个错误:
org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be 'a method call on a mock'.
For example:
when(mock.getArticles()).thenReturn(articles);
Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
2. inside when() you don't call method on mock but on some other object.
3. the parent of the mocked class is not public.
It is a limitation of the mock engine.
at org.powermock.api.mockito.PowerMockito.when(PowerMockito.java:495)
错误在这里:
PowerMockito.mockStatic(MyService.class);
===> Mockito.when(MyService.getInstance(myId)).thenReturn(myService);
我正在使用这个罐子:
JUnit 4
cglib-nodep-2.2.2.jar
javassist-3.18.1-GA.jar
mockito-all-1.9.5.jar
objenesis-2.1.jar
powermock-mockito-1.5.4-full.jar
ant 和 PowerMockito 有冲突吗? 为什么 Eclipse 测试运行良好,Ant 却不行?
【问题讨论】:
-
这类问题的典型原因(在命令行或 IDE 上构建工作,但不是另一个)与类路径的差异有关。通常命令行工具和 IDE 会以不同的方式解决依赖关系。
-
您使用的是什么版本的 JUnit?
-
只是一个旁注:mockito 在西班牙语中的意思是“小鼻涕”。就是这样
标签: java eclipse ant junit mockito