2013-10-30 39 views
2

我想在客戶端(在服務器端調用Spring MVC)測試我的REST服務。MockRestServiceServer&java.lang.IllegalArgumentException:沒有指定InputStream

我可以在服務器端使用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的指定

任何幫助將非常感激,因爲我有支出的方式來多少時間就這項

回答

5

請試試這個:

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 
相關問題