Here am try to spy a method In that its externally calls another method. All I need is to mock the external method. Below is my simulation of my project kind of legacy project
public class TestClass {
public int dependencyOne(){
System.out.println("Need to mock Externally ");
return 100;
}
public int methodNeedTobeTested(int a){
int c = new AnotherClass().dependencyTwo();
return a + this.dependencyOne() + c;
}
}
public class AnotherClass {
public int dependencyTwo(){
System.out.println("Need to mock externally");
return 100;
}
}
Here is my Test Case
public class TestClassTest {
@InjectMocks
TestClass testClass;
@Mock
AnotherClass anotherClass;
@Before
public void init() {
MockitoAnnotations.initMocks(this);
}
@Rule
public MockitoRule initRule = MockitoJUnit.rule();
@Test
public void methodNeedTobeTested() {
testClass = Mockito.spy(new TestClass());
Mockito.when(anotherClass.dependencyTwo()).thenReturn(10);
Mockito.doReturn(10).when(testClass).dependencyOne();
assertEquals(testClass.methodNeedTobeTested(10),30);
}
}
My Output :
Need to mock externally
java.lang.AssertionError: Expected :30 Actual :120
what am Missing?
Dependency :
byte-buddy-1.10.11.jar
cglib-nodep-3.2.9.jar
hamcrest-core-1.3.jar
javassist-3.24.0-GA.jar
junit-4.12.jar
mockito-all-1.10.19.jar
mockito-core-2.23.0.jar
objenesis-3.0.1.jar