0
私はSpringBoot Controllerのユニットテストをしようとしています。目標ヘッダーがAccept
を含む場合、application/json
以外のものとして406 Not Acceptable
を返すことをテストすることです。しかし、私はMockMvc
で動作しないことがわかります。私は406の代わりに200 OKを取得しています。任意のアイデアをお願いします!もちろん、サービスは、私がPostmanまたは任意の残りのクライアントを使用してテストすると、期待通りに406を返します。問題テストSpring MockMvcのリクエストヘッダを受け入れる
@Test
public void shouldRejectIfInvalidAcceptHeaderIsPassed() throws Exception {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Accept","application/xml");
httpHeaders.add("Authorization", "some jwt token");
this.mockMvc.perform(post("/sample/test")
.contentType(MediaType.APPLICATION_JSON)
.headers(httpHeaders)
.content(toJson("")))
.andExpect(status().isNotAcceptable());
// Then
verify(mockSampleService).getSampleOutput();
}
そして、私のコントローラ
@RestController
@Validated
@RequestMapping(PATH_PREFIX)
public class SampleController {
public static final String PATH_PREFIX = “/sample”;
private final SampleService sampleService;
@Autowired
public SampleController(SampleService sampleService) {
sampleService = sampleService;
}
@RequestMapping(value = “/test”, method = RequestMethod.POST))
public SampleResponse createSession() {
return sampleService.getSampleOutput();
}
}
コントローラの外観はどうですか? – pvpkiran
今質問のコントローラを見つけてください。 – deejo