I’m not super familiar with mockito. I am trying to mock the results of a class that is being instantiated within another class.
public class TestClass {
@Test
public void testSomething() {
SomethingConfig somethingConfigMock = mock(SomethingConfig.class);
final ClassBeingTested cbt = new ClassBeingTested(
parameter1,
parameter2,
somethingConfigMock
);
cbt.getResults();
}
}
public class ClassBeingTested {
public ClassBeingTested(parameter1, paramater2, somethingConfig) {
this.parameter1 = parameter1;
this.parameter2 = parameter2;
this.somethingConfig = somethingConfig;
...
}
public Results getResults() {
// do lots of stuff
OtherClassToTest otherClassToTest = new OtherClassToTest(somethingConfig);
String s = otherClassToTest.getSomeString();
// do lots of stuff
}
}
so in this example I just want to expect that once OtherClassBeingTested is instantiated with somethingConfigMock it will return generic results for methods like getSomeString().
In my mind it would look something like
when(new OtherClassToTest(somethingConfigMock)).thenReturn(otherClassToTestMock);
when(otherClassToTestMock.getSomeString()).thenReturn("genericString");
Am I looking at this the wrong way or is there an easy way to do this without significant refactoring?
I have tried just mocking somethingConfigMock but at a certain point i always get a NullPointerException within OtherClassToTest.
Max Deyo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.