Java 在JobLauncherTestUtils不工作的情况下运行作业的资格

Java 在JobLauncherTestUtils不工作的情况下运行作业的资格,java,spring,spring-batch,spring-java-config,Java,Spring,Spring Batch,Spring Java Config,我有一个launch-context.xml,它定义了7个不同的作业,所有作业都有相同的父作业。它们的名字像“jobA”、“jobB”等等 我试过: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "/launch-context.xml", "/OurBatchKernelTestConfig.xml" }) public class AllTest extends BaseRapto

我有一个launch-context.xml,它定义了7个不同的作业,所有作业都有相同的父作业。它们的名字像“jobA”、“jobB”等等

我试过:

 @RunWith(SpringJUnit4ClassRunner.class)
 @ContextConfiguration(locations = { "/launch-context.xml", "/OurBatchKernelTestConfig.xml" })
 public class AllTest extends BaseRaptorBatchTest {

     @Autowired
     private JobLauncherTestUtils utils;

     @Autowired
     @Qualifier(value="jobA")
     private Job job;

     @Test
     public void testLaunch() {
         Properties p = new Properties(); // then I set these up.
         JobExecution je = utils.launchJob(paraCvter.getJobParameters(p));
     }
 }
这是行不通的

我得到一个例外,比如:

 STDOUT [WARN ] [2015.04.15 11:14:42] support.GenericApplicationContext - Exception encountered during context initialization - cancelling refresh attempt
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobLauncherTestUtilsForSnapshot': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.batch.test.JobLauncherTestUtils.setJob(org.springframework.batch.core.Job); nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.batch.core.Job] is defined: expected single matching bean but found 2: coverageRuleBatch,generateMetricsSnapshotJob
我还尝试:

我试着:

这两种方法都不管用


我可以创建launch-content.xml的副本并删除其他作业。然后我在注释中提到了这一点,一切都很好。但是我需要7个不同的xml文件。单击。

根据异常消息中的信息,似乎需要消除Spring上下文中应该自动连接到成员变量
utils
的bean的歧义。大概是这样的:

 @Autowired
 @Qualifier(value="coverageRuleBatch")
 private JobLauncherTestUtils utils;
或者这个:

 @Autowired
 @Qualifier(value="generateMetricsSnapshotJob")
 private JobLauncherTestUtils utils;

应该解决歧义。

显然,您的测试配置不应该实例化超过1个
作业,因为JobLauncherTestUtils只需要一个bean

为了解决
JobLauncherTestUtils
限制,您可以通过构造函数/设置器自己管理它的依赖项,如中所述:

或者,您可以子类化
JobLauncherTestUtils
,并将
@限定符设置为:

@Autowired
@Qualifier("myChosenJobBeanName")
public void setJob(Job job) {
    this.job = job;
}
但是你需要为你想测试的每项工作覆盖它。最好以工厂法结束

您可以在不使用JobLauncherTestUtils的情况下启动作业。只需研究执行。这很简单:

getJobLauncher().run(this.job, jobParameters);

另外请看

因此,首先我想使用一个自动连接的JobLauncherTestUtils实例,并使用所需的作业对其调用setJob。但是使用@Qualifier(value=“jobA”)自动连接作业ivar不起作用。其次,我尝试使用包含作业集的JobLauncherTestUtils子类。这也不起作用。事实上,你所建议的解决方案与我上面引用的解决方案相同。这不起作用。如果自动连线不起作用,您可以尝试构造函数注入。虽然我在autowire+限定符方面取得了很好的成功,但您解决过这个问题吗?