简体   繁体   English

当 class 注入限定符时,如何在单元测试中使用不同的实现

[英]How to use different implementation in unit test when class injects with Qualifier

I have a spring bean component which I want to unit test for example:我有一个 spring bean 组件,我想对其进行单元测试,例如:

@Component
public class UploadRequestTasklet implements Tasklet {

    private final Uploader fileUploader;

    public UploadRequestTasklet(@Qualifier("mainUploader") final Uploader fileUploader) {
        this.fileUploader = fileUploader;
    }

    @Override
    public RepeatStatus execute(final StepContribution stepContribution,
                                final ChunkContext chunkContext) throws IOException, InterruptedException {
        
        Info result = fileUploader.remoteUpload(context, info);
      
        return RepeatStatus.FINISHED;
    }
}

I have multiple different implementation of Uploader interface.我有多个不同的Uploader接口实现。 For production, I use the mainUploader which uploads files to remote server.对于生产,我使用将文件上传到远程服务器的mainUploader For integration and unit test, I want to use the localFileUploader implementation.对于集成和单元测试,我想使用localFileUploader实现。 The uploader classes live outside of my project and I have imported these classes from another project using maven dependency.上传器类位于我的项目之外,我已经使用 maven 依赖项从另一个项目中导入了这些类。

My question is, how can I override the mainuploader and in unit test, use the localFileUploader version?我的问题是,如何覆盖 mainuploader 并在单元测试中使用 localFileUploader 版本?

the class UploadRequestTasklet has Qualifier annotation specifying to use main implementation which I cannot seem to override in unit test. class UploadRequestTasklet 有限定符注释指定使用我似乎无法在单元测试中覆盖的主要实现。

You can use @Profile together with your interface Tasklet implementations, for example您可以将@Profile 与您的接口 Tasklet 实现一起使用,例如

    @Component
    @Profile("local")
    public class UploadRequestTasklet implements Tasklet {
    ...
}

for a run that set jvm args -Dspring.profiles.active=local if you want to run test by maven or gradle.对于设置 jvm args -Dspring.profiles.active=local的运行,如果您想通过 maven 或 gradle 运行测试。 But, if you want to do it by IDE you can set @ActiveProfiles("local") with your component但是,如果您想通过 IDE 执行此操作,您可以使用您的组件设置@ActiveProfiles("local")

see more here在这里查看更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用限定词来确定类的字段? - How to use qualifier to decide the fields of a class? Spring 引导 - 如何对 @Service class 方法在使用 JWT 和自定义声明时使用 @PreAuthorize 进行单元测试 - Spring Boot - How to unit test @Service class methods that use @PreAuthorize when using JWT with custom claims 使用@Qualifier @Autowired值测试类 - Test class with @Qualifier @Autowired value @Qualifier 在单元测试中不起作用,而在构造函数中注入 - @Qualifier not working in unit test, while injecting in constructor 如何获得注入某些东西的类的类名 - how to get class name of class that injects something 如何在Hibernate中使用@Qualifier - How to use @Qualifier in Hibernate 如何使用 dagger 限定符注解来提供不同的 OkHttpClient 构建器? - How to use dagger qualifier annotations to provide different OkHttpClient builders? 如何在 android 多风味项目中使用相同的 class 但不同的实现 - How to use same class but different implementation in android multi flavor project Spring Qualifier,应用程序启动时的服务实现 - Spring Qualifier, service implementation when application starts 如何使用Java CDI在非默认类上使用限定符 - How to use qualifier on non default class with java cdi
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM