Combining Mockito with other testing techniques

In software development, testing is a crucial part of ensuring the quality and reliability of the code. Among various testing techniques, Mockito is a popular and powerful mocking framework used for unit testing in Java applications. Mockito allows developers to create mock objects, stub methods, and verify interactions between objects. However, Mockito can also be combined with other testing techniques to enhance the effectiveness of test cases. In this article, we will explore some of the ways to combine Mockito with other testing techniques such as stubbing and assertions.

Stubbing methods with Mockito

Stubbing is the process of providing predefined responses for specific method calls on mock objects. It allows developers to control the behavior of the mocked object during the test. Mockito provides a straightforward way to stub methods using the when() and thenReturn() methods.

Consider the following example of a UserService class:

public class UserService {
    private UserRepository userRepository;

    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public boolean isValidUser(String username) {
        User user = userRepository.findByUsername(username);

        if (user != null) {
            return user.isValid();
        }

        return false;
    }
}

To stub the UserRepository dependency in a test using Mockito, we can create a mock object of UserRepository and define the desired behavior using the when() and thenReturn() methods:

@Test
public void testIsValidUser() {
    UserRepository userRepositoryMock = Mockito.mock(UserRepository.class);
    UserService userService = new UserService(userRepositoryMock);
    
    User mockedUser = Mockito.mock(User.class);
    Mockito.when(userRepositoryMock.findByUsername(anyString())).thenReturn(mockedUser);
    Mockito.when(mockedUser.isValid()).thenReturn(true);
    
    boolean result = userService.isValidUser("john.doe");
    
    assertTrue(result);
}

In the above example, we stubbed the UserRepository.findByUsername() method to return a mocked User object, and then stubbed the User.isValid() method to return true. This allows us to test the isValidUser() method without relying on the actual implementation of UserRepository and User.

Combining assertions with Mockito

Assertions are used to verify the expected behavior of the code being tested. Mockito can be easily combined with assertion libraries like JUnit's Assert class or Hamcrest Matchers to provide more comprehensive verification of test results.

Let's consider the modified UserService class that uses an instance of UserValidator to validate the user:

public class UserService {
    private UserRepository userRepository;
    private UserValidator userValidator;

    public UserService(UserRepository userRepository, UserValidator userValidator) {
        this.userRepository = userRepository;
        this.userValidator = userValidator;
    }

    public boolean isValidUser(String username) {
        User user = userRepository.findByUsername(username);

        if (user != null) {
            return userValidator.validate(user);
        }

        return false;
    }
}

To combine Mockito with assertions, we can use the verify() method provided by Mockito to verify the interactions with mock objects, and then use assertions to validate the expected outcome.

Here's an example test case that combines Mockito and JUnit assertions:

@Test
public void testIsValidUser() {
    UserRepository userRepositoryMock = Mockito.mock(UserRepository.class);
    UserValidator userValidatorMock = Mockito.mock(UserValidator.class);
    UserService userService = new UserService(userRepositoryMock, userValidatorMock);

    String username = "john.doe";
    User mockedUser = Mockito.mock(User.class);
    Mockito.when(userRepositoryMock.findByUsername(username)).thenReturn(mockedUser);
    Mockito.when(userValidatorMock.validate(mockedUser)).thenReturn(true);

    boolean result = userService.isValidUser(username);

    Mockito.verify(userRepositoryMock).findByUsername(username);
    Mockito.verify(userValidatorMock).validate(mockedUser);
    assertTrue(result);
}

In the above example, we use Mockito's verify() method to verify that the findByUsername() method on the UserRepositoryMock and the validate() method on the UserValidatorMock were called with the expected parameters. Additionally, we use the assertTrue() assertion to ensure that the isValidUser() method returns the expected result.

Conclusion

Combining Mockito with other testing techniques such as stubbing and assertions can significantly improve the effectiveness and reliability of unit tests. Mockito provides powerful capabilities for creating mock objects and stubbing methods, while assertion libraries like JUnit's Assert or Hamcrest Matchers allow developers to validate the expected outcomes of tests. By combining these techniques, developers can write more robust and comprehensive unit tests, leading to higher quality and more maintainable codebases.


noob to master © copyleft