junit测试service时一直报空指针异常

用@Autowired注解如下:

@Autowired
private CategoryServiceImpl categoryService;

然后如下的方法一直报:空指针异常

@Test
public void findOne() {
    ProductCategory one = categoryService.findOne(1);
    Assert.assertEquals(new Integer(1),one.getCategoryId());
}

调了好久发现

原因:引入的包错了,

importorg.junit.jupiter.api.Test;

要引入如下的包才行

importorg.junit.Test;

生成测试类的时候要选择junit4,不能选择junit5

 


版权声明:本文为latin_06原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。