使用 Mockito 模拟发布请求会在"when(event.request().getParam("类型上抛出 NullPointerException ")).thenReturn("应用程序/八



我正在尝试使用 Mockito 模拟文件上传(使用 Vertx),但它对when(event.request().getParam("type")).thenReturn("application/octet-stream");中使用的getParam("type")抛出NullPointerError

我的单元测试如下:

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";
}
}

我认为这可能与没有此参数的模拟路由上下文有关,但我不知道如何修复/绕过此错误?

我将行更改为 when(event.request()).thenReturn(request); when(request.getParam("type")).thenReturn("application/oct et-stream"); 和模拟为:模拟(答案 = Answers.RETURNS_DEEP_STUBS) 路由上下文事件; @Mock 文件夹;设置上传;HttpServerRequest request;HttpServerResponse response;但我仍然得到一个空指针

堆栈跟踪:

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(未知来源) 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.statement.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)

由于您没有记录任何行为event.request(),它将返回null。当然,调用getParam会失败,并NullPointerException.有几种方法可以解决它,但恕我直言,最优雅的是使用深存根:

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

模拟不能递归地工作。

您必须拆分此行

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

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

相关内容

  • 没有找到相关文章

最新更新