(是的,我知道,有些人认为不得不求助于PowerMock承认失败)
您好,
版本:TestNG 6.8,Mockito 1.9.5和PowerMock 1.5。
尽管谷歌搜索了很长时间,但我找不到一个例子。
相关课程SyntaxProcessor
有process()
方法进行初步检查,如果此检查通过,则会调用validate()
。
在这里,我故意使初步检查失败。我想检查validate()
确实从未被调用过。
在@ user1951544的帮助下,我提出了:
@PrepareForTest(SyntaxProcessor.class)
public final class SyntaxProcessorTest
{
// ....
@ObjectFactory
public IObjectFactory getObjectFactory()
{
return new PowerMockObjectFactory();
}
// ....
@DataProvider
public Iterator<Object[]> notSchemas()
{
return SampleNodeProvider.getSamplesExcept(NodeType.OBJECT);
}
@Test(dataProvider = "notSchemas")
public void syntaxProcessorYellsOnNonSchemas(final JsonNode node)
throws ProcessingException
{
final ArgumentCaptor<ProcessingMessage> captor
= ArgumentCaptor.forClass(ProcessingMessage.class);
final ProcessingReport report = PowerMockito.mock(ProcessingReport.class);
final JsonSchemaTree tree = new CanonicalSchemaTree(node);
final ValidationData data = new ValidationData(tree);
final Map<String, SyntaxChecker> map = Maps.newHashMap();
final SyntaxProcessor processor = PowerMockito.spy(new SyntaxProcessor(map));
processor.process(report, data);
// No PowerMockito.verify()??
// "any()" is from org.mockito.Matchers
Mockito.verify(processor, never())
.validate(any(SyntaxReport.class), any(JsonSchemaTree.class));
final JsonNode msgNode = captor.getValue().asJson();
assertEquals(msgNode.get("message").textValue(),
"document is not a JSON Schema: not an object");
}
}
遗憾的是,这个测试失败了,堆栈跟踪很大:
java.lang.RuntimeException: java.lang.ExceptionInInitializerError
at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:143)
at org.testng.internal.Parameters.handleParameters(Parameters.java:426)
at org.testng.internal.Invoker.handleParameters(Invoker.java:1383)
at org.testng.internal.Invoker.createParameters(Invoker.java:1075)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1180)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1198)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1123)
at org.testng.TestNG.run(TestNG.java:1031)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:111)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.ExceptionInInitializerError
at org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter.<init>(ConditionalStackTraceFilter.java:17)
at org.mockito.exceptions.base.MockitoException.filterStackTrace(MockitoException.java:30)
at org.mockito.exceptions.base.MockitoException.<init>(MockitoException.java:19)
at org.mockito.exceptions.misusing.MockitoConfigurationException.<init>(MockitoConfigurationException.java:18)
at org.mockito.internal.configuration.ClassPathLoader.loadImplementations(ClassPathLoader.java:145)
at org.mockito.internal.configuration.ClassPathLoader.findPluginImplementation(ClassPathLoader.java:110)
at org.mockito.internal.configuration.ClassPathLoader.findPlatformMockMaker(ClassPathLoader.java:106)
at org.mockito.internal.configuration.ClassPathLoader.<clinit>(ClassPathLoader.java:59)
at org.mockito.internal.util.MockUtil.<clinit>(MockUtil.java:21)
at org.mockito.internal.configuration.injection.scanner.MockScanner.<init>(MockScanner.java:22)
at org.mockito.internal.configuration.InjectingAnnotationEngine.injectMocks(InjectingAnnotationEngine.java:96)
at org.powermock.api.mockito.internal.configuration.PowerMockitoInjectingAnnotationEngine.process(PowerMockitoInjectingAnnotationEngine.java:38)
at org.powermock.api.extension.listener.AnnotationEnabler.injectSpiesAndInjectToSetters(AnnotationEnabler.java:58)
at org.powermock.api.extension.listener.AnnotationEnabler.beforeTestMethod(AnnotationEnabler.java:53)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:2014)
at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:744)
at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:415)
at org.powermock.modules.testng.internal.PowerMockTestNGMethodHandler.injectMocksUsingAnnotationEnabler(PowerMockTestNGMethodHandler.java:72)
at org.powermock.modules.testng.internal.PowerMockTestNGMethodHandler.invoke(PowerMockTestNGMethodHandler.java:47)
at com.github.fge.jsonschema.processing.syntax.SyntaxProcessorTest_$$_javassist_0.notSchemas(SyntaxProcessorTest_$$_javassist_0.java)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:135)
... 26 more
Caused by: java.lang.NullPointerException
at org.mockito.internal.exceptions.stacktrace.StackTraceFilter.<clinit>(StackTraceFilter.java:21)
... 56 more
我也尝试过阅读this page,以使测试扩展PowerMockTestCase
,但随后又出现了另一个错误:
java.lang.RuntimeException: Can't invoke method public void com.github.fge.jsonschema.processing.syntax.SyntaxProcessorTest.syntaxProcessorYellsOnNonSchemas(com.fasterxml.jackson.databind.JsonNode) throws com.github.fge.jsonschema.processing.ProcessingException, probably due to classloader mismatch
那么,你怎么写这样的测试?
(编辑:还试图将版本的TestNG,mockito和powermock降级到@ user1951544建议的版本。它适用于他,不适合我......但这次我在两种情况下都得到了第二个更短的例外)< / p>
答案 0 :(得分:2)
这可以通过PowerMock完成,您必须为单元测试添加2个注释: @RunWith @PrepareForTest 然后用PowerMockito替换Mockito的间谍/模拟方法。
你可以在这里找到一些有用的例子: http://www.smartics.de/archives/1233
最简单的测试可能如下:
@RunWith(PowerMockRunner.class)
@PrepareForTest(MyClass.class)
public class MyClassTest {
@Test
public void test(){
MyClass myClass = PowerMockito.spy(new MyClass());
myClass.interfaceMethod(some arguments);
Mockito.verify(myClass,Mockito.never()).otherMethod();
}
}
如果是TestNG,必须设置一个合适的ObjectFactory(而不是@RunWith),如http://almirsadikovic.blogspot.fr/2011/07/testng-and-powermockito-powermock.html或Cant mock static functions with powermock-easymock-testng (non-maven project)中的示例
答案 1 :(得分:1)
我找到了第二个问题的解决方法。
似乎这里描述的testng / powermock版本存在某种问题: http://code.google.com/p/powermock/issues/detail?id=414
在我的测试用例中,我将powermock版本更改为1.4.11并将testng更改为6.4并且一切正常:)
狡猾的东西......