简体   繁体   English

用Mockito模拟发布请求会在“ when(event.request()。getParam(“ type”))。thenReturn(“ application / octet-stream”);”上引发NullPointerException

[英]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"); 我正在尝试使用Mockito模拟文件上传(使用Vertx),但它会在when(event.request().getParam("type")).thenReturn("application/octet-stream"); getParam("type")使用的getParam("type")上引发NullPointerError 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);" “ 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? 我认为这可能与模拟的RoutingContext没有此参数有关,但我不知道如何解决/绕过此错误?

I changed the line to when(event.request()).thenReturn(request); 我将行更改为when(event.request())。thenReturn(request); when(request.getParam("type")).thenReturn("application/oct‌​et-stream"); when(request.getParam(“ type”))。thenReturn(“ application / octetetstream”); and the mocks to: Mock(answer = Answers.RETURNS_DEEP_STUBS) RoutingContext event; 以及模拟到:Mock(answer = Answers.RETURNS_DEEP_STUBS)RoutingContext事件; @Mock File folder; @Mock文件文件夹; Set upload; 设置上传; HttpServerRequest request; HttpServerRequest请求; HttpServerResponse response; HttpServerResponse响应; but I'm still getting a NullPointer 但我仍然得到一个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 nl.icaprojecten.TestIntegratieQuintor.ServiceLayer.rest.UploadResultatenHandlerTest.setUp(UploadResultatenHandlerTest.java:42)处的java.lang.NullPointerException在org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50)在sun.reflect.DelegatingMethodAccessorImpl.invoke(未知源)在java.lang.reflect.Method.invoke(未知源)在org.junit org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)上的.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)org.junit.internal.runners.statements.RunBefores.evaluate (RunBefores.java:24)在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)在org.junit.runners.BlockJUnit4ClassRunner。 org.junit.runne上的runChild(BlockJUnit4ClassRunner.java:57) 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) org.junit.runners.rs.ParentRunner $ 3.run(ParentRunner.java:290)org org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71) org.junit.runners.ParentRunner.access.000(ParentRunner.java:58)在org.junit.runners.ParentRunner.run(ParentRunner.java:363)上的.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58) ),位于org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)的org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)处。 org.eclipse.jdt上的eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)位于org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)。 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)上的internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)

Since you haven't recorded any behavior for event.request() , it will return null . 由于您尚未记录event.request()任何行为,因此它将返回null Calling getParam on it will, of course, fail with a NullPointerException . 当然,对其调用getParam将会失败,并出现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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 spring controller 如何处理应用程序/八位字节流请求? - How can a spring controller deal with application/octet-stream request? Junit mockito when(..)。thenReturn()抛出NullPointerException - Junit mockito when(..).thenReturn() throws NullPointerException Mockito InvalidUseOfMatchersException 当 mocking 发布请求时 - Mockito InvalidUseOfMatchersException when mocking a post request 具有应用程序/八位字节流的Spring Boot multipartFile引发异常 - spring boot multipartFile with application/octet-stream throws exception 如何使用RestAssured将“application / octet-stream”内容类型发布到RestAPI - How to post “application/octet-stream” content type to RestAPI using RestAssured Mockito-模拟服务时抛出nullpointerException - Mockito - throws nullpointerException when mocking a service 如何将 RestTemplate 与 application/octet-stream 响应类型一起使用 - How to use RestTemplate with application/octet-stream response type HTTPUrlConnection和应用程序/八位字节流内容类型响应处理 - HTTPUrlConnection and application/octet-stream content-type response handling 为什么我的控制器发送内容类型为“ application / octet-stream”? - Why is my controller sending the content type “application/octet-stream”? 找不到媒体类型= application / octet-stream的MessageBodyReader - MessageBodyReader not found for media type=application/octet-stream
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM