简体   繁体   中英

Mockito verify method called once

I'm trying to use mockito to verify if method is called. Here is an example:

@Test
public void t(){
    InvokedFromTest ift = mock(InvokedFromTest.class);
    TestClass t = new TestClass();
    t.ift = ift;

    t.mm(new String(ByteBuffer.allocate(4).put("123".getBytes()).array()));
    verify(ift, times(1)).m("123");
}

private static class TestClass{
    public InvokedFromTest ift;
    public void mm(String s){ ift.m(s); }
}

private static class InvokedFromTest{
    public void m(String s){}
}

But when runnig t() I got the following exception:

Argument(s) are different! Wanted:
invokedFromTest.m("123");
-> at com.pack.age.TableRowIgniteProcessingLogicTest.t(TableRowIgniteProcessingLogicTest.java:62)
Actual invocation has different arguments:
invokedFromTest.m("123");
-> at com.pack.age.TableRowIgniteProcessingLogicTest$TestClass.mm(TableRowIgniteProcessingLogicTest.java:67)

Why? Why did I get this error? How to make this test work as expected?

You are allocating a byte buffer of length 4 while only 3 digits (each of length 1 byte) are stored. Passing this byte array to the constructor of String, creates a String of 4 characters where the last character is \ (byte=0).

Use ByteBuffer.allocate(3) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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