Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Mockito to show all invocations on a mock

I have a unit test that is failing and I'm unsure why. I want to be able to see all invocations on the mock that occur in the System Under Test. This is not the behavior that I want for all tests always, simply for a test that I need to quickly tweak to be able to figure out what's wrong.

However, it seems kind of like a hack. Is it possible to do this natively in Mockito, without having to use Thread.currentThread().getStackTrace()?

This is not preferred, because the stack trace includes all the other invocations used internally by Mockito.

like image 322
durron597 Avatar asked Jun 18 '14 20:06

durron597


People also ask

Is Mockito mock same as @mock?

They both achieve the same result. Using an annotation ( @Mock ) is usually considered "cleaner", as you don't fill up your code with boilerplate assignments that all look the same.

Can we mock interface using Mockito?

The Mockito. mock() method allows us to create a mock object of a class or an interface. We can then use the mock to stub return values for its methods and verify if they were called.

How does @mock work in Mockito?

With Mockito, you create a mock, tell Mockito what to do when specific methods are called on it, and then use the mock instance in your test instead of the real thing. After the test, you can query the mock to see what specific methods were called or check the side effects in the form of changed state.


2 Answers

This feature is builtin since Mockito 1.9.5. Just use

mock(ClassToMock.class, withSettings().verboseLogging()) 
like image 103
MRalwasser Avatar answered Sep 24 '22 19:09

MRalwasser


From Mockito 2.2.6 you can inspect a mock with MockingDetails Mockito.mockingDetails(Object mockToInspect).

You can either dig into the MockingDetails properties by invoking : getMock(), getStubbings(), getInvocations() and so for ... or simply use the printInvocations() method that returns :

a printing-friendly list of the invocations that occurred with the mock object. Additionally, this method prints stubbing information, including unused stubbings. For more information about unused stubbing detection see MockitoHint.

For example with JUnit 5 :

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.Mock;
import org.mockito.Mockito;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
public class FooTest {

    Foo foo;

    @Mock
    Bar bar;

    @Test
    void doThat() throws Exception {
        
        Mockito.when(bar.getValue())
               .thenReturn(1000L);          

        // ACTION
        foo.doThat();

        // ASSERTION
        // ...

        // add that to debug the bar mock           
        System.out.println(mockingDetails(bar).printInvocations());
    }
}

And you get an output such as :

[Mockito] Interactions of: Mock for Bar, hashCode: 962287291
 1. bar.getValue();
  -> at Foo.doThat() (Foo.java:15) 
   - stubbed -> at FooTest.doThat(FooTest.java:18)

Note that the classes with a referenced line in the output are links to your source code/test class. Very practical.

like image 35
davidxxx Avatar answered Sep 25 '22 19:09

davidxxx