5
I’m making a list of exercises and I’m having doubts on the following:
Question 3 - The Verify method, from the org.mockito.Mockito.Verify package, is used to check the amount of times a method is invoked. Add a test in the Atest class to check if when invoking the method area(2) the pi() method is invoked exactly 1 time.
The class to be tested is as follows:
package aula;
public abstract class A {
public long fatorial(long n) {
if (n <= 1) {
return 1;
}
return n * fatorial(n - 1);
}
public abstract Object calc(Object x, Object y) throws
NullPointerException, Exception;
public void msg(String txt) {
}
public double area(double r) {
return 2 * pi() * r;
}
public double pi() {
return Math.PI;
}
public double pow() {
return pi() * pi();
}
public abstract int inc();
}
The test I created using Junit 4 to solve exercise three is as follows::
@Test
public void test7() throws Exception {
when(a.area(2)).thenReturn(2.0);
//when(a.area(2.0)).thenCallRealMethod();
verify(a, times(0)).pi();
//assertSame(2.0, a.area(2));
verify(a, times(1)).pi();
}
As we can see, I am very confused with this question, I have researched many things and I have not been able to resolve my doubt. When I try to run this test, the result is blue and not green as expected. The following error appears:
Wanted but not Invoked: Actually, there Were zero interactions with this mock.
If anyone can help me by explaining with the class I’ve made available up there, I appreciate it! I’m really not getting it done ):
Ana, as you instantiate in the test this abstract class A?
– Dherik