我正在开发一个基于spring-boot的项目,这对我来说是相当新的。目前,我使用@WebApplicationContext注解来运行任何JUnit测试,因为我似乎无法以其他方式启动应用程序。我的目标是要么得到一个明确的答案来避免使用它,要么获得链接到使用适用概念的项目。
我的确切目标是:我想要一个测试配置,它不会为了测试更小的服务和子类集而加载整个Web应用程序。
例如: 我目前有一系列3个装配器。其中一个是父对象的,另外两个是与子对象相关的。
我的确切目标是:我想要一个测试配置,它不会为了测试更小的服务和子类集而加载整个Web应用程序。
例如: 我目前有一系列3个装配器。其中一个是父对象的,另外两个是与子对象相关的。
@Component
public class ReportResponseAssembler {
@Autowired
private ParameterResponseAssembler parameterResponseAssembler;
@Autowired
private TimeRangeResponseAssembler timeRangeResponseAssembler;
public ReportResponseAssembler makeResponse() {
return new ReportResponseAssembler();
}
}
为了测试目的,我希望只加载这三个类,并使它们适当地将依赖项注入父级。像这样的:
public class ReportResponseAssemblerTest {
@Autowired
ReportInstanceResponseAssembler reportResponseAssembler;
@Test
public void testPlaceHolder() {
Assert.assertNotNull(reportResponseAssembler);
}
}
我尝试了以下的做法:
@EnableAutoConfiguration
@ComponentScan(basePackages = { "com.blahpackage.service.assembler" })
@Configuration
public class TestContextConfiguration {}
将此内容提供给SpringApplicationConfiguration,但即使进行了扫描,它仍无法检测到适用于自动注入的Bean。也许我需要在配置中直接标识它们为@Bean并返回新实例?是否有其他好的方法?如果您有示例项目或解释的链接,那就太好了。
感谢任何回复此帖的人,感谢您花费宝贵的时间。
ReportResponseAssembler
,在第二个代码片段中是ReportInstanceResponseAssembler
。这是打字错误吗? - a better oliver