I am trying to stub method of testing class. And it is not working.
Like am testing below class
enter code here
Class to test:
public Class A{
public String method1(){
C c=new C();
int num=c.methodC();
B b=new B();
String str=method2(b);
return str;
}
public String method2(B b){
String str=method2(b);
return str;
}
}
JUnit class:
@RunWith(JUnitPlatform.class)
Class ATest {
@InjectMocks
A a;
@Mock
C c;
@Mock
A a1;
@BeforeEach
public void initMocks() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testMethod1(){
Mockito.doReturn(34).when(c).methodC(anyString());
a1=Mockito.mock(A.class);
Mockito.doReturn("Added successfully").when(a1).method2(any(B.class));
assertEquals("Added successfully", a.method1());
}
}
When am stubbing methodC(...) from Class C it is working. But method2(...) from Class A is not stubbing.
Please let me know what is the issue, and solution.