- Spring 5.0 Microservices(Second Edition)
- Rajesh R V
- 219字
- 2021-07-02 19:45:02
Testing Spring Boot microservice
There are multiple ways to test REST/JSON Spring Boot microservices. The easiest way is to use a web browser or a curl command pointing to the URL, like this:
curl localhost:8080
There are number of tools available for testing RESTful services such as Postman, Advanced Rest Client, SOAP UI, Paw, and more.
In this example, for testing the service, the default test class generated by Spring Boot will be used. Adding a new test case to ApplicatonTests.java results in this:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class ApplicationTests {
@Autowired
private TestRestTemplate restTemplate;
@Test
public void testSpringBootApp() throws JsonProcessingException,
IOException {
String body = restTemplate.getForObject("/", String.class);
assertThat(new ObjectMapper().readTree(body)
.get("message")
.textValue())
.isEqualTo("Hello World!");
}
}
Note that @SpringBootTest is a simple annotation for testing Spring Boot applications, which enables Spring Boot features during test execution. webEnvironment=WebEnvironment.RANDOM_PORT property directs the Spring Boot application to bind to a random port. This will be handy when running many Spring Boot services as part of a regression test. Also note that TestRestTemplate is being used for calling the RESTful service. TestRestTemplate is a utility class, which abstracts the lower-level details of the HTTP client, and also automatically discovers the actual port used by Spring Boot.
To test this, open a terminal window, go to the project folder, and run mvn install.
- Qt 5 and OpenCV 4 Computer Vision Projects
- Django+Vue.js商城項目實戰
- OpenCV實例精解
- Microsoft Dynamics 365 Extensions Cookbook
- Ext JS Data-driven Application Design
- 碼上行動:零基礎學會Python編程(ChatGPT版)
- 精通軟件性能測試與LoadRunner實戰(第2版)
- Visual Basic程序設計習題解答與上機指導
- Apache Karaf Cookbook
- Unreal Engine 4 Shaders and Effects Cookbook
- AutoCAD 2009實訓指導
- Unity 5.X從入門到精通
- Microsoft HoloLens By Example
- Offer來了:Java面試核心知識點精講(框架篇)
- 軟硬件綜合系統軟件需求建模及可靠性綜合試驗、分析、評價技術