を設定されていません。すべての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の代わりに使用します。
あなたは 'MediaType.APPLICATION_JSON_UTF8_VALUE'と' .contentType(APPLICATION_JSON_UTF8) 'を使用しています。あなたは同じものを使うべきではありませんか? –
実際、それは...私がオプションで遊んでいた間に定義された定数でした。私はコードを貼り付けたときにそれを元に戻すのを忘れてしまった。私は上記を更新します。 –