Java Spring启动测试-字段注入引发NullPointerException

Java Spring启动测试-字段注入引发NullPointerException,java,spring,spring-mvc,junit4,Java,Spring,Spring Mvc,Junit4,我正在为SpringBoot中的rest应用程序编写测试 但在我的测试中,CategoryController中的接口CategoryService在运行测试和引发NullPointerException时无法自动连接 类别服务是在字段级别注入的。 以下是我的课程: 类别控制器 @RestController @RequestMapping("/v1/categories") public class CategoryController { @Autowired privat

我正在为SpringBoot中的rest应用程序编写测试

但在我的测试中,CategoryController中的接口CategoryService在运行测试和引发NullPointerException时无法自动连接

类别服务是在字段级别注入的。

以下是我的课程:

类别控制器

@RestController
@RequestMapping("/v1/categories")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @RequestMapping
    public List<Category> getAll() {
        return categoryService.findAll();
    }
}
类别控制器测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes =  {AppTestConfig.class})
public class CategoryControllerTest {

    @Autowired
    private CategoryRepository categoryRepository;

    @Test
    public void testGetAll() throws Exception {
        CategoryController controller = new CategoryController();
        List<Category> categories = controller.getAll();
        assertEquals(categoryRepository.findAll().size(), categories.size());
    }
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(类={AppTestConfig.class})
公共类类别控制器测试{
@自动连线
私人分类报告分类报告;
@试验
public void testGetAll()引发异常{
CategoryController控制器=新CategoryController();
列表类别=controller.getAll();
assertEquals(categoryRepository.findAll().size(),categories.size());
}
}
使用构造函数注入,一切都很好


有什么问题吗?

不看整个项目很难知道,但请尝试添加更多注释:-):

  • AppTestConfig上的
    @ComponentScan
    -确保所有bean都可用
  • @SpringApplicationConfiguration
    而不是
    @ContextConfiguration
    ——我认为springboot总是这样
  • CategoryController测试上的
    @WebAppConfiguration
    -如果您使用的是Spring MVC,Servlet可能需要Web应用上下文

  • 如果看不到整个项目,很难知道,但请尝试添加更多注释:-):

  • AppTestConfig上的
    @ComponentScan
    -确保所有bean都可用
  • @SpringApplicationConfiguration
    而不是
    @ContextConfiguration
    ——我认为springboot总是这样
  • CategoryController测试上的
    @WebAppConfiguration
    -如果您使用的是Spring MVC,Servlet可能需要Web应用上下文

  • 您正在使用
    new
    关键字构造
    CategoryController
    。这样,spring就不知道这个对象,因此它不能向其中注入任何东西

    bean应该通过spring实例化,就像
    CategorService
    一样,可以在spring
    配置
    类中设置被测试的对象

    如果spring创建bean,它将自动关联相关属性

    @Autowired
    private CategoryController underTest;
    

    您正在使用
    new
    关键字构造
    CategoryController
    。这样,spring就不知道这个对象,因此它不能向其中注入任何东西

    bean应该通过spring实例化,就像
    CategorService
    一样,可以在spring
    配置
    类中设置被测试的对象

    如果spring创建bean,它将自动关联相关属性

    @Autowired
    private CategoryController underTest;
    

    谢谢你们的回答,@burna的建议帮助了我。谢谢你们的回答,@burna的建议帮助了我。
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes =  {AppTestConfig.class})
    public class CategoryControllerTest {
    
        @Autowired
        private CategoryRepository categoryRepository;
    
        @Test
        public void testGetAll() throws Exception {
            CategoryController controller = new CategoryController();
            List<Category> categories = controller.getAll();
            assertEquals(categoryRepository.findAll().size(), categories.size());
        }
    }
    
    @Autowired
    private CategoryController underTest;