Spring 启动测试 - 字段注入引发 NullPointerException
Spring Boot Test - field injection raises NullPointerException
我正在 spring 引导中为 rest 应用程序编写测试。
但在我的测试中,CategoryController 中的接口 CategoryService 在 运行 测试和 NullPointerException 引发时无法自动装配。
CategoryService 在字段级别注入。
这是我的 类:
类别控制器
@RestController
@RequestMapping("/v1/categories")
public class CategoryController {
@Autowired
private CategoryService categoryService;
@RequestMapping
public List<Category> getAll() {
return categoryService.findAll();
}
}
类别服务
@Service
public class CategoryServiceImpl implements CategoryService {
@Autowired
CategoryRepository categoryRepository;
@Override
public List<Category> findAll() {
return categoryRepository.findAll();
}
}
CategoryRepositoryImpl
@Repository
public class CategoryRepositoryImpl implements CategoryRepository {
@Override
public List<Category> findAll() {
...
}
}
----- 测试 ------
AppTestConfig
@Configuration
public class AppTestConfig {
@Bean
public CategoryRepository categoryRepository() {
return new TestCategoryRepository();
}
@Bean
public CategoryService categoryService() {
return new App.CategoryServiceImpl();
}
}
类别控制器测试
@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());
}
}
使用构造函数注入,一切正常。
有什么问题?
没有看到整个项目很难知道,但请尝试添加更多注释:-) :
@ComponentScan
AppTestConfig - 确保所有 bean 都可用
@SpringApplicationConfiguration
而不是 @ContextConfiguration
- 我认为 Spring Boot 总是这样
@WebAppConfiguration
on CategoryControllerTest - 如果您使用 Spring MVC,则 servlet 可能需要 Web App 上下文。
您正在使用 new
关键字构建 CategoryController
。这样,spring不知道这个对象,因此它不能向它注入任何东西。
bean 应该由 spring 实例化,就像 CategorService
一样,被测对象可以在 spring Configuration
class 中设置.
如果 spring 创建 bean,它将自动装配相关属性。
@Autowired
private CategoryController underTest;
我正在 spring 引导中为 rest 应用程序编写测试。
但在我的测试中,CategoryController 中的接口 CategoryService 在 运行 测试和 NullPointerException 引发时无法自动装配。
CategoryService 在字段级别注入。
这是我的 类:
类别控制器
@RestController
@RequestMapping("/v1/categories")
public class CategoryController {
@Autowired
private CategoryService categoryService;
@RequestMapping
public List<Category> getAll() {
return categoryService.findAll();
}
}
类别服务
@Service
public class CategoryServiceImpl implements CategoryService {
@Autowired
CategoryRepository categoryRepository;
@Override
public List<Category> findAll() {
return categoryRepository.findAll();
}
}
CategoryRepositoryImpl
@Repository
public class CategoryRepositoryImpl implements CategoryRepository {
@Override
public List<Category> findAll() {
...
}
}
----- 测试 ------
AppTestConfig
@Configuration
public class AppTestConfig {
@Bean
public CategoryRepository categoryRepository() {
return new TestCategoryRepository();
}
@Bean
public CategoryService categoryService() {
return new App.CategoryServiceImpl();
}
}
类别控制器测试
@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());
}
}
使用构造函数注入,一切正常。
有什么问题?
没有看到整个项目很难知道,但请尝试添加更多注释:-) :
@ComponentScan
AppTestConfig - 确保所有 bean 都可用@SpringApplicationConfiguration
而不是@ContextConfiguration
- 我认为 Spring Boot 总是这样
@WebAppConfiguration
on CategoryControllerTest - 如果您使用 Spring MVC,则 servlet 可能需要 Web App 上下文。
您正在使用 new
关键字构建 CategoryController
。这样,spring不知道这个对象,因此它不能向它注入任何东西。
bean 应该由 spring 实例化,就像 CategorService
一样,被测对象可以在 spring Configuration
class 中设置.
如果 spring 创建 bean,它将自动装配相关属性。
@Autowired
private CategoryController underTest;