Hi I have the following code to test:
@RequestMapping(value = "/displayPages", method = RequestMethod.GET)
public ModelAndView errorPage(ModelMap model, HttpServletRequest request) {
String token = (request != null) ? request.getParameter("tok") : "";
boolean requestP = ESAPI.validator().isValidInput("Request Param", tok, "HTTPParameterValue", 1, false);
if (requestP || token.contains(msg.getMessage("techErr.tok", new Object[]{}, Constants.LOCAL))) {
return new ModelAndView("dispError");
} else {
return new ModelAndView("login");
}
}
Part of my unit test class is shown below:
@RunWith(PowerMockRunner.class)
@PrepareForTest({ESAPI.class})
public class ApplicationConTest {
@InjectMocks
private ApplicationCont appController;
private MockMvc mockMvc;
....
@Before
public void setup() {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpSession session = new MockHttpSession();
request.setSession(session);
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
this.mockMvc = MockMvcBuilders.standaloneSetup(appController).build();
}
@Test
public void errorPages_ExpectLogout() throws Exception {
String returnVal = "test";
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getParameter("tok")).thenReturn("dummuy value");
when(msg.getMessage("techErr.tok",new Object[]{},Constants.LOCAL)).thenReturn(returnVal);
//msg.getMessage("techErr.tok", new Object[]{}, Constants.LOCAL)
mockMvc.perform(get("/errorPages"))
.andExpect(view().name("logout")
);
}
When I execute the code the String tok is null and i get a null pointer exception. However as showed above I am returning a dummy value, any advice why tok is null?
Thanks in advance for any help.
Aucun commentaire:
Enregistrer un commentaire