I am trying to figure out a way to mock the method inside the following builder for my unit test.
public class A {
private C ObjectC;
private D ObjectD;
public getObjectC() {
// This should not be executed in Unit test as object creation involves external systems.
// The object C creation depends upon the properties set by Withers inside the inner Builder class.
return new C();
}
public A(ABuilder builder) {
this.ObjectD = builder.ObjectD;
this.ObjectC = geObjectC();
}
// Similarly there are three for* methods.
public D forXYZ(final String xyz) {
this.ObjectD.setXYZ(xyz);
// Sometimes based on the property of XYX, the value of ABC changes.
this.ObjectD.setABC(null);
return this.ObjectD;
}
public static class ABuilder() {
private D ObjectD;
// Similar to the below wither method, I have four Withers with few branch logic to built Object D
public A withFoo(final String foo) {
this.ObjectD.setFoo(foo);
return this;
}
public A build() {
return new A(this);
}
}
My Unit test class
A ObjectA = new A.ABuilder().withFoo("foo").build();
assertEquals(A.getObjectD().getFoo(),"foo");
// Similarly assert other properties of Object D. This assertion is required as the property values varies based on what the user passes in the wither.
// Eg : If 'even' is set for value 10, 'odd' is set for value 5.
D ObjectD = A.forXYZ("Request");
ObjectD.doSomething();
// Now assert the properties of D and see if proper XYZ is set.
// Similarly I have two three for* methods inside A used for modifying the property of D after creation.
// The inside builder takes care of the creation of ObjectD with properties that are going to be set only once and changes very rarely. Where as the outside class A has for* methods which handles the setters of other properties which are going to be set multiple times.
D ObjectD = A.forXYZ("Request");
ObjectD.doSomethingXYZ();
A.forABC("RequestId").doSomethingABC();
Tried using Spy but it did not mocked the internal getObjectC method.
@Mock C ObjectC;
A a = spy(new A());
doReturn(ObjectC).when(A).getObjectC();
Currently using TestNG and Mockito for my Unit tests. I don't want to use PowerMockito to achieve this. How can I mock the above use case ?