![](/img/trans.png)
[英]MockMvc and Spring Security - Null FilterChainProxy
[英]MockMvc Spring null Pointer
嗨,我是Spring測試框架的新手,我有以下代碼Spring MVC Controller來測試:
public ModelAndView viewChangePassword(ModelMap model, Principal principal, HttpServletRequest request) throws NSException, SQLException {
System.err.println("testing");
String name = principal.getName();
.....
}
請在控制器中找到以下字段和initBinder方法定義:
@Autowired
private MessageSource msg;
@Autowired
private ApplicationMa applicationMa;
@Autowired
private SesVal sesVal;
@Autowired
private CommonManager commonManager;
@Autowired
private ApplicationListManager applicationListManager;
@Autowired
private QueryManager queryManager;
@Autowired
private Manager manager;
@Autowired
private FormBlankValidator formValidator;
@Autowired
private FormDataValidator dataValidator;
@InitBinder
protected void initBinder(HttpServletRequest request, WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat(Constants.DD_M_MYYYY);
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
這是我的單元測試,我也在使用Mockito:
@RunWith(MockitoJUnitRunner.class)
public class ApplicationControllerTest {
@InjectMocks
private ApplicationController appController;
@Mock
Principal principal;
@Mock
private RequestAttributes requestAttributes;
@Mock
private SessionValidator sessionValidator;
@Mock
User user;
private MockMvc mockMvc;
@Before
public void setup() {
// Process mock annotations
//MockitoAnnotations.initMocks(this);
// Setup Spring test in standalone mode
RequestContextHolder.setRequestAttributes(requestAttributes);
this.mockMvc = MockMvcBuilders.standaloneSetup(appController).build();
}
@Test
public void viewChangePassword() throws Exception{
MockHttpServletRequest request = new MockHttpServletRequest();
when(principal.getName()).thenReturn("user1");
when(sessionValidator.isSessionValid(user)).thenReturn(true);
mockMvc.perform(
get("/viewChangePassword"))
.andExpect(view().name("denied")
);
}
當我運行單元測試時,會顯示以下空指針異常:
Running com.controller.application.test.ApplicationControllerTest
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
testing
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 1.063 sec <<< FAILURE! - in com.controller.application.test.ApplicationControllerTest
viewChangePassword(com.controller.application.test.ApplicationControllerTest) Time elapsed: 0.688 sec <<< ERROR!
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
at com.controller.application.test.ApplicationControllerTest.viewChangePassword(ApplicationControllerTest.java:117)
Caused by: java.lang.NullPointerException
at com.controller.application.test.ApplicationControllerTest.viewChangePassword(ApplicationControllerTest.java:117)
Results :
Tests in error:
ApplicationControllerTest.viewChangePassword:117 » NestedServlet Request proce...
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
Total time: 4.423 s
這是第117行:
mockMvc.perform(
我不知道什么是null這里有任何想法如何解決這個問題?
只是在同一個類中指出這個方法工作正常,沒有問題:
@Test
public void defaultPageTestOK() throws Exception{
mockMvc.perform(get("/"))
.andExpect(redirectedUrl("/view"))
.andExpect(status().isFound()
);
}
提前感謝任何建議。
雖然幾年前報告了原始問題,但我遇到了同樣的問題 - mockMvc.perform()上的NullPointerException
經過大量的調試后,我發現我編寫的用於驗證我的請求的自定義JWT過濾器中有一個Null Pointer問題,它在perform()方法本身被報告為NullPointerException。
一旦我修復我的過濾器以避免空指針異常,一切都開始工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.