简体   繁体   English

想要但不调用:Mockito测试

[英]Wanted but not invoke: Mockito test

I have a problem. 我有个问题。 I don't understand why mockito go in exception. 我不明白为什么Mockito会例外。 RecordWriter write a Record into the file. RecordWriter将记录写入文件。 I want verify if it write or not. 我想验证它是否写入。 I implemented another function and i tried to mock(OutputStream.class), but the result is the same. 我实现了另一个函数,并尝试模拟(OutputStream.class),但结果是相同的。 My current test is: 我当前的测试是:

    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));

            }

The exception is: 例外是:

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)

Thanks to all. 谢谢大家。

You are not calling the writeRecordToFile method on the mock. 您没有在模拟中调用writeRecordToFile方法。

You have: 你有:

1) a real instance - RecordWriter writer = new RecordWriter(serverAlias) 1)一个真实实例RecordWriter writer = new RecordWriter(serverAlias)

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

and you call the method on the real instance (writer) and try to verify if the mock (wr) was interacted with. 然后在实实例(编写器)上调用该方法,然后尝试验证该模拟对象(wr)是否与之交互。

Change the method call to: wr.writeRecordToFile(httpRecord.printRecord().getBytes(CHARSET_UTF8)); 将方法调用更改为: wr.writeRecordToFile(httpRecord.printRecord().getBytes(CHARSET_UTF8));

and it should work. 它应该工作。 Although it seems quite a pointless test, if you ask me. 尽管这似乎是毫无意义的考验,但是如果您问我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM