作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在客户端测试我的 REST 服务(在服务器端调用 Spring MVC)。
我可以使用 PostMan 在服务器端使用 Json 访问 url,一切正常,所以我知道服务器端是正确的。
服务器端 MVC 方法:的代码:
@RequestMapping(value = "/device/event/{type}", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
public void processEvent(@PathVariable String type,
@RequestBody DeviceEvent event) throws Exception {
log.info("Device Event["+type+"] generated by Device["+event.getDeviceAddress()+"] ");
if (StringUtils.isBlank(type) || event == null ||
StringUtils.isBlank(event.getDeviceAddress()) ||
StringUtils.isBlank(event.getDeviceType())) {
return;
}
deviceEventHandlers.get(type).handleEvent(event);
}
客户端方法:代码:
public void newNodeEvent(DeviceEvent event) {
final String apiUrl = eventRepository + "/" + DeviceEventType.DEVICE_NEW.getCode();
this.sendEvent(event, apiUrl);
}
@Async
private void sendEvent(final DeviceEvent event, final String apiUrl) {
try {
ResponseEntity<String> response = restTemplate.postForEntity(new URI(apiUrl), event,
String.class);
HttpStatus status = response.getStatusCode();
if (status.value() != HttpStatus.OK.value()) {
log.error("Event Controller returned with status code[" + status.value()
+ "] for [" + apiUrl + "]");
}
}
catch (ResourceAccessException | URISyntaxException ex) {
log.error("Cannot connect to Event Controller, for [" + apiUrl + "]");
}
}
测试类:代码:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { AnnotationConfigContextLoader.class, MockRestConfig.class })
public class DeviceEventServiceTest {
@Autowired
private RestTemplate restTemplate;
@Autowired
private IDeviceEventService deviceEventService;
@Value("${event.repository.path}")
private String eventRepository;
private MockRestServiceServer mockServer;
@Before
public void setUp() throws Exception {
mockServer = MockRestServiceServer.createServer(restTemplate);
}
@Test
public void testNewNodeEventTriggered() throws JsonGenerationException,
JsonMappingException, IOException, URISyntaxException {
DeviceEvent de = new DeviceEvent();
de.setHubId(123);
de.setDeviceAddress("1234567890abcdef");
de.setSwVersion("1.0-1");
de.setHwVersion("1.0-1");
de.setDeviceType(DeviceType.WIRELESS_VALVE.getCode());
String expectedContent = JsonConverter.objectToJsonString(de, true);
mockServer.expect(requestTo(new URI(eventRepository + "/DNW")))
.andExpect(method(HttpMethod.POST))
.andExpect(content().contentType(JsonConverter.APPLICATION_JSON_UTF8))
.andExpect(content().string(expectedContent))
.andRespond(withSuccess());
deviceEventService.newNodeEvent(de);
mockServer.verify();
assertTrue(true);
}
}
客户端代码 100% 工作,但测试失败:java.lang.IllegalArgumentException
: 未指定 InputStream
任何帮助将不胜感激,因为我已经花了很多时间在这上面
最佳答案
请试试这个:
mockServer.expect(requestTo(new URI(eventRepository + "/DNW"))).andExpect(method(HttpMethod.POST))
.andExpect(content().contentType(JsonConverter.APPLICATION_JSON_UTF8))
.andExpect(content().string(expectedContent))
.andRespond(withStatus(HttpStatus.OK).body(new byte[0]));
与你相比有什么变化:
... .andRespond(withSuccess()); // Yours
... .andRespond(withStatus(HttpStatus.OK).body(new byte[0])); // Suggested fix
关于spring - MockRestServiceServer 和 java.lang.IllegalArgumentException : No InputStream specified,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19684297/
我是一名优秀的程序员,十分优秀!