2016-12-05 4 views
0

HTTPレスポンスコードが200になるようにするために400を取得しています。エンドポイントにbyte []オブジェクトを渡していますが、コンテンツを追加していないようですタイプ正しく?助言がありますか?@RequestBodyでエンドポイントをテストするユニット

@RequestMapping(value = "/test", method = RequestMethod.POST, consumes = "application/octet-stream") 
     public ResponseEntity<String> receiveCompressedBinary(@RequestHeader String site, @RequestHeader String customer, 
      @RequestHeader String table, @RequestBody byte[] binary, @RequestHeader String loadStatus) { 
     if(binary.length < maxFileSize) { 
      return new ResponseEntity<String>(HttpStatus.OK); 
     } 
     else{ 
      return new ResponseEntity<String>(HttpStatus.PAYLOAD_TOO_LARGE); 
     } 
     } 

私のテスト:

@Test 
    public void testUploadCompressedBinaryInitialRunning() throws Exception{ 
    File file = new File("src/test/resources/testFile.txt"); 
    String site = "site"; 
    String customer = "customer"; 
    String table = "table"; 
    String loadStatus = "INITIALRUNNING"; 
    this.mockMvc.perform(post("/test").header("site",site).param("customer",customer). 
     param("table", table).content(compress(file)).param("loadStatus",loadStatus) 
     .with(user("user"))).andExpect(status().isOk()); 
    this.mockMvc.perform(post("/uploadCompressedBinary")).andDo(print()).andExpect(status().isOk()); 
    } 

圧縮方法:

public static byte[] compress(File file) throws IOException { 
    if (file.length() == 0) { 
     return null; 
    } 
    FileInputStream fileInputStream = null; 
    byte[] fileInBytes = new byte[(int)file.length()]; 
    try { 
     //convert file into array of bytes 
     fileInputStream = new FileInputStream(file); 
     fileInputStream.read(fileInBytes); 
     fileInputStream.close(); 
    } catch (IOException e) { 
     System.out.println("Exception whilst compressing the file: " + e.getMessage()); 
    } 

    ByteArrayOutputStream obj = new ByteArrayOutputStream(); 
    GZIPOutputStream gzip = new GZIPOutputStream(obj); 
    gzip.write(fileInBytes); 
    gzip.close(); 
    return obj.toByteArray(); 
    } 

UPDATE:それは過去のガット、むしろ.PARAMよりも、私が使用する必要がありますそれがない

答えて

0

.headerあなたが投稿しているときにコンテンツタイプを設定しているように見えません。

this.mockMvc.perform(post("/test").header("site",site).param("customer",customer). 
     param("table", table).content(compress(file)).contentType("application/octet-stream").param("loadStatus",loadStatus) 
     .with(user("user"))).andExpect(status().isOk()); 
をお試しください
関連する問題