argumentcaptor java mockito

Solutions on MaxInterview for argumentcaptor java mockito by the best coders in the world

showing results for - "argumentcaptor java mockito"
Aliyah
16 Oct 2020
1// Consider you have a method:
2class A {
3    public void foo(OtherClass other) {
4        SomeData data = new SomeData("Some inner data");
5        other.doSomething(data);
6    }
7}
8
9// Now if you want to check the inner data you can use the captor:
10
11// Create a mock of the OtherClass
12OtherClass other = mock(OtherClass.class);
13
14// Run the foo method with the mock
15new A().foo(other);
16
17// Capture the argument of the doSomething function
18ArgumentCaptor<SomeData> captor = ArgumentCaptor.forClass(SomeData.class);
19verify(other, times(1)).doSomething(captor.capture());
20
21// Assert the argument
22SomeData actual = captor.getValue();
23assertEquals("Some inner data", actual.innerData);
24