gpt4 book ai didi

unit-testing - 想要但不调用 : Mockito test

转载 作者:行者123 更新时间:2023-11-28 21:27:25 25 4
gpt4 key购买 nike

我有一个问题。我不明白为什么 mockito 会异常(exception)。RecordWriter 将记录写入文件。我想验证它是否写入。我实现了另一个功能并尝试模拟(OutputStream.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));

}

异常(exception)情况是:

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)

感谢大家。

最佳答案

您没有在 mock 上调用 writeRecordToFile 方法。

你有:

1) 一个真实的实例 - RecordWriter writer = new RecordWriter(serverAlias)

2) 模拟 - RecordWriter wr = mock(RecordWriter.class);

然后您在真实实例(writer)上调用该方法并尝试验证是否与 mock(wr)进行了交互。

将方法调用更改为:wr.writeRecordToFile(httpRecord.printRecord().getBytes(CHARSET_UTF8));

它应该可以工作。尽管这似乎是一个毫无意义的测试,但如果你问我的话。

关于unit-testing - 想要但不调用 : Mockito test,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36083385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com