What should b
, c
and z
be in your sample-code?
Usually you mock every other object your method under test is invoking or those invoked objects require, f.e. a.method1()
call can be mocked like this:
// Arrange or given
Z sut = new Z();
Object method1RetValue = new Object();
A mockedA = mock(A.class);
Mockito.when(mockedA.method1()).thenReturn(method1RetValue);
// set the mocked version of A as a member of Z
// use one of the following instructions therefore:
sut.a = mockedA;
sut.setA(mockedA);
Whitebox.setInternalState(sut, "a", mockedA);
// Act or when
Object ret = sut.method2();
// Assert or then
assertThat(ret, is(equalTo(...)));
As a
is a member of your test-class, you will need to set the mocked version first though, either via direct field assignment, setter-methods or via Whitebox.setInternalState(...)
as depicted in the sample code above.
Note that the mocked method currently returns an object, you can return what ever the method returns. But as your example lacks a real type I just used an object here.