您好我是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()
);
}
提前感谢任何建议。
答案 0 :(得分:0)
尽管最初的问题是在几年前报告的,但我遇到了同样的问题-在MockMvc.perform()上出现NullPointerException
经过大量调试后,我发现为验证请求而编写的自定义JWT过滤器中存在Null Pointer问题,该问题在perform()方法本身上被报告为NullPointerException。
一旦修复了过滤器以避免出现空指针异常,一切就开始起作用。