Jun 24th, 2011, 11:30 PM
Junit test fails to autowire JobLauncherTestUtils sees two jobs, "job1" and "myJob"
I have a problem testing a Spring batch job using a SpringJUnit4ClassRunner test. The batch job runs successfully when I use CommandLineRunner and the following mvn command:
mvn exec:java -Dexec.mainClass=org.springframework.batch.core.lau nch.support.CommandLineRunner -Dexec.args="jobs/startServerJob.xml startServerJob -next"
But when I run the same batch job through a spring batch unit test I get the following error:
expected single matching bean but found 2: [job1, startServerJob]
Caused by: org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'org.springframework.batch.test.JobLauncherTestUti ls#0': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Could not autowire method: public void org.springframework.batch.test.JobLauncherTestUtil s.setJob(org.springframework.batch.core.Job); nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No unique bean of type [org.springframework.batch.core.Job] is defined: expected single matching bean but found 2: [job1, startServerJob]
I've double checked the configuration files, but I cannot find a bean registered as job1. I suspect that there is a default "job1" setup at run time and then when the unit test tries to run my job "startServerJob" it creates a duplicate job name and the autowire fails.
If I change the name of my job from "startServerJob" to "job1" the test is able to run but this is not a workable solution for me. Here is part of my job configuration:
<job id="startServerJob" job-repository="jobRepository" incrementer="dynamicJobParameters" restartable="true" xmlns="http://www.springframework.org/schema/batch" >
<chunk reader="serverItemReader" processor="serverItemProcessor" writer="dummyWriter" commit-interval="1" />
It's probably a dumb configuration problem, but I have not been able to resolve it. If anyone has suggestions on how to fix this problem or what other techniques I can use to run my unit test let me know.
Thanks in advance.
Jun 27th, 2011, 06:02 AM
There must be a job1 bean somewhere in your configuration. Try to track <import /> tags, especially those using wildcard (e.g. **/*.xml). I'm pretty sure there's a job1 bean in the Spring Batch Admin sample. Perhaps Spring Batch Admin is on your classpath and your configuration imports Spring configuration files from META-INF (just a thought).
Jun 28th, 2011, 10:47 AM
Thanks for the quick reply. That was the problem. My junit test was including /launch-context.xml which then imported the following:
<import resource="classpath:/META-INF/spring/module-context.xml" />
The module-context.xml has some example definitions created and this was where the "job1" was getting defined. Thanks for the information as I was searching the wrong folders to find the job1 definition.
Tags for this Thread