测试弹簧批处理作业stepScope

时间:2017-12-08 10:20:26

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

我试图测试执行读取(从另一个应用程序获取数据)进程(简单计算)并写入(进入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());
    }
}

我不知道如何处理弹簧批量测试。任何建议或帮助都将受到欢迎。

1 个答案:

答案 0 :(得分:0)

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

相反,您应该使用读者读取方法将数据加载到读者。

相关问题