I have a class X
which takes in a YBuilder
and constructs an instance of Y
public class X {
private Y y;
public X(YBuilder builder) {
y = builder.build();
}
}
I need to mock all calls to Y
, so I have written my Unit tests like this:
@Mock
private Y Y;
@Mock
private YBuilder builder;
@InjectMocks
private X x;
@Before
public void setup() {
when(builder.build()).thenReturn(y); // this does not work
}
I get a Null Pointer Exception
in my main class since the dependency Y
has not been mocked.
I think this is due to the fact that @InjectMocks
instantiates a new instance of X
before I am able to mock the call.
How can I fix this? Any inputs are appreciated.
Aucun commentaire:
Enregistrer un commentaire