Use Mocks in Testing? Choose the Lesser Evil!

Use Mocks in Testing? Choose the Lesser Evil!

Here, I explain best practices around mocking and why it might be better to avoid mocking in order to have real code quality.

Mocking Test Methodology 

The key idea of mocking is to replace real code (e.g. calls to a database or service) with artificial functionality that has the same return type. There are different approaches in this practice. Here, I explain best practices and why it might be better to avoid mocking in order to have real code quality.

User Service - Example to Be Tested With Mocks

Let's write simple application that fetch users from http service. 

Java

public Optional<User> createUser(String name) {
        try {
            //Preparation of request: auth, body and media type of request
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(APPLICATION_JSON);
            headers.set("auth", "mySecretKey");
            //Specifying body
            HttpEntity<String> request =
              new HttpEntity<String>("{\"name\":" + name + "}", headers)
            //Making request with and deserializing response into User object
            User newUser = (new RestTemplate()).postForEntity("/server/users",                                                      request, User.class).getBody();
            newUser.setIssueDate(Date.now());
            return Optional.of(newUser);
        } catch (Exception e) {
            System.out.println("Something went wrong! : " + e.getMessage());
            Return Optional.empty();
        }
}
@Data
class User {
    private Integer id;
    private String name;
    private Date issueDate;
}

Implementation Details

The method, createUser, basically does 4 things: 

  1. Preparation of request: authentication, preparing body and media type of request (line 4-5 corresponding to 1, 2, 3 step on the diagram. 
  2. Making request to server with given url (line 11 or step 4).
  3. Receiving the response and deserializing it into an object. (line 11- yes the same line! and step 9 on diagram).
  4. Setting the current date to created user (line 12, or step 10).

The remaining actions are done on server-side and hidden from application:

   1. Server check that body has json type and correct auth key (step 5,6).

   2. Make business logic (create a user) (step 7).

   3. Generate response with new user id (step 8).

createUser workflow

performance testing spring mockito powermock mock http server http testing

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Spring Boot Testing Tutorial - Unit Testing with Junit 5 and Mockito

Spring Boot Testing Tutorial – Learn about Unit Testing Spring Boot application using Junit 5 and we will see how to use Mocking frameworks like Mockito. Learn how to write unit tests using libraries like Junit 5 and Mockito

How to Test Mobile App Performance: 3 Key Components - DZone Performance

In this post, we’ll go over the different factors that impact a mobile app’s performance. We’ll also explain how to test mobile app performance.

How to Do API Testing?

API endpoint when you send the get request to that URL it returns the JSON response. In this article, I am going to use postman assertions for all the examples since it is the most popular tool. But this article is not intended only for the postman tool.

Where To Learn Test Programming — July 2020 Edition

What do you do when you have lots of free time on your hands? Why not learn test programming strategies and approaches?

7 Approaches to Testing - DZone Performance

This article is for anyone who wants to better understand project quality assurance processes — check out 7 different testing approaches and their benefits.