In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
How to solve the problem of SpringBootTest unit test error, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
SpringBootTest unit test error @ RunWith (SpringRunner.class) @ SpringBootTest (classes = {DataRulesApplication.class}) @ EnableAutoConfiguration//@SpringBootTest (classes = {DataRulesApplication.class}) public class HuaboAddressTest extends AbstractTestNGSpringContextTests {@ Autowired private HuaboAddressServiceImpl johnyService; @ Test public void queryState () {/ / johnyService.resetAllDistricts (); long startTime = System.currentTimeMillis (); / / johnyService.resetAllDistricts (); / / johnyService.batchUpdate2 () / / johnyService.batchupdate3 (); / / johnyService.resetAllDistricts (); johnyService.updateBatch (); long endTime = System.currentTimeMillis (); System.out.println ("execution time:" + (endTime-startTime)); / / long startTime = System.currentTimeMillis (); / / johnyService.select1 (); / / long endTime = System.currentTimeMillis () / / System.err.println ("execution time 1:" + (endTime-startTime)); / / startTime = System.currentTimeMillis (); / / johnyService.select2 (); / / endTime = System.currentTimeMillis (); / / System.err.println ("execution time 2:" + (endTime-startTime)) @ Test public void check () {} @ Test public void register () {} @ Test public void detail () {} @ Test public void queryCategory () {}}
In fact, you only need to set up the environment in which test is running in setting.
Unit tests @ mock and @ SpringBootTest use
In the process of writing unit tests, we will find that there are many dependencies on the classes that need to be tested, and these dependent classes or resources will have dependencies, so that the construction can not be completed in the unit test code. The method we should deal with is Mock. Simply put, it simulates the classes or resources that need to be built and provides them to the objects that need to be tested.
one。 The unit testing tool mock uses
1. Introduce dependency package
2.mock test class
II. Springboot uses @ SpringBootTest unit test
1. Introduce dependency package
two。 Test class
III. The difference between mock and @ springBootTest
1.mock unit testing does not rely on spring's bean definition file, does not need to start the web service, and is executed very quickly.
2.@springBootTest needs to start the service, perform real operations, and execute slowly. This test method can be selected when real dao layer operations are needed.
After reading the above, do you know how to solve the problem of SpringBootTest unit test error? If you want to learn more skills or 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.