![](/img/trans.png)
[英]Writing a JUnit test for a “Step” scoped bean - No scope registered for scope name “step” (Spring Batch 3.0.10)
[英]JUnit test for a Step scoped Bean using StepScopeTestExecutionListener.class : Still getting “No Scope registered”
我正在尝试为“步骤”范围的 bean 编写一个独立的单元测试用例。 我之前发布了这个问题,并了解我需要使用StepScopeTestExecutionListener
为我的单元测试创建一个步骤 scope; 但是,即使在使用StepScopeTestExecutionListener
之后,我仍然会收到以下异常:
Caused by: java.lang.IllegalStateException: No Scope registered for scope name 'step'
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:343)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
我的联合
@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 的步骤 scope 仍未初始化?
注意:在不相关的说明中,我在getStepExecution
JobExecution
不是使用MetaDataInstanceFactory.createJobExecution
的原因是因为某些奇怪的原因,编译器能够找到createJobExecution
方法,但在运行时找不到该方法。 I used the -verbose:class
flag and can confirm that there is only one jar from where the MetaDataInstanceFactory
class gets loaded and that jar contains the required method as well. 我假设手动创建JobExecution
实例不应该是我的问题的根本原因。
该错误甚至在加载您的测试之前发生,因为您的common-context.xml
文件中没有定义步骤 scope bean。 由于您没有在该文件中使用批处理命名空间,因此您需要手动声明步骤 scope。 这是文档(3.0.10)的摘录:
Because it is not part of the Spring container by default,
the scope must be added explicitly, either by using the batch namespace
or by including a bean definition explicitly for the StepScope (but not both)
将以下内容添加到您的应用程序上下文中应该可以解决问题:
<bean class="org.springframework.batch.core.scope.StepScope">
<property name="proxyTargetClass" value="true" />
</bean>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.