Java MockMvc-Spring空指针
您好,我是Spring测试框架的新手,我需要测试以下Spring MVC控制器代码:Java MockMvc-Spring空指针,java,spring,spring-mvc,spring-test,Java,Spring,Spring Mvc,Spring Test,您好,我是Spring测试框架的新手,我需要测试以下Spring MVC控制器代码: public ModelAndView viewChangePassword(ModelMap model, Principal principal, HttpServletRequest request) throws NSException, SQLException { System.err.println("testing"); String name = princip
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
我不确定这里什么是空的,你知道怎么解决吗
只需指出,在同一类中,此方法运行良好,没有问题:
@Test
public void defaultPageTestOK() throws Exception{
mockMvc.perform(get("/"))
.andExpect(redirectedUrl("/view"))
.andExpect(status().isFound()
);
}
提前感谢您的建议。尽管最初的问题在几年前就已经报告过了,但我遇到了相同的问题-mockMvc.perform()上的NullPointerException 经过大量调试后,我发现我为验证请求而编写的自定义JWT筛选器中存在空指针问题,在perform()方法本身上被报告为NullPointerException
一旦我修复了过滤器以避免空指针异常,一切都开始工作。是正确的
注释之前的:org.junit.Before
而不是org.aspectj.lang.annotation.Before
(或其他一些)?是的,它是org.junit.Before您检查过这里吗?检查了它,但在添加标题后仍然出现相同的错误ApplicationController有哪些字段和构造函数?
@Test
public void defaultPageTestOK() throws Exception{
mockMvc.perform(get("/"))
.andExpect(redirectedUrl("/view"))
.andExpect(status().isFound()
);
}