简体   繁体   English

模拟对象 null 用于 spring 服务 class 单元测试

[英]mock objects null for spring service class unit test

I have Spring Service class with multiple constructor:我有 Spring 服务 class 与多个构造函数:

@Service("async")
public class AsyncProcessor extends Processor {


    private final JobRunner JobRunner;

    private final ExecutorService executorService;

    @Autowired
    public AsyncProcessor(final MyRepo MyRepo,
                              final JobRunner JobRunner) {
        this(MyRepo, JobRunner, 1, 1, 60);
    }

    public AsyncProcessor(final MyRepo MyRepo,
                              final JobRunner JobRunner,
                              @Value("${processor.corePoolSize:1}") final int corePoolSize,
                              @Value("${processor.maxPoolSize:1}") final int maxPoolSize,
                              @Value("${processor.keepAliveTime:60}") final int keepAliveTime) {
        super(MyRepo);
        this.JobRunner = JobRunner;
        this.executorService = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, SECONDS, new LinkedBlockingQueue<>());
    }
    
}

I am using Junit 5 to create a service test using mocked dependencies but tripping up with an error which I am not able to solve:我正在使用 Junit 5 使用模拟依赖项创建服务测试,但出现我无法解决的错误:

here is my test class:这是我的测试 class:

@ExtendWith(MockitoExtension.class)
public class MyTest {

    @Mock
    private MyRepo repo;

    @Mock
    private JobRunner jobRunner;

    @Mock
    private ExecutorService executorServiceMock;

    @InjectMocks
    private AsyncProcessor processor = new AsyncProcessor(repo, jobRunner);

    @Test
    public void shouldSubmitTaskOnExecutor() {

    }
    
}

I am getting null on the mocked repo and job runner fields and don't know why.我在模拟的 repo 和 job runner 字段上得到 null 并且不知道为什么。 The issue started when I added the second constructor with the additional spring property params.当我使用附加的 spring 属性参数添加第二个构造函数时,问题就开始了。

Any ideas?有任何想法吗?

I think the new keyword is messing things up.我认为新的关键字把事情搞砸了。 Create a Spring @Configuration class which has a @Bean annotated method that returns the async processor generated from the newly added constructor.创建一个 Spring @Configuration class ,它有一个 @Bean 注释方法,该方法返回从新添加的构造函数生成的异步处理器。 Your test can then autowire it, without the constructor call.然后,您的测试可以自动装配它,而无需调用构造函数。

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

相关问题 Spring Integration Java DSL单元测试-如何模拟Service Activator类或其他组件/端点? - Spring Integration Java DSL unit test - How to mock a Service Activator class or other components/endpoints? 使用空字段自动连线的Spring单元测试对象 - Spring unit test objects autowired with null fields Spring - 使用 Mock 进行单元测试 - 如何在服务单元测试中模拟自定义收集器 - Spring - Unit Test with Mock - How to mock a Custom Collector in a Service unit test model 映射器模拟在 Z2A2D595E6ED9A0B24F027F2B63B134D 引导单元中返回 null object - model mapper mock returns null object in spring boot unit test Spring 引导单元测试 Junit 5 为什么模拟返回 null - Spring boot unit test by Junit 5 why mock return null 如何在单元测试中模拟服务 - How to mock service in unit test Spring数据存储库未在测试用例类中注入@Mock返回null? - Spring data repository not injecting with @Mock in test case class returns null? 通过模拟服务进行弹簧控制器测试 - spring controller test by mock service 自动连线字段的模拟在单元测试中为空 - mock of autowired field is null in unit test 单元测试:在父测试类中为被测单元生成模拟 - Unit test : Produce mock for the unit under test in a parent test class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM