2016-10-26 9 views
0

を設定されていません。すべてのGET要求の単体テストは完全に動作しています。しかし、POST要求はすべてここコンテンツタイプは、私は春ブーツRESTアプリケーションを持っているJUnitのREST POSTテスト

java.lang.AssertionError: Content type not set 

を返すされ、コントローラである:

@RestController 
public class ClassificationController { 

private IClassificationService classificationService; 

@Autowired 
public ClassificationController(IClassificationService classificationService) { 
    this.classificationService = classificationService; 
} 

@RequestMapping(value="/category", method = RequestMethod.POST, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}) 
@ResponseStatus(HttpStatus.CREATED) 
@ResponseBody 
public CategoryDTO createCategory(@RequestBody final CategoryDTO category) throws MctException { 
    return classificationService.createCategory(category); 
} 

私はユニットテストがある:

@RunWith(MockitoJUnitRunner.class) 
public class ClassificationControllerTest { 

@Mock 
private IClassificationService classificationService; 

@Before 
public void setUp() { 
    mockMvc = MockMvcBuilders.standaloneSetup(new ClassificationController(classificationService)).build(); 
} 

@Test 
public void createCategoryTest() throws Exception { 
    String jsonTask = String.format("{\"id\": \"2\",\"categoryName\": \"Category Name 2\"}"); 
    MvcResult result = mockMvc.perform(post("/category") 
      .contentType(MediaType.APPLICATION_JSON_UTF8) 
      .content(jsonTask)) 
      .andDo(MockMvcResultHandlers.print()) 
      .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) 
      .andExpect(content().string(containsString("\"id\":2"))) 
      .andExpect(content().string(containsString("\"categoryName\":\"Category Name 2\""))) 
      .andExpect(status().isCreated()) 
      .andReturn(); 
} 

IはCategoryDTOオブジェクトにこれをまた試みています同じ結果を持つString jsonTaskの代わりに使用します。

+0

あなたは 'MediaType.APPLICATION_JSON_UTF8_VALUE'と' .contentType(APPLICATION_JSON_UTF8) 'を使用しています。あなたは同じものを使うべきではありませんか? –

+0

実際、それは...私がオプションで遊んでいた間に定義された定数でした。私はコードを貼り付けたときにそれを元に戻すのを忘れてしまった。私は上記を更新します。 –

答えて

1

私はそれが最初のものだったので、そのアサーションで失敗していることを発見しましたが、エンドポイントから何も戻ってこなかっただけです。私は、コンテンツタイプが有効であるように、挿入されているオブジェクトを返すので、コンテンツタイプを返しています。私はテストを変更してObjectMapperを使用してコンテンツJSONを作成した後、ドメインオブジェクトにequalsメソッドを追加する必要がありました....一度equalsメソッドを追加すると、テストは合格しました。私はモックフレームワークがその方法を使っていることを知らなかった。

@Test 
public void createClassTest() throws Exception { 
    String jsonInString = objectMapper.writeValueAsString(singleClass); 
    when(classificationService.createClass(5, singleClass)).thenReturn(singleClass); 
    MvcResult result = mockMvc.perform(post("/class/5") 
      .contentType(MediaType.APPLICATION_JSON_UTF8) 
      .content(jsonInString)) 
      .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) 
      .andExpect(content().string(containsString("\"id\":1"))) 
      .andExpect(content().string(containsString("\"className\":\"Test Class Name 1\""))) 
      .andExpect(status().isCreated()) 
      .andReturn(); 
    verify(classificationService).createClass(5, singleClass); 
} 
0

アサーションエラーで判断すると、エンドポイントがMediaType.APPLICATION_JSON_UTF8を返さないようです。 contentTypeチェックまたはデバッグを削除して、エンドポイントが実際に戻っているものを確認してください。もう一度、あなたが見ているエラーで判断すると、コンテンツタイプがまったく返されていないようです。したがって、コンテンツタイプが設定されていないことを確認する必要があります。

私は通常、私は通常の試験では、すべてのcontentTypeを返さないPOSTリクエストを知っています。

は結局のところ、それはあなたがコンテンツタイプが設定されるように期待していた場合、そのエンドポイントは、実際に間違って何かをやっている可能性があります。

関連する問題