Unit test Spring and PowerMock - Getting null pointer in request.getParameter value - java

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.

Related

Spring Boot Rest template Mocking : ResponseEntity.getBody() throws a Null pointer exception

I am new to JUnit and Mockito. Here I am trying to mock the rest template call and return a response entity. But it throws me a Null pointer Exception. I am not able to figure out what's wrong with the mocking. Can anyone guide me where I am doing wrong?
JUnit5 Unit test cases
class MyTest {
#InjectMocks
private MyService service;
#Mock
private RestTemplate template;
#BeforeEach
void setUp(){
MockitoAnnotations.initMocks(this);
}
private ResponseEntity<String> generateResponse(String body, HttpStatus http) {
return new ResponseEntity<>(body, http);
}
#Test
public void publishEventsuccessResponseTest() {
when(template.postForEntity(ArgumentMatchers.anyString(), ArgumentMatchers.any(),
ArgumentMatchers.<Class<String>>any())).thenReturn(generateResponse("uuid",
HttpStatus.OK));
String result = service.sentData("data");
Assertions.assertTrue("uuid".equals(result));
}
}
Service
class MyService {
public String sentData(String data) {
String jsonResp = "";
ResponseEntity<String> response;
try {
HttpEntity<String> requestBody = new HttpEntity<String>(data, headers);
response = restTemplate.postForEntity("url", requestBody, String.class);
} catch (Exception ex) {}
System.out.println(response); //value : Null
if (response.getStatusCodeValue() == 200) { // Null pointer exception
jsonResp = response.getBody();
}
return jsonResp;
}
}
Change the #BeforeEach to #Before of jUnit4 and add public void setUp()
Your test should be green!

Not able to call the controller method using MockMvc

I am not able to get how to call the controller method(API) in this case. How to send the MultipartFile as request parameter and how to pass the HttpServletRequest so that mockMvc can call the actual method for testing.
#RunWith(SpringRunner.class)
public class PartnerSiteLoadControllerTest {
private MockMvc mockMvc;
#Mock
private PartnerSiteUploadService partnerSiteUploadService;
#InjectMocks
private PartnerSiteLoadController partnerSiteLoadController;
#Before
public void setup() {
this.mockMvc = MockMvcBuilders.standaloneSetup(partnerSiteLoadController)
.setControllerAdvice(new PartnerExceptionHandlerMapper()).build();
}
#Test
public void uploadSitesInBulk()throws Exception {
String userId = "userId";
HttpServletRequest request = mock(HttpServletRequest.class);
UserPrincipal userPrincipal = new UserPrincipal();
userPrincipal.setId("id");
BulkUploadResponseDTO bulkUploadResponseDTO = new BulkUploadResponseDTO();
FileInputStream inputFile = new FileInputStream( "src/test/resources/PartnerSites_2019-09-04_v1.xlsx");
MockMultipartFile file = new MockMultipartFile("PartnerSites_2019-09-04_v1.xlsx", "PartnerSites_2019-09-04_v1.xlsx", "multipart/form-data", inputFile);
when(file.getOriginalFilename()).thenReturn("PartnerSites_2019-09-04_v1.xlsx");
when(request.getAttribute("userPrincipal")).thenReturn(userPrincipal);
when(partnerSiteUploadService.uploadSitesInBulk(userId,file)).thenReturn(bulkUploadResponseDTO);
mockMvc.perform(MockMvcRequestBuilders.fileUpload("/v4/slm/partners/sites/import")
.file(file).contentType(MediaType.MULTIPART_FORM_DATA).requestAttr("userPrincipal",userPrincipal))
.andExpect(status().isOk());
verify(partnerSiteUploadService,times(1)).uploadSitesInBulk(userId,file);
verifyNoMoreInteractions(partnerSiteUploadService);
}
}
Controller class method
#RestController
#RequestMapping("/v4/slm/partners/sites/import")
#Api(value = "Site Bulk Upload Service")
#Slf4j
#Validated
public class PartnerSiteLoadController {
private PartnerSiteUploadService partnerSiteUploadService;
#Autowired
public PartnerSiteLoadController(PartnerSiteUploadService partnerSiteUploadService) {
this.partnerSiteUploadService = partnerSiteUploadService;
}
#PostMapping(value = "", headers = ("content-type=multipart/*"))
#ApiOperation(value = "Import sites in bulk")
public ResponseEntity<BulkUploadResponseDTO> uploadSitesInBulk(#RequestParam("file") MultipartFile excelFile, HttpServletRequest request){
UserPrincipal userPrincipal = (UserPrincipal) request.getAttribute("userPrincipal");
String userId = userPrincipal.getId();
log.info("Received excel file with name {}......",excelFile.getOriginalFilename());
if(!excelFile.isEmpty()){
return ResponseEntity.status(HttpStatus.CREATED).body(partnerSiteUploadService.uploadSitesInBulk(userId,excelFile));
}
else{
throw new BadRequestException("Received empty excel file");
}
}
}
while executing the test I am getting the 400 error code. the mockmvc is not calling the original API.

