Main Class
public class BootSample {
public int call(int m) {
System.out.println("Entering into Call Method");
int n = m*10;
TestUtil testUtil = new TestUtil();
testUtil.add(m, n);
System.out.println("End of Call Method Value n : " + n);
return n;
}
}
Util Class
public class TestUtil {
public void add(int a, int b) {
System.out.println(" Entering into TestUtil Method ");
int c = a +b;
System.out.println(" End of TestUtil Method Value : " + c);
}
}
Test Class
@RunWith(MockitoJUnitRunner.class)
public class BootSampleTest {
@Mock
TestUtil testUtil;
@Before
public void setup() {
}
@Test
public void utilSuccess() throws Exception {
BootSample bootSample = new BootSample();
doNothing().when(testUtil).add(any(Integer.class),any(Integer.class));
int result = bootSample.call(10);
assertEquals(result,100);
}
}
Output :
Entering into Call Method
Entering into TestUtil Method
End of TestUtil Method Value : 110
End of Call Method Value n : 100
I'm trying to mock util void method call with doNothing but doesn't work.Can anyone please help me with solution? I'm stuck with similar functionality in our application.