In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you what Spring uses RestTemplate and Junit unit testing points for attention, I hope you will learn something after reading this article, let's discuss it!
Considerations for using RestTemplate and Junit unit tests
For the service that was previously injected into the unit test, the unit test can also run away.
Unit testing in springboot
But if you send RestTemplate now, the code is as follows:
Running a unit test will report the following error: (connection denied)
Summarize running unit tests using RestTemplate, which is different from previous unit tests. You must get the project up and running before running the unit tests.
Run the springboot project, and then run the unit test RestTemplate and the results will be fine.
The use and comparison of MockMVC and TestRestTemplate
MockMVC and TestRestTemplate are both test tools for testing RestFul interface, and TestRestTemplate can be replaced by WebTestClient.
MockMVC
MockMVC simulates a SpringMVC runtime environment and does not really run a complete Servlet container.
Usage
Add @ AutoConfigureMockMvc to the test class, and then dependency injection into the MockMvc object.
Characteristics
1. Support the rollback of data during testing, which is suitable for testing a method that involves modifying database data
2. Because MockMVC does not really start a complete server service, when testing some detectable exceptions and will return error page (status code starting with 4), because error page is provided by Servlet container, MockMVC cannot test these methods, he will still return 200status code.
RestTemlate
RestTemplate is to start a complete server.
Usage
Set the webEnvironment property to WebEnvironment.RANDOM_PORT or WebEnvironment.DEFINED_PORT on the @ SpringBootTest annotation
/ / An highlighted block@SpringBootTest (webEnvironment = WebEnvironment.RANDOM_PORT) class RandomPortTestRestTemplateExampleTests {@ Test void exampleTest (@ Autowired TestRestTemplate restTemplate) {String body = restTemplate.getForObject ("/", String.class); assertThat (body) .isEqualTo ("Hello World");}}
Characteristics
1. Rollback is not supported because the running test method and the server service are not in the same thread, so the server service cannot be controlled.
2. You can test a method that will throw a detectable exception.
3. TestRestTemplate does not support testing Http requests of Patch type. You can use TestWebClient instead.
4. The returned result can be deserialized to a specific object, which MockMVC cannot do, so it can only convert the returned Json data into String.
After reading this article, I believe you have a certain understanding of "what are the precautions for Spring to use RestTemplate and Junit unit testing". If you want to know more about it, 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.