Spring Boot 单元测试 - 服务方法未被调用
SpringBoot Unit test - Service method is not invoked
我是单元测试的新手。在引用 google 之后,我创建了一个测试 class 来测试我的控制器,如下所示:
@RunWith(SpringRunner.class)
@WebMvcTest(PromoController.class)
public class PromoApplicationTests {
@Autowired
protected MockMvc mvc;
@MockBean PromoService promoService;
protected String mapToJson(Object obj) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.writeValueAsString(obj);
}
protected <T> T mapFromJson(String json, Class<T> clazz)
throws JsonParseException, JsonMappingException, IOException {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(json, clazz);
}
@Test
public void applyPromotionTest_1() throws Exception {
String uri = "/classPath/methodPath";
List<Cart> cartLs = new ArrayList<Cart>();
// added few objects to the list
String inputJson = mapToJson(cartLs);
MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(uri)
.contentType(MediaType.APPLICATION_JSON).content(inputJson)).andReturn();
int status = mvcResult.getResponse().getStatus();
assertEquals(200, status);
String actual = mvcResult.getResponse().getContentAsString();
String expected = "{\"key1\":val1, \"key2\":\"val 2\"}";
assertEquals(expected, actual, true);
}
}
我有以下控制器和服务 Class :
@RequestMapping("/classPath")
@RestController
public class PromoController {
@Autowired
PromoService promoService;
@PostMapping("/methodPath")
public PromoResponse applyPromo(@RequestBody List<Cart> cartObj) {
PromoResponse p = promoService.myMethod(cartObj);
return p;
}
}
@Component
public class PromoServiceImpl implements PromoService{
@Override
public PromoResponse myMethod(List<Cart> cartList) {
// myCode
}
}
当我调试单元测试时,控制器中的 p 对象为空。
我的状态为 200,但不是预期的 JSON 响应
我在这里错过了什么?
而使用@WebMvcTest spring 启动只会初始化web层,不会加载完整的应用程序上下文。并且您需要在使用 @WebMvcTest
.
时使用 @MockBean
创建和注入模拟
你做了什么
@MockBean
PromoService promoService;
Spring Boot instantiates only the web layer rather than the whole context
We use @MockBean to create and inject a mock for the GreetingService (if you do not do so, the application context cannot start), and we set its expectations using Mockito.
但是,因为它是模拟 bean,所以您有责任模拟 myMethod()
调用
@Test
public void applyPromotionTest_1() throws Exception {
String uri = "/classPath/methodPath";
List<Cart> cartLs = new ArrayList<Cart>();
// added few objects to the list
// create PromoResponse object you like to return
When(promoService.myMethod(ArgumentsMatchesr.anyList())).thenReturn(/*PromoResponse object */);
String inputJson = mapToJson(cartLs);
MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(uri)
.contentType(MediaType.APPLICATION_JSON).content(inputJson)).andReturn();
int status = mvcResult.getResponse().getStatus();
assertEquals(200, status);
String actual = mvcResult.getResponse().getContentAsString();
String expected = "{\"key1\":val1, \"key2\":\"val 2\"}";
assertEquals(expected, actual, true);
}
我是单元测试的新手。在引用 google 之后,我创建了一个测试 class 来测试我的控制器,如下所示:
@RunWith(SpringRunner.class)
@WebMvcTest(PromoController.class)
public class PromoApplicationTests {
@Autowired
protected MockMvc mvc;
@MockBean PromoService promoService;
protected String mapToJson(Object obj) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.writeValueAsString(obj);
}
protected <T> T mapFromJson(String json, Class<T> clazz)
throws JsonParseException, JsonMappingException, IOException {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(json, clazz);
}
@Test
public void applyPromotionTest_1() throws Exception {
String uri = "/classPath/methodPath";
List<Cart> cartLs = new ArrayList<Cart>();
// added few objects to the list
String inputJson = mapToJson(cartLs);
MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(uri)
.contentType(MediaType.APPLICATION_JSON).content(inputJson)).andReturn();
int status = mvcResult.getResponse().getStatus();
assertEquals(200, status);
String actual = mvcResult.getResponse().getContentAsString();
String expected = "{\"key1\":val1, \"key2\":\"val 2\"}";
assertEquals(expected, actual, true);
}
}
我有以下控制器和服务 Class :
@RequestMapping("/classPath")
@RestController
public class PromoController {
@Autowired
PromoService promoService;
@PostMapping("/methodPath")
public PromoResponse applyPromo(@RequestBody List<Cart> cartObj) {
PromoResponse p = promoService.myMethod(cartObj);
return p;
}
}
@Component
public class PromoServiceImpl implements PromoService{
@Override
public PromoResponse myMethod(List<Cart> cartList) {
// myCode
}
}
当我调试单元测试时,控制器中的 p 对象为空。 我的状态为 200,但不是预期的 JSON 响应
我在这里错过了什么?
而使用@WebMvcTest spring 启动只会初始化web层,不会加载完整的应用程序上下文。并且您需要在使用 @WebMvcTest
.
@MockBean
创建和注入模拟
你做了什么
@MockBean
PromoService promoService;
Spring Boot instantiates only the web layer rather than the whole context
We use @MockBean to create and inject a mock for the GreetingService (if you do not do so, the application context cannot start), and we set its expectations using Mockito.
但是,因为它是模拟 bean,所以您有责任模拟 myMethod()
调用
@Test
public void applyPromotionTest_1() throws Exception {
String uri = "/classPath/methodPath";
List<Cart> cartLs = new ArrayList<Cart>();
// added few objects to the list
// create PromoResponse object you like to return
When(promoService.myMethod(ArgumentsMatchesr.anyList())).thenReturn(/*PromoResponse object */);
String inputJson = mapToJson(cartLs);
MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(uri)
.contentType(MediaType.APPLICATION_JSON).content(inputJson)).andReturn();
int status = mvcResult.getResponse().getStatus();
assertEquals(200, status);
String actual = mvcResult.getResponse().getContentAsString();
String expected = "{\"key1\":val1, \"key2\":\"val 2\"}";
assertEquals(expected, actual, true);
}