【问题标题】:Testing spring batch job stepScope测试spring批处理作业stepScope
【发布时间】:2018-05-22 13:47:12
【问题描述】:

我正在尝试测试一个执行读取(从另一个应用程序获取数据)过程(简单计算)和写入(到 mongodb)的 spring 批处理作业

读者是@StepScope

这里是读取任务的 postConstruct。

 @PostConstruct
    public void init(){
        employees.addAll(getListOfEmployeesBy(affectationMotifService.findAllRegistrationNumbers()));
    }

    public List<EmployeeForSalaryDTO> getListOfEmployeesBy(List<String> registrationNumbers){
        LOG.debug("request to get all the employees by registration numbers {}" , registrationNumbers);
        return coreResourceFeign.getAllEmployeesForSalaryByRegistrationNumbers(registrationNumbers).getBody();
    }

当我尝试启动作业测试或应用程序中的任何测试时。 spring 总是运行读取任务的 init() .. 这将导致测试失败,因为我需要模拟 coreResourceFeign.getAllEmployeesForSalaryByRegistrationNumbers(registrationNumbers) 。 我无法模拟该方法,因为它在测试开始之前运行。

这里是测试

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {SalaryApp.class, SecurityBeanOverrideConfiguration.class})
public class SalaryJobServiceTest {
    @Autowired
    @InjectMocks
    private SalaryJobService salaryJobService;

    @Test
    public void startJob() throws Exception {
        SalaryJobDTO SalaryJobDTO = salaryJobService.start(Collections.emptyList());
        Assert.assertNotNull(salaryJobDTO.getId());
    }
}

我不知道如何处理春季批量测试。欢迎任何建议或帮助。

【问题讨论】:

    标签: java spring spring-boot junit spring-batch


    【解决方案1】:

    @PostConstruct 将确保在创建对象后立即调用您的方法。 Spring 应用程序在应用程序启动时根据配置创建所有 bean。这是预期的行为。如果您不想在应用程序启动期间调用您的方法,请删除 @PostConstruct 并且您可以运行您的测试来模拟依赖对象。

    您应该使用 reader read 方法将数据加载到 reader。

    【讨论】:

      猜你喜欢
      • 2016-03-04
      • 1970-01-01
      • 1970-01-01
      • 2012-10-19
      • 1970-01-01
      • 2014-08-10
      • 2020-08-29
      • 2015-03-04
      • 2019-07-26
      相关资源
      最近更新 更多