St.Antario
St.Antario

Reputation: 27375

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?

Upvotes: 0

Views: 583

Answers (1)

Luciano van der Veekens
Luciano van der Veekens

Reputation: 6577

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 \u0000 (byte=0).

Use ByteBuffer.allocate(3).

Upvotes: 1

Related Questions