コントローラレイヤのユニットテストケースを作成しています。私はSpring SecurityContextHolderからユーザを取得しているところのコールを持っています。テストケースを実行すると、Springのセキュリティコンテキストを模倣する方法がわからないため、NULLポインタ例外が発生します。MockMVCを使用したMock SpringContextHolder
以下は私のコードです、どのようにそれを行うには?
コントローラーMethhod:
@RequestMapping(method = RequestMethod.POST)
public void saveSettings(@RequestBody EmailSettingDTO emailSetting) {
User user = ((CurrentUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUser();
settings.saveUserEmailSetting(user, emailSetting);
}
私のテストケース:
@Test public void testSaveSettings() throws Exception {
mockMvc.perform(post(BASE_URL).content(this.objectMapper.writeValueAsString(emailDto))
.contentType(MediaTypes.HAL_JSON)).andExpect(status().isOk());
}
http://stackoverflow.com/questions/15203485/spring-test-security-how-to-mock-authenticationをご覧ください – VinayVeluri