运行Mockito测试用例时获取空指针异常

时间:2021-03-12 16:15:10

标签: java rest junit mockito

我试图为控制器类中的休息 API 编写一个模拟测试用例(spring boot java rest)。我收到了空指针异常,提前致谢。

测试控制器

@RestController
@RequestMapping("/heloo")
public class TestController {

    @GetMapping
    public String HellooWorld()
    {
        return "hello world";
    }
}

JunitTestCase 类

@SpringBootTest
@RunWith(SpringRunner.class)
class EmployeeApiTest {

    private MockMvc mockmvc; 
    @Autowired
    WebApplicationContext context;
    @InjectMocks
    private TestController testcontroller;
    @InjectMocks
    private EmployeeApi employeeapi;
    @Before
    public void setup()
    {
        mockmvc=MockMvcBuilders.standaloneSetup(testcontroller).build();
        //mockmvc = MockMvcBuilders.webAppContextSetup(context).build();
    }

    @Test
    void getMockitotest() throws Exception {
        mockmvc.perform(MockMvcRequestBuilders.get("/get"))
        .andExpect(MockMvcResultMatchers.status().isOk())
        .andExpect(MockMvcResultMatchers.content().string("hello world"));
        
    }

}

error obtained below

0 个答案:

没有答案
相关问题