Spring controller test fails despite url working

I am trying to test a basic controller:
#Autowired
DAOInterface db;
#RequestMapping(value = "/postdb", method = RequestMethod.GET)
#ResponseBody
public String postdb(
#RequestParam(value = "id", required = true) String id
) {
db.addEntry(id);
return "Added " + id + ".";
}
This url works as when I access it, it adds it to a db and I get the string output as a response.
I am trying to create a simple unit test for it:
#Autowired
MockMvc mockMvc;
#MockBean
DAOInterface daoInterface;
#Test
public void shouldReturnA200() throws Exception {
mockMvc.perform(get("/postdb?id=3"))
.andExpect(status().isOk());
}
But instead I get the following
MockHttpServletRequest:
HTTP Method = GET
Request URI = /postdb
Parameters = {id=[3]}
Headers = {}
Handler:
Type = org.springframework.web.servlet.resource.ResourceHttpRequestHandler
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 404
Error message = null
Headers = {}
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []
java.lang.AssertionError: Status
Expected :200
Actual :404
Not sure why I it's working whenever I try and access it but fails when running this test. I don't see any issues. Might it be because I'm not using any headers or a formal response body/view and rather just outputting a String?
EDIT = It works when I add
.contextPath("/postdb")).. not sure if that's the correct way but when I write another test and dont include any request paramters, that test gives a 200 instead of a 400 or 404....
#Autowired
DAOInterface db;
#RequestMapping(value = "/postdb", method = RequestMethod.GET)
public ResponseEntity<String> postdb(#RequestParam(required = true) String id) {
db.addEntry(id);
return new ResponseEntity<>("Added " + id + ".", HttpStatus.OK);
}
Test:
#Test
public void shouldReturnA200() throws Exception {
mockMvc.perform(get("/postdb?id=3")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
Below is working fine for me
public class FirstWebController {
#RequestMapping(value = "/postdb", method = RequestMethod.GET)
#ResponseBody
public String postdb(#RequestParam(value = "id", required = true) String id) {
System.out.println("idddddddddddd "+id);
return "Added " + id + ".";
}
}
Test class is
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration
public class FirstWebControllerTest {
#Configuration
static class FirstWebControllerTestConfiguration {
#Bean
public FirstWebController firstWebController() {
return new FirstWebController();
}
}
#Autowired
private FirstWebController firstWebController;
private MockMvc mockMvc;
#Before
public void setup() {
mockMvc = standaloneSetup(firstWebController).build();
}
#Test
public void shouldReturnA200() throws Exception {
mockMvc.perform(get("/postdb?id=3")).andExpect(status().isOk());
}
}
Try adding query parameter as below:
#Test
public void shouldReturnA200() throws Exception {
mockMvc.perform(get("/postdb).param("id", "3"))
.andExpect(status().isOk());
}

Spring MVC: How to bind to nested object properties in the #ModelAttribute

I have a unit test to carry out based on the following part of code:
#RequestMapping(value = "/changePass", method = RequestMethod.POST)
public ModelAndView changePass(#ModelAttribute(TAPPLICATION) AppBean applicationBean, BindingResult result, ModelMap model, Principal principal, HttpServletRequest request) throws NSException, SQLException {
// ...
if (applicationBean != null
&& applicationBean.getChangePassDto() != null
&& StringUtils.isNotEmpty(applicationBean.getChangePassDto().getNewPassword())) {
String newPassword = applicationBean.getChangePassDto().getNewPassword();
// ...
}
// ...
The AppBean contains the following getter and setter:
private ChangePassDto changePassDto;
public ChangePassDto getChangePassDto() {
return changePassDto;
}
public void setChangePassDto(ChangePasswordDto changePassDto) {
this.changePassDto = changePassDto;
}
Basically when I execute the unit test the method applicationBean.getChangePassDto() is null but applicationBean is not null. How can I initialise the applicationBean.getChangePassDto() so that it does not return null? I have initialised the other non object parameters with the .param method as it can be seen in my unit test.
I am also using Powermock as unit test framework.
Please find below part of my unit test:
#Before
public void setup() {
request = new MockHttpServletRequest();
request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap());
response = new MockHttpServletResponse();
session = new MockHttpSession();
request.setSession(session);
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
//Added viewResolver to prevent circular view path error
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/jsp/");
viewResolver.setSuffix(".jsp");
this.mockMvc = MockMvcBuilders.standaloneSetup(appController).setViewResolvers(viewResolver).build();
}
#Test
public void changePass_ExpectC() throws Exception {
PowerMockito.doNothing().when(passwordVal).validate(any(User.class), anyListOf(Params.class), any(Object.class),any(Errors.class));
mockMvc.perform(post("/changePass").param("userLogName", "JOHN").param("userLogged", "userLogged").param("password", "password123").param("newPassword", "newPassword123").param("confirmNewPassword", "newPassword123"))
.andExpect(view().name(Constants.DENIED))
.andExpect(status().isOk()
);
}
Any idea how I can intitialise applicationBean.getchangePassDto() so that it is not null?
Thanks in advance for help.
Simply create a new instance of ChangePassDto in your AppBean:
public class AppBean {
private ChangePassDto changePassDto = new ChangePassDto();
public ChangePassDto getChangePassDto() {
return changePassDto;
}
public void setChangePassDto(ChangePasswordDto changePassDto) {
this.changePassDto = changePassDto;
}
// ...
}
You then need to use the full path to the properties in the nested DTO like this:
mockMvc.perform(post("/changePass")
.param("changePassDto.userLogName", "JOHN")
.param("changePassDto.userLogged", "userLogged")
.param("changePassDto.password", "password123")
.param("changePassDto.newPassword", "newPassword123")
.param("changePassDto.confirmNewPassword", "newPassword123"))
.andExpect(view().name(Constants.DENIED))
.andExpect(status().isOk());

Spring Testing - java.lang.IllegalArgumentException: Not enough variable values available to expand

I am writing Unit Tests for the below REST Controller which takes a UserID and grants a List of Authorities to that user.
#RestController
#RequestMapping("/user")
#Api(value = "User", description = "User API")
public class UserController{
// some code
#RequestMapping(method = RequestMethod.POST, value = "/{userId}/grantAuthz")
#ApiOperation(value = "GrantAuthz", notes = "Grant Authorization")
public Collection<UserEntity.UserAuthz> grantAuthz(#PathVariable("userId") String userId,
#RequestBody ArrayList<String> authorities) {
UserEntity userEntity = userRepository.findOne(userId);
if(userEntity == null) {
//TODO: throw and send resource not found
return null;
}
log.debug("Authorities to be granted to user " + userId + " are : " + authorities);
for(String authz : authorities) {
log.debug("Adding Authorization " + authz);
userEntity.addUserAuthz(authz);
}
userRepository.save(userEntity);
return userEntity.getAuthorities();
}
}
I wrote the below Unit Test for the UserController
#RunWith(SpringJUnit4ClassRunner.class)
#SpringApplicationConfiguration(classes = Application.class)
#WebAppConfiguration
public class UserControllerTest {
private final Log log = LogFactory.getLog(getClass());
private MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(),
MediaType.APPLICATION_JSON.getSubtype(),
Charset.forName("utf8"));
private MockMvc mockMvc;
private HttpMessageConverter mappingJackson2HttpMessageConverter;
private final String USER_URL = "/{userId}/grantAuthz";
private final String USER_ID = "111";
private final String USER_NAME = "MockUser";
#Autowired
private WebApplicationContext webApplicationContext;
#Autowired
private UserRepository userRepository;
private String createdToken = null;
#Autowired
void setConverters(HttpMessageConverter<?>[] converters) {
this.mappingJackson2HttpMessageConverter = Arrays.asList(converters).stream().filter(
hmc -> hmc instanceof MappingJackson2HttpMessageConverter).findAny().get();
Assert.assertNotNull("the JSON message converter must not be null",
this.mappingJackson2HttpMessageConverter);
}
#Before
public void setup() throws Exception {
this.mockMvc = webAppContextSetup(webApplicationContext).build();
}
#Test
public void testGrantAuthorizationForUser() throws Exception{
Optional<UserEntity> userEntityAuthz = userRepository.findOneByUsername(USER_NAME);
Set<String> expectedAuthzList = (LinkedHashSet)userEntityAuthz.get().getAuthorizations();
List<String> grantList = new ArrayList<>();
grantList.add("ABC");
grantList.add("DEF");
grantList.add("GHI");
grantList.add("JKL");
grantList.add("MNO");
grantList.add("PQR");
grantList.add("STU");
grantList.add("VWX");
grantList.add("YZA");
JSONObject json = new JSONObject();
json.put("grantList",grantList);
MvcResult grantAuthzResult = mockMvc.perform(MockMvcRequestBuilders.post(USER_URL)
.contentType(contentType)
.param("userId",USER_ID)
.param("authorities",json.toString()))
.andExpect(status().isOk())
.andDo(print())
.andReturn();
}
}
When executed, my test is throwing an Illegal Argument Exception:
"Not enough variable values available to expand 'userId'"
I am sending the required URL Parameters using the .param() method in the test, what am I doing wrong ? I reffered this possible duplicate question but did not find it much useful. Using RestTemplate in Spring. Exception- Not enough variables available to expand
I found out what I am doing wrong, using param() method is not the right way here as I have #PathVariable and #RequestBody in my Controller Methods as the parameters.
public Collection<UserEntity.UserAuthz> grantAuthz(#PathVariable("userId") String userId,
#RequestBody ArrayList<String> authorities) {
So I passed the #PathVariable in the post() method of the test.
MockMvcRequestBuilders.post(USER_URL,USER_ID)
As the required type is #RequestBody ArrayList<String> instead of using the JSONObject I used JSONArrayand used the content() method to send the JSONArray as the string.
Here are the changes I have made to the Test Method.
#Test
public void testGrantAuthorizationForUser() throws Exception{
Optional<UserEntity> userEntityAuthz = userRepository.findOneByUsername(USER_NAME);
Set<String> expectedAuthzList = (LinkedHashSet)userEntityAuthz.get().getAuthorizations();
List<String> grantList = new ArrayList<>();
grantList.add("ABC");
grantList.add("DEF");
grantList.add("GHI");
grantList.add("JKL");
grantList.add("MNO");
grantList.add("PQR");
grantList.add("STU");
grantList.add("VWX");
grantList.add("YZA");
JSONArray json = new JSONArray();
MvcResult grantAuthzResult = mockMvc.perform(MockMvcRequestBuilders.post(USER_URL,USER_ID)
.contentType(contentType)
.content(json.toString()))
.andExpect(status().isOk())
.andDo(print())
.andReturn();
}
#Test
public void getOneContactAPI() throws Exception {
String id = "8";
mvc.perform(MockMvcRequestBuilders.get("/api/contact/{id}",id).accept(MediaType.APPLICATION_JSON))
.andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("id").exists());
}

Categories