【发布时间】:2017-11-15 07:38:15
【问题描述】:
我对 Spring Boot 还很陌生,并且将一个基本的微服务放在一起(它接受 Yelp 网址并为该餐厅抓取图像)。我认为现在是编写一些单元测试的好时机,但我遇到的一件事是将模拟类注入到我的 servlet 请求/响应测试中。
我要测试的代码很简单,看起来像这样。这基本上是我的服务的入口点,它接受 JSON 正文,从中提取 Yelp url,然后YelpRequestController.makeYelpRequest() 负责抓取图像并在 ArrayList 中返回图像链接。
@RestController
public class RequestController {
@PostMapping(value = "/")
public ArrayList<String> index(@RequestBody String reqBodyString) {
//my own function to parse the json string
HashMap<String, String> requestBody = parseReqBodyString(reqBodyString);
String yelpURL = requestBody.get("yelpURL");
YelpRequestController yelpRequest = new YelpRequestController(yelpURL);
ArrayList<String> yelpImgLinks = yelpRequest.makeYelpRequest();
return yelpImgLinks;
}
}
这是我的单元测试代码。它基本上创建了一个 JSON 字符串并向我的 RequestController 发送请求,并确保响应正常。它现在通过了,但我希望测试只测试RequestController 而没有别的。目前它通过YelpRequestController 发送测试中的url 并开始抓取图像,这是我不想要的,因为我只想在这个测试中隔离RequestController。我一直在尝试模拟 YelpRequestController 类并返回结果,但我真的遇到了很多麻烦。
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class RequestControllerTest {
@Autowired
private MockMvc mvc;
@Test
public void postRequestController() throws Exception {
ObjectMapping jsonObject = new ObjectMapping();
jsonObject.setYelpUrl("https://www.yelp.ca/biz/l-industrie-pizzeria-brooklyn");
Gson gson = new Gson();
String json = gson.toJson(jsonObject);
mvc.perform(MockMvcRequestBuilders.post("/")
.accept(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isOk());
}
}
我一直在阅读有关如何使用 Mockito 的信息,并了解如何模拟另一个类并将其注入您正在测试的类中。但我真的很难在这里应用它。任何帮助将不胜感激。
【问题讨论】:
-
使 YelpRequestController 成为一个 SPring bean。在您的 RequestController 中自动装配它。将 yelpURL 传递给 makeYelpRequest() 方法而不是构造函数,以使其成为无状态对象。然后您的单元测试可以将模拟 YelpRequestController 注入 RequestController。可测试性就是依赖注入。如果您使用
new自己创建依赖项,则无法注入模拟依赖项。 -
啊,谢谢,我会考虑这样做的!
标签: java unit-testing spring-boot mockito spring-boot-test