Spring Boot Test使用TestRestTemplate
測試GET
,POST
RESTful API範例如下。
在Spring Boot Test整合測試(integration tests)時,除了RestTemplate
外,也可以利用TestRestTemplate
來測試Web程式端點(endpoints)。
範例環境:
- Java 1.8
- Spring Boot 2.2.1.RELEASE
下面是被測試的Controller類別DemoController
,有兩個API分別為取得會員(GET
)與新增會員(POST
)。
DemoController
package com.abc.demo.controller;
import com.abc.demo.controller.response.DemoResponse;
import com.abc.demo.dto.MemberDto;
import org.springframework.web.bind.annotation.*;
@RestController
public class DemoController {
@GetMapping(value = "/member/{id}")
public DemoResponse<MemberDto> getMember(@PathVariable("id") Long id) {
MemberDto memberDto = new MemberDto(1L, "John", 28);
if (memberDto.getId().equals(id)) {
return new DemoResponse<>("0000", "success", memberDto);
} else {
return new DemoResponse<>("9999", "fail", null);
}
}
@PostMapping(value = "/member/add")
public DemoResponse<MemberDto> addMember(@RequestBody MemberDto memberDto) {
return new DemoResponse<>("0000", "success", memberDto);
}
}
會員資料物件類別。
MemberDto
package com.abc.demo.dto;
public class MemberDto {
private Long id;
private String name;
private Integer age;
public MemberDto(Long id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
// getters and setters...
}
DemoResponse
為呼叫API後的返回物件類別。
DemoResponse
package com.abc.demo.controller.response;
public class DemoResponse<T> {
private String code;
private String message;
private T data;
public DemoResponse(String code, String message, T data) {
this.code = code;
this.message = message;
this.data = data;
}
// getters and setters...
}
測試類別DemoControllerTests
,使用TestRestTemplate
針對DemoController
中的API進行測試。在真實的web測試環境(使用@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
)可以@Autowired
直接注入TestRestTemplate
的實例。
DemoControllerTests
package com.abc.demo.controller;
import com.abc.demo.controller.response.DemoResponse;
import com.abc.demo.dto.MemberDto;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.*;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class DemoControllerTests {
@Autowired
private TestRestTemplate testRestTemplate;
@LocalServerPort
private int port;
@Test
void memberGetJohn() throws Exception {
ResponseEntity<DemoResponse> responseEntity =
testRestTemplate.getForEntity("/member/1", DemoResponse.class);
Assertions.assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
Assertions.assertEquals(HttpStatus.OK.value(), responseEntity.getStatusCodeValue());
Assertions.assertNotNull(responseEntity.getBody());
DemoResponse demoResponse = responseEntity.getBody();
Assertions.assertEquals("0000", demoResponse.getCode());
Assertions.assertEquals("success", demoResponse.getMessage());
ObjectMapper mapper = new ObjectMapper();
String data = mapper.writeValueAsString(demoResponse.getData());
Assertions.assertTrue(data.contains("John"));
String body = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(responseEntity.getBody());
System.out.println(body);
/*
{
"code" : "0000",
"message" : "success",
"data" : {
"id" : 1,
"name" : "John",
"age" : 28
}
}
*/
}
@Test
void memberAddBob() throws Exception {
MemberDto memberDto = new MemberDto(2L, "Bob", 33);
HttpEntity<MemberDto> request = new HttpEntity<>(memberDto);
ResponseEntity<DemoResponse> responseEntity =
testRestTemplate.postForEntity("/member/add", request, DemoResponse.class);
Assertions.assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
Assertions.assertEquals(HttpStatus.OK.value(), responseEntity.getStatusCodeValue());
Assertions.assertNotNull(responseEntity.getBody());
DemoResponse demoResponse = responseEntity.getBody();
Assertions.assertEquals("0000", demoResponse.getCode());
Assertions.assertEquals("success", demoResponse.getMessage());
ObjectMapper mapper = new ObjectMapper();
String data = mapper.writeValueAsString(demoResponse.getData());
Assertions.assertTrue(data.contains("Bob"));
String body = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(responseEntity.getBody());
System.out.println(body);
/*
{
"code" : "0000",
"message" : "success",
"data" : {
"id" : 2,
"name" : "Bob",
"age" : 33
}
}
*/
}
}
參考:
沒有留言:
張貼留言