Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Unit testing

What are unit tests

In computer programming, Unit Testing (English: Unit Testing), also known as module Testing, is a test for the correctness of the program module (the smallest Unit of software design). A program unit is the smallest testable part of an application. In procedural programming, a unit is a single program, function, procedure, etc. For object-oriented programming, the smallest unit is a method, including methods in a base class (superclass), an abstract class, or a derived class (subclass).

Quote from Wikipedia

Why do we need unit tests

  • Improve branch coverage to avoid bugs due to code changes
  • To reconstruct
  • Improve code quality
  • The feedback from changing code is so fast that unit tests can fail without changing them

Are you bored with these unit tests

Let me show you the unit testing toolMock

Mock

What is a Mock unit test

Mock is a dynamic Mock object generator that takes the hassle out of manually generating Mock objects and is perfect for unit testing on which services depend

use

The introduction of the project

The Maven project can be introduced with the following code

<dependency>

  <groupId>org.mockito</groupId>

  <artifactId>mockito-core</artifactId>

  <version>2.8.47</version>

</dependency>
Copy the code

Gradle projects can be introduced by the following

org.mockito:mockito-core:2.847.
Copy the code

The sample

Injecting the service you need into the unit test class simulates the object with the following code

@Mock

private TestService testService;
Copy the code

Set the expected return value

when(testService.getById(1L)).thenReturn(test);
Copy the code

Without knowing the input parameters of the getById method, any() can be used to simulate that all parameter inputs are accepted. Note: For multiple parameters, use any() instead of any() and one specific parameter. Causes compilation errors (potholes)

/ / is

when(testService.getBy(any(), any())).thenReturn(test);

/ /

when(testService.getBy(any(), 1L)).thenReturn(test);
Copy the code

Methods that do not return a value can be verified using the verify**() method

verify(testService).update(any());
Copy the code

If you want to test a branch in a throw, you can use exception catching

@Rule

public ExpectedException expectedEx = ExpectedException.none();



// RuntimeException is expected to be thrown

expectedEx.expect(RuntimeException.class);

expectedEx.expectMessage("Database connection failed");



// Call getById to throw RuntimeException

when(testService.getById(1L)).thenThrow(new RuntimeException("Database connection failed"));
Copy the code

How do you call the method you want to test when everything is modeled

@InjectMocks
private TestManager testManager;

testManager.testMock();
Note that the verify method needs to be used after testMock()
verify(testService).update(any());
Copy the code

conclusion

Today we will cover the basic use of mocks. Unit tests are still necessary in daily development to show the stability of the application. Snapshots and other knowledge will be covered later. Mocks make it easier to write unit tests.