Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the considerations for Spring to use RestTemplate and Junit unit tests

Shulou Source: shulou.com Published: 2022-06-02 22:24:04 09月18日 Update

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!

Tags: Tests units runs methods data matters considerations support services usage objects characteristics status articles results projects differences code done containers Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Tech Info Shulou Technology OPPO Reno MariaDB