简体   繁体   中英

Mocking a post request with Mockito throws NullPointerException on “when(event.request().getParam(”type“)).thenReturn(”application/octet-stream“);”

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/oct‌​et-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.runne rs.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)

Since you haven't recorded any behavior for event.request() , it will return null . Calling getParam on it will, of course, fail with a NullPointerException . There are a few ways around it, but the most elegant, IMHO, would be to use deep stubbing :

@Mock(answer = Answers.RETURNS_DEEP_STUBS)
RoutingContext event;

mocking does not work recursively.

you must split this line

 when(event.request().getParam("type")).thenReturn("application/octet-stream"); 

to this

Request request = mock(Request.class);
when(event.request()).thenReturn(request);
when(request.getParam("type")).thenReturn("application/octet-stream");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM