>我有一个问题。我不明白为什么嘲笑会例外。记录编写器将记录写入文件。我想验证它是否写入。我实现了另一个函数,并尝试模拟(输出流.class),但结果是一样的。我目前的测试是:
try (RecordWriter writer = new RecordWriter(serverAlias))
{
RecordWriter wr = mock(RecordWriter.class);
writer.writeRecordToFile(httpRecord.printRecord().getBytes(CHARSET_UTF8));
verify(wr,times(1)).writeRecordToFile(httpRecord.printRecord().getBytes(CHARSET_UTF8));
}
例外情况是:
Wanted but not invoked:
recordWriter.writeRecordToFile(
[50, 48, 49, 54, 48, 51, 49, 56, 45, 49, 50, 49, 52, 49, 51, 44, 50, 48, 49, 54, 48, 51, 49, 56, 49, 49, 50, 50, 44, 50, 48, 49, 54, 48, 51, 49, 56, 49, 49, 50, 57, 44, 51, 51, 51, 54, 53, 52, 56, 55, 57, 56, 44, 112, 114, 111, 118, 97, 44, 104, 101, 108, 108, 111, 44, 105, 32, 100, 111, 110, 39, 116, 32, 107, 110, 111, 119, 10]
);
-> at report.RecordWriterTest.setup(RecordWriterTest.java:72)
Actually, there were zero interactions with this mock.
at report.RecordWriterTest.setup(RecordWriterTest.java:72)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
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.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
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.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
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)
谢谢大家。
您没有在模拟上调用 writeRecordToFile 方法。
你有:
1) 一个真实的实例 - RecordWriter writer = new RecordWriter(serverAlias)
2)模拟 - RecordWriter wr = mock(RecordWriter.class);
并在真实实例(编写器)上调用该方法,并尝试验证模拟 (WR) 是否与之交互。
将方法调用更改为:wr.writeRecordToFile(httpRecord.printRecord().getBytes(CHARSET_UTF8));
它应该有效。虽然这似乎是一个毫无意义的测试,但如果你问我。