我的测试是分开执行的。当我执行Test时,其中一个会失败:
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();
}
}如上文所述,一个测试通过,另一个错误消息失败。
谢谢你的暗示。
发布于 2020-03-27 13:24:57
我很抱歉。我在一个缓存陷阱中运行。第一个测试激活rest调用的缓存,第二个test中的第二个rest调用尚未执行。
测试后,我清除了所有的缓存:
@After
public void after() {
mockServer.verify();
cacheManager.getCacheNames().forEach(n -> cacheManager.getCache(n).clear());
}https://stackoverflow.com/questions/60886344
复制相似问题