In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.