I am trying to unit test a class A, where I call a method in its Constructor getB(). I want to Unit test this class A without calling this method inside the constructor in order to test other methods inside the class.
class A{
B b;
public A(){
B = getB();
}
doSomething(){
b.doOtherThing();
}
getB(){
return somethingThatReturnB;
}
}
@Test
class TestA{
@Spy
A a;
@Mock
B b;
@BeforeMethod
public void beforeMethod(){
MockiyoAnnotations.openMocks(this);
}
@Test
public void testMethod(){
doReturn(smt).when(b).doOtherThing();
a.doSomething();
}
}
I tried the mocking constructor but this doesn’t call the real Method I want to test.
New contributor
Mohamed Hafidi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.