Java 为“编写JUnit测试”;“步骤”;作用域bean-没有为“作用域名称”注册作用域;“步骤”;(弹簧批次3.0.10)
我想为Java 为“编写JUnit测试”;“步骤”;作用域bean-没有为“作用域名称”注册作用域;“步骤”;(弹簧批次3.0.10),java,spring,junit,spring-batch,spring-test,Java,Spring,Junit,Spring Batch,Spring Test,我想为Springmanagedbean编写一个JUnit测试用例,其作用域为“步骤”。这个bean由一个Spring批处理Tasklet进行仲裁 configDAO ConfigDAImpl类的Bean定义 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframew
Spring
managedbean编写一个JUnit测试用例,其作用域为“步骤”。这个bean由一个Spring批处理Tasklet进行仲裁
configDAO ConfigDAImpl类的Bean定义
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- bean has been defined with a scope of "step" as it uses the stepExecutionContext -->
<bean id="configDAO"
class="com.myproject.common.dataaccess.impl.ConfigDAOImpl" scope="step">
<property name="jdbcTemplate" ref="jdbcTemplate" />
<property name="corePoolSize" value="${threadpool.size}"/>
<property name="frequency" value="#{stepExecutionContext['frequency']}" />
</bean>
</beans>
当我运行上述测试类时,它失败,出现以下异常:
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)
我在测试类上方添加了@EnableBatchProcessing注释,但这并没有解决问题
如何为
步骤
作用域bean编写JUnit测试?您将在官方文档中找到更多信息,其中有一部分名为。但首先,您应该使用这两个注释(Spring 4.1之前的版本)对测试进行注释,以启用步骤范围
@TestExecutionListeners( { DependencyInjectionTestExecutionListener.class,
StepScopeTestExecutionListener.class })
或此注释,用于Spring 4.1+
@SpringBatchTest
然后还需要定义一个步骤执行,类似于此(取自文档)
我将继续接受这个答案,因为这似乎是解决问题的官方方式。但是,这对我不起作用,因此我将发布一个新问题来了解原因。请查看您是否有兴趣了解我是如何最终解决此问题的。
@SpringBatchTest
public StepExecution getStepExecution() {
StepExecution execution = MetaDataInstanceFactory.createStepExecution();
execution.getExecutionContext().putString("input.data", "foo,bar,spam");
return execution;
}