為什么要求對Web服務的API進行測試?請移步這里Java中幾種Unit Test場景
假設:我們需要需要對前端提供一個API,這個API的功能是:通過結(jié)果關(guān)鍵字查詢產(chǎn)品的描述信息萧求;
目錄:
1,建立API契約
2, API代碼
3, 測試代碼:測試參數(shù)顶瞒,測試找到資源情況下的返回結(jié)果夸政,測試未找到資源是的返回結(jié)果
建立API的契約:
建議使用Swagger 的yaml格式來定義,這里為了易讀榴徐,使用純中文描述.
GET請求
http://localhost:8888/api/products/{productId}
查詢參數(shù):
countryCode: String
返回結(jié)果:
200: 找到了相關(guān)資源
{
"name" : "ABC",
"countryCode" : "US",
"description" : "Super ABC"
}
404: 未找到資源
{
"message": "Not found."
}
API代碼
ProductResource
@RestController
@RequestMapping("/api/products")
public class ProductResource {
@Autowired
private ProductService productService;
@GetMapping("/{productId}
public Product query(@PathVariable("productId") String productId,
@RequestParam("countryCode") String countryCode) thrown NotFoundException {
productService.query(productId, countryCode);
}
}
ProductService
@Service
public class ProductService {
public ProductDTO query(String productId, String countryCode) throws NotFoundException {
if ("ABC".equals(productId) && "US".equals(countryCode)) {
return new ProductDTO("ABC", "US", "Super ABC");
}
throw new NotFoundException("Not found");
}
}
ProductDTO
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ProductDTO {
private String name;
private String countryCode;
private String description;
}
測試代碼
測試參數(shù):should_response_4XX_when_country_code_is_empty()
測試找到資源情況下的返回結(jié)果:should_response_expected_product_information_when_find_product_by_product_id_and_country_code()
測試未找到資源是的返回結(jié)果:should_response_404_when_product_not_found_by_produt_id_and_country_code()
@RunWith(SpringRunner.class)
@SpringBootTest(class=Application.class)
public class ProductResourceTest {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
protected MockMvc mockMvc;
@Autowired
private ProductResource productResource;
@Autowired
private ProductService productService;
@Autowired
private GlobalEntityExceptionHandler globalEntityExceptionHandler;
@BeforeClass
public static void setup() {
this.mockMvc = MockMvcBuilders.standaloneSetup(productResource)
.setControllerAdvice(globalEntityExceptionHandler)
.build();
}
@Test
public void should_response_4XX_when_country_code_is_empty() {
mockMvc.perform(
get("/api/products/ABC")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().is4xxClientError());
}
@Test
public void should_response_expected_product_information_when_find_product_by_product_id_and_country_code() {
String expectedResult = OBJECT_MAPPER.writeValueAsString(ProductDTO.builder()
.name("ABC")
.countryCode("US");
.description("Super ABC")
.build());
mockMvc.perform(
get("/api/products/ABC")
.contentType(MediaType.APPLICATION_JSON)
.param("countryCode", "US"))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(content().String(expectedResult));
}
@Test
public void should_response_404_when_product_not_found_by_produt_id_and_country_code() {
String expectedResult = "{\"message\":\"\"}";
mockMvc.perform(
get("/api/products/AAA")
.contentType(MediaType.APPLICATION_JSON)
.param("countryCode", "GB"))
.andExpect(status().isNotFound())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(content().String(expectedResult));
}
}