【发布时间】:2020-03-27 21:51:41
【问题描述】:
我的测试在单独执行时运行。当我执行测试类时,其中一个失败了:
java.lang.AssertionError: Further request(s) expected leaving 1 unsatisfied expectation(s).
0 request(s) executed.
测试类:
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles("test")
@Transactional
public class ProductListControllerIT {
@Autowired RestTemplate restTemplate;
@Autowired MockMvc mvc;
@Test
public void testGet_1() throws Exception {
MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate);
mockServer.expect(ExpectedCount.once(),
requestTo(/* any url */))
.andExpect(method(HttpMethod.GET))
.andRespond(withStatus(HttpStatus.OK)
.contentType(MediaType.APPLICATION_JSON)
.body(/* JSON-STRING */)
);
var model = mvc.perform(MockMvcRequestBuilders.get("/url")
.andReturn().getModelAndView().getModel();
mockServer.verify();
}
@Test
public void testGet_2() throws Exception {
MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate);
mockServer.expect(ExpectedCount.once(),
requestTo(/* any url */))
.andExpect(method(HttpMethod.GET))
.andRespond(withStatus(HttpStatus.OK)
.contentType(MediaType.APPLICATION_JSON)
.body(/* JSON-STRING */)
);
var model = mvc.perform(MockMvcRequestBuilders.get("/url")
.andReturn().getModelAndView().getModel();
mockServer.verify();
}
}
一个测试通过,另一个失败并显示上述错误消息。
感谢您的提示。
【问题讨论】:
标签: spring-boot spring-boot-test mockrestserviceserver