I'm trying to mock a file upload (using Vertx) with Mockito but it throws a NullPointerError
on getParam("type")
used in when(event.request().getParam("type")).thenReturn("application/octet-stream");
My unit test is as follows:
private UploadResultatenHandler uploadResultatenHandler;
@Mock
RoutingContext event;
File folder;
Set<io.vertx.ext.web.FileUpload> upload;
HttpServerRequest httpServerRequest;
HttpServerResponse httpServerResponse;
@Before
public void setUp() {
uploadResultatenHandler = new UploadResultatenHandler();
MockitoAnnotations.initMocks(this);
when(event.request().getParam("type")).thenReturn("application/octet-stream");
when(event.fileUploads()).thenReturn(upload);
when(httpServerResponse.setStatusCode(200)).thenReturn(httpServerResponse);
when(event.response()).thenReturn(httpServerResponse);
}
@Test
public void testCleanHandler() {
uploadResultatenHandler.handle(event);
verify(event).response();
}
"uploadResultatenHandler.handle(event);" :
@Override
public void handle(RoutingContext event) {
String newFileName = event.request().getParam("type");
Set<FileUpload> uploads = event.fileUploads();
System.out.println(uploads);
for (FileUpload fileUpload : uploads) {
String oldFileName = fileUpload.uploadedFileName();
char[] inhoud = FileReader.readFileUTF8ToString(oldFileName).toCharArray();
String fileExtention = setFileExtension(fileUpload.contentType());
FileCreater.createFile(newFileName, fileExtention, "file-uploads/", inhoud);
FileDeleter.deleteFile(oldFileName);
}
event.response().setStatusCode(200).end("Check");
}
private String setFileExtension(String type) {
switch (type) {
case "text/xml":
return ".xml";
case "application/octet-stream":
return ".json";
default:
return ".txt";
}
}
I think it might have something to do with the mocked RoutingContext not having this parameter, but I don't know how to fix / bypass this error?
I changed the line to when(event.request()).thenReturn(request); when(request.getParam("type")).thenReturn("application/octet-stream"); and the mocks to: Mock(answer = Answers.RETURNS_DEEP_STUBS) RoutingContext event; @Mock File folder; Set upload; HttpServerRequest request; HttpServerResponse response; but I'm still getting a NullPointer
Stack trace:
java.lang.NullPointerException at nl.icaprojecten.TestIntegratieQuintor.ServiceLayer.rest.UploadResultatenHandlerTest.setUp(UploadResultatenHandlerTest.java:42) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
when(event.request()).thenReturn(httpServerRequest);
statement in your setup method? – JonKevent.request()
should return. If you haven't set that up, it will be returning anull
. – JonK