Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis based on SpringBoot Mock Unit Test

Shulou Source: shulou.com Published: 2022-06-02 12:05:32 10月04日 Update

Editor to share with you the example analysis based on SpringBoot Mock unit testing. I hope you will get something after reading this article. Let's discuss it together.

Basic comments in Junit:

Test: the public void method annotated with this annotation is represented as a test method

BeforeClass: means that it is executed before any public static void method in the class executes

@ AfterClass: indicates execution after any public static void method in the class

@ Before: indicates execution before any public void method annotated with @ Test is executed

@ After: indicates execution after any public void method annotated with @ Test is executed

Detailed explanation of SpringBoot unit test (Mockito, MockBean)

SpringBoot unit test (cobertura generates coverage report)

The concept of 1.Mock:

The so-called mock is to create a fake object of a class, which is used to replace the real object in the test environment, in order to achieve two main purposes:

Verify that some methods of this object are called, how many times they are called, what the parameters are, etc., specify the behavior of some methods of this object, return a specific value, or perform a specific action 2. Add dependency

Spring-boot-starter-test dependencies are included by default in the newly created springBoot project. If not, you can add dependencies in pom.xml by yourself.

Org.springframework.boot spring-boot-starter-test test

When you enter spring-boot-starter-test-2.2.2.RELEASE.pom, you can see that there are already most of the dependencies needed for unit testing in this dependency, such as:

Junit

Mockito

Hamcrest

Note that the included junit is junit5, and junit4 is still used primarily, so add dependencies in pom.xml

Junit junit test

If you don't add it here, you will be prompted to add it when you use the @ RunWith annotation, and clicking Add 'JUnit4' to classpath will automatically add it to pom.xml.

If it is a non-springboot project, other spring projects need to add their own Junit and mockito dependencies. Do not add SpringBoot. There will be an error when adding Test.

Junit junit 4.12 test org.mockito mockito-all 1.10.19 test 3. Commonly used Mockito methods

The use of Mockito generally has the following combinations:

Do/when: including doThrow (…) When (...) / doReturn (...) When (...) / doAnswer (...) When (...)

Given/will: including given (…) .willReturn (…) / given (...) .willAnswer (…)

When/then: including when (…) .thenReturn (...) / when (...) .thenAnswer (…)

For example:

Given (userRepository.findByUserName (Mockito.anyString () .willReturn (user)

Given + willReturn

Given is used to customize the return value of the specified method, which needs to be used with the method at the beginning of will

Through willReturn, you can directly specify the return value of the piling method.

When (userRepository.findByUserName (Mockito.anyString () .thenReturn (user)

When + thenReturn

The role of when is somewhat similar to that of Given, but it is generally used with methods that begin with then.

ThenReturn is similar to willReturn, but it is generally used with when.

After reading this article, I believe you have some understanding of "sample Analysis based on SpringBoot Mock Unit Test". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Tags: Methods tests units annotations objects projects examples analysis beginning time articles falsehood functions actions parameters most of them finished that is commonly used situations Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi vpn OPPO Reno Linux Shulou Tech Info