CodeGym/Java Course/Module 3. Java Professional/Revealing Specific Behavior in Objects

Revealing Specific Behavior in Objects

Available

5.1 The verify() method

In addition to creating virtual objects, another interesting task often arises - to make sure that the class under test calls the right methods of the right objects. Moreover, he called the required number of times, with the correct parameters, and the like.

For this, Mockito also has some magic - a family of methods Mockito.verify(…). The general rule that specifies method call checking is:

Mockito.verify(an object).method name(parameter);

Example:

@ExtendWith(MockitoExtension.class)
class VerifyTest {
    @Mock
    List<String> mockList;

    @Test
    public void whenMockAnnotation() {
        //method call
        String name = mockList.get(10);

        // check if the method was called
        Mockito.verify(mockList).get(10);
    }
}

During the method call, verify()we set the rule that mockitoListthe method with the parameter 10 should be called on the object get().

5.2 The verify() method with checking the number of calls

Sometimes there are more complex verification scenarios. For example, you need to check not just the fact that the method was called, but, for example, that it was called 3 times. Or it was called from you in a cycle and therefore should have been called N times.

We will not ask if this can be done, we will immediately ask: how to write down such a rule? And again, Mockito doesn't let us down. The rule can be specified as:

Mockito.verify(an object,quantity).method name(parameter);

Important! Quantity is not a type int, but a special object that can define various patterns. Do you remember the different versions of the method any()? It's the same here - there are special methods with which you can set various scenarios:

Method Description
1 never() The method should never be called
2 times(n) n times
3 atLeast(n) n or more times
4 atLeastOnce() 1 or more times
5 atMost(n) n or less times
6 only() There should be only one call and only to this method

Example:

String name1 = mockList.get(1);  //method call
String name2 = mockList.get(2);  //method call
String name3 = mockList.get(3);  //method call

//check that the get() method was called 3 times
Mockito.verify(mockList, times(3)).get(anyInt());

You can also require that, apart from the specified method invocations, no other references to the object be made . There is a rule for this:

Mockito.verifyNoMoreInteractions(an object);

5.3 Method call order

The previous rules did not regulate the order of calling methods in any way. The rule just has to be fulfilled and that's it. But there are situations when the order of method calls is important, and Mockito has a solution for this too.

A strict order of method calls can be specified using a special object InOrder. First you need to create it:

InOrder inOrder = Mockito.inOrder(an object);

And then add rules to it by calling methods verify().

Example:

List<String> mockedList = mock(MyList.class);
mockedList.size();
mockedList.add("a parameter");
mockedList.clear();

InOrder inOrder = Mockito.inOrder(mockedList);
inOrder.verify(mockedList).size();
inOrder.verify(mockedList).add("a parameter");
inOrder.verify(mockedList).clear();

5.4 Checking exceptions in Mockito

The fact that exceptions have been thrown is checked a little differently. To do this, use the method assertThrows(). The general format for such a check is:

Assertions.assertThrows(exception.class, () -> an object.methodname());

Nothing complicated.

Example:

@ExtendWith(MockitoExtension.class)
class ThenThrowTest {
    @Mock
    List mockList;

    @Test
    public void whenMockAnnotation() {
        //set the behavior of the method (needed for demonstration purposes only)
        Mockito.when(mockList.size()).thenThrow(IllegalStateException.class);

        //check if an IllegalStateException will be thrown when calling the size method
        assertThrows(IllegalStateException.class, () -> mockList.size());
    }
}
Comments
  • Popular
  • New
  • Old
You must be signed in to leave a comment
This page doesn't have any comments yet