小编kun*_*ike的帖子

如何使用SpringApplicationConfiguration或ContextConfiguration加载较小的应用程序部分

我正在开发一个基于spring-boot的项目,这对我来说还是一个新手.目前,我正在使用@WebApplicationContext注释来运行我的任何Junit测试,因为我似乎无法让应用程序以任何其他方式启动.我提出这个问题的目的是要么得到一个明确的答案,如何避免它的使用,或者链接到项目使用适用的概念.

我的确切目标是:我希望有一个测试配置,它不会加载整个Web应用程序,以便测试较小的服务和子类集.

示例:我目前有一系列3个汇编程序.一个用于父对象,另外两个用于与子相关的对象

@Component
public class ReportResponseAssembler {

    @Autowired
    private ParameterResponseAssembler parameterResponseAssembler;

    @Autowired
    private TimeRangeResponseAssembler timeRangeResponseAssembler;

    public ReportResponseAssembler makeResponse() {
        return new ReportResponseAssembler();
    }
}
Run Code Online (Sandbox Code Playgroud)

出于测试目的,我想加载这3个类并让它们适当地将依赖项注入到父类中.就像是:

public class ReportResponseAssemblerTest {

    @Autowired
    ReportInstanceResponseAssembler reportResponseAssembler;

    @Test
    public void testPlaceHolder() {
        Assert.assertNotNull(reportResponseAssembler);
    }
}
Run Code Online (Sandbox Code Playgroud)

我尝试过以下方面的事情:

@EnableAutoConfiguration
@ComponentScan(basePackages = { "com.blahpackage.service.assembler" })
@Configuration
public class TestContextConfiguration {}
Run Code Online (Sandbox Code Playgroud)

然后将其提供给SpringApplicationConfiguration,但即使使用扫描,它也不会检测适用于自动注入的bean.也许我需要在配置中直接将它们表示为@Beans并返回新实例?还有其他好方法吗?您拥有的示例项目或解释的任何链接都将非常出色.

谢谢,任何回复的人,非常适合你的时间.

java spring spring-mvc spring-boot

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

java ×1

spring ×1

spring-boot ×1

spring-mvc ×1