objectMapper.writeValueAsString提供空指针异常

时间:2019-11-01 11:32:10

标签: junit slf4j

我正在尝试为下面的控制器编写测试用例,它的工作正常,但是问题是当我运行单元测试用例时,它在(LOGGER.info(“ ========= >请求是------>“ + objectMapper.writeValueAsString(basBaaisnEvcIdRequest));

请问有什么可以帮助我解决这个问题

控制器

   @RequestMapping(value = "/api/baaisn/queryevcid", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
        @ResponseBody
        public BaaisnEvcIdResponse getQueryEvcid(@RequestBody BaaisnEvcIdRequest basBaaisnEvcIdRequest)
                throws IOException, DAOException, SQLException, HttpServletException,JsonProcessingException {

            LOGGER.info("========>request is------>" + objectMapper.writeValueAsString(basBaaisnEvcIdRequest));

            if (basBaaisnEvcIdRequest.getLata() != 0 && (basBaaisnEvcIdRequest.getProduct_type() != null
                    && !basBaaisnEvcIdRequest.getProduct_type().equalsIgnoreCase(""))) {
                BaaisnEvcIdResponse baaisnEvcIdResponse = BaaisnEvcIdMSService.getQueryEvcidService(basBaaisnEvcIdRequest);
                return baaisnEvcIdResponse;
            } else {
                BaaisnEvcIdResponse basiBaaisnEvcIdResponse = new BaaisnEvcIdResponse();
                basiBaaisnEvcIdResponse.setStatus_code(0);
                if (basBaaisnEvcIdRequest.getProduct_type() == null || basBaaisnEvcIdRequest.getProduct_type().equals("")) {
                    basiBaaisnEvcIdResponse.setStatus_desc("Please send Product type");
                } else if (basBaaisnEvcIdRequest.getLata() == 0) {
                    basiBaaisnEvcIdResponse.setStatus_desc("Please send Lata");
                }
                return basiBaaisnEvcIdResponse;
            }
        }

测试类

  @InjectMocks
        BaaisnEvcIdMSController baaisnEvcIdMSController;

        @Mock
        BaaisnEvcIdMSService baaisnEvcIdMSService;

        @Before
        public void setup() {
            MockitoAnnotations.initMocks(this);
        }

        @Test
        public void getQueryEvcid() throws 
        IOException, DAOException, SQLException, HttpServletException,JsonProcessingException{

            //Request object
            BaaisnEvcIdRequest baaisnEvcIdRequest = mock(BaaisnEvcIdRequest.class);
            Mockito.when(baaisnEvcIdRequest.getLata()).thenReturn(620);
            Mockito.when(baaisnEvcIdRequest.getProduct_type()).thenReturn("LEVC");
            Mockito.when(baaisnEvcIdRequest.getSvc_type()).thenReturn("EMS");

            //Response object
            BaaisnEvcIdResponse basiBaaisnEvcIdResponse = mock(BaaisnEvcIdResponse.class);
            Mockito.when(basiBaaisnEvcIdResponse.getEvc_id()).thenReturn("/TLSM/100020218/001");
            Mockito.when(basiBaaisnEvcIdResponse.getStatus_code()).thenReturn(1);
            Mockito.when(basiBaaisnEvcIdResponse.getStatus_desc()).thenReturn("Success");


            Mockito.when(baaisnEvcIdMSService.getQueryEvcidService(Mockito.any()))
                    .thenReturn(basiBaaisnEvcIdResponse);

            BaaisnEvcIdResponse mockResponse =  baaisnEvcIdMSController.getQueryEvcid(baaisnEvcIdRequest);

            assertEquals("Success", mockResponse.getStatus_desc());
            assertEquals("/TLSM/100020218/001", mockResponse.getEvc_id());

        }

例外

java.lang.NullPointerException
    at com.verizon.uts.ms.cdm.baaisn.evcid.controller.BaaisnEvcIdMSController.getQueryEvcid(BaaisnEvcIdMSController.java:44)
    at com.verizon.uts.ms.cdm.baaisn.evcid.controller.BaaisnEvcIdMSControllerTest.getQueryEvcid(BaaisnEvcIdMSControllerTest.java:59)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    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.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    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.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
    at org.junit.vintage.engine.execution.RunnerExecutor.execute(RunnerExecutor.java:40)
    at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    at java.util.Iterator.forEachRemaining(Iterator.java:116)
    at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
    at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
    at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
    at org.junit.vintage.engine.VintageTestEngine.executeAllChildren(VintageTestEngine.java:80)
    at org.junit.vintage.engine.VintageTestEngine.execute(VintageTestEngine.java:71)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:229)
    at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:197)
    at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:211)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:191)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:137)
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

0 个答案:

没有答案