Error using Mock: Wanted but not Invoked: Actually, there Were zero interactions with this mock

Asked

Viewed 3,287 times

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?

1 answer

5

You are checking whether the method pi() is called once, but you don’t call him at any time. That’s exactly what the error is saying:

Wanted but not Invoked: Actually, there Were zero interactions with this mock.

Required, but not invoked, there were no interactions with this mock.

The first problem is that you pray says that the method needs to be called zero times:

verify(a, times(0)).pi();

And now you say the method needs to be called 1 time:

verify(a, times(1)).pi();

This is not the root of the problem, but will cause problem later. In this case, only the second verify it is necessary.

That line of code: when(a.area(2)).thenReturn(2.0); is not calling the method. You are just setting what the mock behavior should be when you call the method area passing the value 2 as argument. That is, when you make that call: area(2), irrespective of which method is implemented area, the value 2.0 will be returned. If you pass any other value than 2, the mock will do nothing because you have not defined what the behavior should be for other values.

In your case, for your Verify to work, you need to call the actual method, because the method pi() is called in the real method. If you use this code snippet when(a.area(2)).thenReturn(2.0);, when the method area(2) is called, the method pi() will never be called. The value 2.0 will be returned immediately.

Your method should be implemented in this way:

@Test
public void test7() throws Exception {
    // Define o comportamento do mock. 
    //Nesse caso o mock deverá chamar o método real caso o valor dois seja passado como argumento. 
    when(a.area(2)).thenCallRealMethod(); 
    //chama o método a ser testado
    a.area(2);
    //Verifica se o método pi() foi chamado uma vez.
    verify(a, times(1)).pi();
}

If you want Verify to work for any value passed as argument, you can implement the method in this way:

@Test
public void test7() throws Exception {
    // Define o comportamento do mock. 
    //Nesse caso o mock deverá chamar o método real se qualquer valor Double for passado como argumento 
    when(a.area(any(Double.class))).thenCallRealMethod(); 
    //chama o método a ser testado
    a.area(5);
    //Verifica se o método pi() foi chamado uma vez.
    verify(a, times(1)).pi();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.