(是的,我知道,有些人考虑不得不诉诸 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
我还尝试阅读此页面以使测试扩展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 建议的版本。它适用于他,而不是我......但这次我在两种情况下都得到了第二个更短的例外)