I am writing junit test for my request method. Im using mockMvc to trigger the rest request. When I run my test I get the error
java.lang.NullPointerException: Cannot invoke "org.springframework.test.web.servlet.ResultActions.andExpect(org.springframework.test.web.servlet.ResultMatcher)" because the return value of "org.springframework.test.web.servlet.MockMvc.perform(org.springframework.test.web.servlet.RequestBuilder)" is null
Here is my request method definition
@RequestMapping(value = "data/upload", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA)
@CrossOrigin
@Loggable (activityname = ActivityLogConstants.CONFIG_DATA_UPLOAD, operationName =ActivityLogConstants.UPLOAD_OPERATIONNAME)
public Response importConfigData(HttpServletRequest request, @RequestParam("file") MultipartFile inFile, @RequestParam("rowData") String rowData) throws ServiceException {
....
}
I have HttpServletRequest,Multipartfile and String arguments to this method.
Here is my test
@ExtendWith(SpringExtension.class)
public class ConfigUploadTest {
@MockBean
private MockMvc mockMvc;
@InjectMocks
private UploadController uploadController;
@Test
public void vnfmService_ConfigUploadShouldReturnStatusOkTest() throws Exception {
MockMultipartFile file = new MockMultipartFile(
"file", // The request parameter name
"test.xml", // The original filename (with .xml extension)
"application/xml", // The content type for XML
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><data>This is some XML content</data>".getBytes());
MockMultipartHttpServletRequestBuilder request = MockMvcRequestBuilders.multipart("/data/upload");
request.file("file", file.getBytes());
request.param("rowData","{"test":"test"}");
mockMvc.perform(request).andExpect(status().isOk());
}
}
I don't want to load my entire application context and hence I'm using @ExtendWith(SpringExtension.class)
Please advise.