Junit测试案例:Java.lang.NullPoInterException



我正在尝试使用 JUnitmockito遵循控制器代码的测试用例。但是我得到了无效的指针例外。我是Junit测试的新手。有人可以告诉我在这里做错了什么吗?

departiveController

   @RestController
    @RequestMapping("/api.spacestudy.com/SpaceStudy")
    public class DepartmentController {
        @Autowired
        DepartmentService depService;
        @CrossOrigin(origins = "http://localhost:4200")
        @GetMapping("/LoadDept")
        public  Department findDeptId() {
            return  depService.findDeptId();
        }
    }

depaermentControllerTest

 @WebMvcTest
    public class DepartmentControllerTest {
        @Autowired
        private MockMvc mockMvc;
       @Mock
       public DepartmentService depService;  
       @InjectMocks
       DepartmentController departmentController;
       @Before
        public void setup() throws Exception    {
            mockMvc = MockMvcBuilders.standaloneSetup(departmentController).build(); 
        }

       @Test
       public void findDeptIdTest() throws Exception
       {
           Department department = new Department();
           department.setnDeptId(10);
           Mockito.when(depService.findDeptId()).thenReturn(department);
        mockMvc.perform(get("/api.spacestudy.com/SpaceStudy/LoadDept").accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.nDeptId", Matchers.is(10)));         
       }
    }

测试结果

java.lang.NullPointerException
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.detectHandlerMethods(AbstractHandlerMethodMapping.java:227)
    at org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder$StaticRequestMappingHandlerMapping.registerHandlers(StandaloneMockMvcBuilder.java:490)
    at org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder.registerMvcSingletons(StandaloneMockMvcBuilder.java:356)
    at org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder.initWebAppContext(StandaloneMockMvcBuilder.java:341)
    at org.springframework.test.web.servlet.setup.AbstractMockMvcBuilder.build(AbstractMockMvcBuilder.java:139)
    at com.spacestudy.DepartmentControllerTest.setup(DepartmentControllerTest.java:38)
    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.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:538)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)

要解决该问题,您必须在standalonesetup((上给予dectionsController类的实例。在进行一些单元测试时,我遇到了同样的问题,此后,我的问题消失了。尝试这样做:

@Before
public void setup() throws Exception    {
      mockMvc = MockMvcBuilders.standaloneSetup(new DepartmentController()).build(); 
}

相关内容

  • 没有找到相关文章

最新更新