@TestExecutionListeners({ StepScopeTestExecutionListener.class,DependencyInjectionTestExecutionListener.class })
@RunWith(SpringJUnit4ClassRunner.class)
@PropertySource("classpath:properties/common.properties")
@ContextConfiguration(locations = { "/spring/common-context.xml" })
public class ConfigDAOImplTest {
@Autowired
private ConfigDAOImpl configDAO;
@Spy
private ContextParamDAO contextParamDAO = new ContextParamDAOImpl();
private static final String SCHEMA_CONFIG = "classpath:data/CONFIG_SCHEMA.sql";
private static final String DATA_CONFIG = "classpath:data/CONFIG_DATA.sql";
@Before
public void init() {
MockitoAnnotations.initMocks(this);
DataSource dataSource = new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript(SCHEMA_CONFIG)
.addScript(DATA_CONFIG)
.build();
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
//override the jdbcTemplate for the test case
configDAO.setJdbcTemplate(jdbcTemplate);
configDAO.setContextParamDAO(contextParamDAO);
}
public StepExecution getStepExecution() {
JobParametersBuilder jobParametersBuilder = new JobParametersBuilder();
jobParametersBuilder.addString("test", "test");
JobParameters jobParameters = jobParametersBuilder.toJobParameters();
JobInstance jobInstance = new JobInstance(12345L,"testJob");
JobExecution jobExecution = new JobExecution(jobInstance,jobParameters);
StepExecution execution =
MetaDataInstanceFactory.createStepExecution(jobExecution,"step",11245L);
execution.getExecutionContext().putString("input.data", "foo,bar,spam");
return execution;
}
}
[我可以确定在调试模式下使用此方法的断点运行单元测试时,getStepExecution
确实得到了调用。
问题:为什么仍未为我的JUnit初始化步骤范围?
注:
与此无关,我之所以在JobExecution
中手动创建getStepExecution
而不是使用MetaDataInstanceFactory.createJobExecution
是因为出于某些奇怪的原因,编译器能够找到createJobExecution
]方法,但在运行时找不到该方法。我使用了-verbose:class
标志,可以确认从MetaDataInstanceFactory
类的加载位置只有一个jar,并且该jar也包含所需的方法。我假设手动创建JobExecution
实例不应成为问题的根本原因。我正在尝试为“ step”范围的bean编写一个独立的单元测试用例。我早些时候发布了这个问题,并且了解到我需要使用StepScopeTestExecutionListener来创建步骤作用域...
<bean class="org.springframework.batch.core.scope.StepScope">
<property name="proxyTargetClass" value="true" />
</bean>