[英]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.