Announcement Announcement Module
Collapse
No announcement yet.
Why do I need a jobRepository when loading a job context in an Integration Test? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Why do I need a jobRepository when loading a job context in an Integration Test?

    Hi,

    I have a jobContext.xml file that I want to load up via an IntegrationTest just to make sure all the bean wirings & names will work before I deploy the batch job out to a system we have that runs batch jobs for us.

    The job context file is really simple (see below). No where in my bean hierarchy do I have a setter or @Autowired annotation for a jobRepository. When I run the batch job in production, it works fine as-is. When I wrote this test for it, the test fails because "No bean named 'jobRepository' is defined". My current hypothesis is that some combination of the Spring test support + the StepParserStepFactoryBean is trying to be more aggressive about fulfilling a setJobRepository( ) method than it should be?? Any ideas?

    Java 6 / Spring 3.0.3 / Spring Batch 2.1.8

    Here's the test:
    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration({"/config/jobContext.xml"})
    public class IntTestApplicationContext {
    
        @Autowired
        private ApplicationContext applicationContext;
    
        @Test
        public void testContext() {
            assertNotNull(this.applicationContext);
        }
    
    }
    Here's the jobContext.xml:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:batch="http://www.springframework.org/schema/batch"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                               http://www.springframework.org/schema/batch
                               http://www.springframework.org/schema/batch/spring-batch-2.1.xsd">
    
        
        <import resource="classpath:config/launchContext.xml" />
        
        <batch:job id="reportGenerationJob">
            <batch:step id="createReports">
                <batch:tasklet>
                    <batch:chunk
                        reader="reportGenerationItemReader"
                        writer="reportGenerationItemWriter"
                        skip-limit="100000"
                        commit-interval="1">
                        <batch:skippable-exception-classes>
                            <batch:include class="java.lang.Exception" />
                        </batch:skippable-exception-classes>
                    </batch:chunk>
                    <batch:listeners>
                        <batch:listener>
                            <!-- Calls Stored Procedures before and after this step, and logs errors -->
                            <bean class="poseur.generation.batch.ReportGenerationStepListener" />
                        </batch:listener>
                    </batch:listeners>
                </batch:tasklet>
            </batch:step>
            <batch:listeners>
                <batch:listener>
                    <ref bean="jobCompletionNotifier" />
                </batch:listener>
                <batch:listener>
                    <ref bean="statsJobExecutionListener" />
                </batch:listener>
            </batch:listeners>
        </batch:job>
        </bean>
    
       ... blah blah blah  ...
    </beans>
    Exception when running the IntTest is:
    Code:
    ERROR 2011-09-08 10:17:12,380 [test.context.TestContextManager] Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@3b59d796] to prepare test instance [com.opower.IntTestApplicationContext@758d74b]
    java.lang.IllegalStateException: Failed to load ApplicationContext
            at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:308)
            ....snip....
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'createReports': Cannot resolve reference to bean 'jobRepository' while setting bean property 'jobRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'jobRepository' is defined
            at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
            at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1317)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1076)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
            at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
            at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
            at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
            at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
            at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:557)
            at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
            at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
            at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:84)
            at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1)
            at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:280)
            at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:304)
            ... 30 more
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'jobRepository' is defined
            at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:521)
            at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1068)
            at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:274)
            at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
            at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
            ... 46 more

  • #2
    tvaughan77, the only reason of this exception I have in my mind is that you haven't defined jobRepository bean in your context. <batch:job> has attribute job-repository which should point to the JobRepository bean and is set to default value jobRepository. Jobs in String Batch can't exist on their own: they are registered in repository and repository maintains the state of the job (running / competed / failed / ...). So what you need to add to your context is e.g.:

    Code:
    <batch:job-repository id="jobRepository" data-source="dbDataSource" transaction-manager="transactionManager" table-prefix="BATCH_" />

    Comment

    Working...
    X