Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

argumentcaptor java mockito

// Consider you have a method:
class A {
    public void foo(OtherClass other) {
        SomeData data = new SomeData("Some inner data");
        other.doSomething(data);
    }
}

// Now if you want to check the inner data you can use the captor:

// Create a mock of the OtherClass
OtherClass other = mock(OtherClass.class);

// Run the foo method with the mock
new A().foo(other);

// Capture the argument of the doSomething function
ArgumentCaptor<SomeData> captor = ArgumentCaptor.forClass(SomeData.class);
verify(other, times(1)).doSomething(captor.capture());

// Assert the argument
SomeData actual = captor.getValue();
assertEquals("Some inner data", actual.innerData);
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #argumentcaptor #java #mockito
ADD COMMENT
Topic
Name
1+4 =