Spring Batch:使用Mock而不是@JobScope bean进行测试

时间:2017-03-27 13:45:13

标签: spring unit-testing spring-batch

如何用@JobScope模拟替换Mockito'

我正在尝试以下方法:

@RunWith(SpringRunner.class)
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, JobScopeTestExecutionListener.class})
public class MyBrokenTest {

  @Autowired
  public ValueObject myJobScopedBean;

  @Test
  public void doStuff() throws Exception {
    myJobScopedBean.setVaue(new TestObject());
    JobExecution jobExecution = jobLauncherTestUtils.launchStep("myCoolTasklet", getJobExecution().getExecutionContext());
  }
}

但没有成功。在步骤内,myJobScopedBean的新值未初始化。据我所知,JobScopeTestExecutionListener并不打算与jobLauncherTestUtils一起使用,这是正确的吗?如何使用模拟依赖项测试我的步骤?

UPD。

配置也很简单:

@Configuration
@EnableBatchProcessing
public class MyJobConfig {
  /* ... Job definition ... */

  @Bean
  @JobScope
  public Tasklet getMyCoolTasklet(MyJobScopedBean myJobScopedBean) {
    return new MyCoolTasklet(myJobScopedBean);
  }

  @Bean
  @JobScope
  public MyJobScopedBean myJobScopedBean() {
    return new MyJobScopedBean();
  }

}

0 个答案:

没有答案