Announcement Announcement Module
Collapse
No announcement yet.
JdbcCursorItemReader test fails on step scope Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JdbcCursorItemReader test fails on step scope

    This is the reader configuration used by a job

    Code:
    <bean id="assetItemReader" class="org.springframework.batch.item.database.JdbcCursorItemReader" scope="step">
    		<property name="sql" value="${assetSql}"/>
            <property name="fetchSize" value="${fetchSize}"/>
            <property name="rowMapper" ref="assetMapper"/>
            <property name="dataSource" ref="dataSource"/>
    	</bean>
    The test class which attempts to test this reader.

    Code:
    @ContextConfiguration
    @RunWith(SpringJUnit4ClassRunner.class)
    public class AssetItemReaderTests extends AbstractJobTests
    {
        @Autowired
        private ItemReader<Asset> reader;
    
        @Test
        public void testReadFromAsset() throws Exception
        {
            open(reader);
            List<Asset> inputs = getAssets(reader);
            close(reader);
    
            int itemCount = inputs.size();
            assertTrue(itemCount > 0);
    
            for (int i = 0; i < itemCount; i++)
            {
                assertNotNull(inputs.get(i));
            }
    
        }
    
        /**
         * Read all assets using the provided reader.
         */
        private List<Asset> getAssets(ItemReader<Asset> reader) throws Exception
        {
            Asset asset;
            List<Asset> result = new ArrayList<Asset>();
            while ((asset = reader.read()) != null)
            {
                result.add(asset);
            }
            return result;
    
        }
    
        /**
         * Open the reader if applicable.
         */
        private void open(ItemReader<?> reader)
        {
            if (reader instanceof ItemStream)
            {
                ((ItemStream) reader).open(new ExecutionContext());
            }
        }
    
        /**
         * Close the reader if applicable.
         */
        private void close(ItemReader<?> reader)
        {
            if (reader instanceof ItemStream)
            {
                ((ItemStream) reader).close();
            }
        }
    }
    The error message is " java.lang.IllegalStateException: No context holder available for step scope".

    The full error log
    Code:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.assetItemReader': Scope 'step' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No context holder available for step scope
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:337)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:188)
    	at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:33)
    	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:182)
    	at $Proxy39.open(Unknown Source)
    	at com.dcinv.dragon.infrastructure.batch.AssetItemReaderTests.open(AssetItemReaderTests.java:77)
    	at com.dcinv.dragon.infrastructure.batch.AssetItemReaderTests.testReadFromAsset(AssetItemReaderTests.java:41)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
    	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
    	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
    	at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
    	at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
    	at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
    	at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:338)
    	at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:997)
    Caused by: java.lang.IllegalStateException: No context holder available for step scope
    	at org.springframework.batch.core.scope.StepScope.getContext(StepScope.java:193)
    	at org.springframework.batch.core.scope.StepScope.get(StepScope.java:135)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:323)
    	... 38 more

  • #2
    It was incorrect to place the step scope on the reader, writer. Works after that was removed.

    Comment


    • #3
      It was probably correct to use step scope in the reader, actually (it's stateful). Spring Batch Test has a couple of utilities for working with step-scoped beans in integration tests. The easiest solution for your example would be to declare the TestExecutionListeners explicitly at the top of the test class (overriding the defaults):

      Code:
      @TestExecutionListeners( { DependencyInjectionTestExecutionListener.class, StepScopeTestExecutionListener.class })
      This is covered in the user guide I think (but only available recently, maybe in 2.1.0.RC1).

      Comment


      • #4
        After changing spring-core, spring-infrastructure and spring-test to 2.1.0.RC1, I get the following error

        Code:
        -------------------------------------------------------------------------------
        Test set: org.springframework.batch.admin.sample.JobIntegrationTests
        -------------------------------------------------------------------------------
        Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 6.343 sec <<< FAILURE!
        testSimpleProperties(org.springframework.batch.admin.sample.JobIntegrationTests)  Time elapsed: 6.265 sec  <<< ERROR!
        java.lang.IllegalStateException: Failed to load ApplicationContext
        	at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:308)
        	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
        	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
        	at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:333)
        	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:220)
        	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:301)
        Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.batch.core.configuration.support.ClassPathXmlJobLoader] for bean with name 'jobLoader' defined in URL [jar:file:/C:/Documents%20and%20Settings/shri/.m2/repository/org/springframework/batch/spring-batch-admin-manager/1.0.0.M1/spring-batch-admin-manager-1.0.0.M1.jar!/META-INF/bootstrap/execution-context.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.batch.core.configuration.support.ClassPathXmlJobLoader
        	at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1220)
        	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:568)
        	at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1290)
        	at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:843)
        	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:540)
        	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:842)
        	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:416)
        	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)
        	... 28 more
        Caused by: java.lang.ClassNotFoundException: org.springframework.batch.core.configuration.support.ClassPathXmlJobLoader
        	at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
        	at java.security.AccessController.doPrivileged(Native Method)
        	at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        	at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        	at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
        	at org.springframework.util.ClassUtils.forName(ClassUtils.java:258)
        	at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:408)
        	at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1242)
        	at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1212)
        	... 38 more
        The POM file for reference.

        Code:
        <?xml version="1.0" encoding="UTF-8"?>
        <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        	<modelVersion>4.0.0</modelVersion>
        	<artifactId>spring-batch-admin-sample</artifactId>
        	<description>A sample web application (WAR project) for Spring Batch Admin console.</description>
        	<parent>
        		<artifactId>spring-batch-admin-parent</artifactId>
        		<groupId>org.springframework.batch</groupId>
        		<version>1.0.0.M1</version>
        		<relativePath>../spring-batch-admin-parent</relativePath>
        	</parent>
        	<packaging>war</packaging>
        	<name>Web Sample</name>
        	<dependencies>
        		<dependency>
        			<groupId>junit</groupId>
        			<artifactId>junit</artifactId>
        		</dependency>
        		<dependency>
        			<groupId>org.easymock</groupId>
        			<artifactId>easymock</artifactId>
        		</dependency>
        		<dependency>
        			<groupId>javax.servlet</groupId>
        			<artifactId>servlet-api</artifactId>
        		</dependency>
        		<dependency>
        			<groupId>org.springframework</groupId>
        			<artifactId>spring-aop</artifactId>
        		</dependency>
        		<dependency>
        			<groupId>org.springframework</groupId>
        			<artifactId>spring-web</artifactId>
        		</dependency>
        		<dependency>
        			<groupId>org.springframework</groupId>
        			<artifactId>spring-webmvc</artifactId>
        		</dependency>
        		<dependency>
        			<groupId>org.springframework</groupId>
        			<artifactId>spring-jdbc</artifactId>
        		</dependency>
        		<dependency>
        			<groupId>commons-dbcp</groupId>
        			<artifactId>commons-dbcp</artifactId>
        		</dependency>
        		<dependency>
        			<groupId>commons-io</groupId>
        			<artifactId>commons-io</artifactId>
        		</dependency>
        		<dependency>
        			<groupId>commons-collections</groupId>
        			<artifactId>commons-collections</artifactId>
        		</dependency>
        		<dependency>
        			<groupId>org.aspectj</groupId>
        			<artifactId>aspectjrt</artifactId>
        		</dependency>
        		<dependency>
        			<groupId>org.aspectj</groupId>
        			<artifactId>aspectjweaver</artifactId>
        		</dependency>
        		<dependency>
        			<groupId>hsqldb</groupId>
        			<artifactId>hsqldb</artifactId>
        			<scope>runtime</scope>
        		</dependency>
        		<dependency>
        			<groupId>org.freemarker</groupId>
        			<artifactId>freemarker</artifactId>
        		</dependency>
        		<dependency>
        			<groupId>javax.servlet</groupId>
        			<artifactId>jstl</artifactId>
        			<scope>runtime</scope>
        		</dependency>
        		<dependency>
        			<groupId>org.springframework.webflow</groupId>
        			<artifactId>spring-js</artifactId>
        		</dependency>
        		<dependency>
        			<groupId>org.springframework.security</groupId>
        			<artifactId>spring-security-taglibs</artifactId>
        		</dependency>
        		<dependency>
        			<groupId>org.springframework.batch</groupId>
        			<artifactId>spring-batch-core</artifactId>
        			<version>2.1.0.RC1</version>
        		</dependency>
        		<dependency>
        			<groupId>org.springframework.batch</groupId>
        			<artifactId>spring-batch-infrastructure</artifactId>
        			<version>2.1.0.RC1</version>
        		</dependency>
        		<dependency>
        			<groupId>org.springframework.batch</groupId>
        			<artifactId>spring-batch-admin-manager</artifactId>
        			<version>${pom.version}</version>
        		</dependency>
        		<dependency>
        			<groupId>org.springframework.batch</groupId>
        			<artifactId>spring-batch-admin-resources</artifactId>
        			<version>${pom.version}</version>
        		</dependency>
        		<dependency>
        			<groupId>com.dcinv.dragon</groupId>
        			<artifactId>dragon-core</artifactId>
        			<version>0.1.0-SNAPSHOT</version>
        			<type>jar</type>
        			<scope>compile</scope>
        		</dependency>
        		<dependency>
        			<groupId>org.springframework.batch</groupId>
        			<artifactId>spring-batch-test</artifactId>
        			<version>2.1.0.RC1</version>
        		</dependency>
        		<dependency>
        			<groupId>org.springframework</groupId>
        			<artifactId>spring-test</artifactId>
        			<version>3.0.0.RELEASE</version>
        		</dependency>
        	</dependencies>
        	<build>
        		<plugins>
        			<plugin>
        				<groupId>org.mortbay.jetty</groupId>
        				<artifactId>maven-jetty-plugin</artifactId>
        				<configuration>
        					<contextPath>/contributor</contextPath>
        				</configuration>
        			</plugin>
        		</plugins>
        	</build>
        	<repositories>
        		<repository>
        			<id>spring-milestones</id>
        			<name>Spring Maven Milestone Repository</name>
        			<url>http://s3.amazonaws.com/maven.springframework.org/milestone</url>
        			<snapshots>
        				<enabled>false</enabled>
        			</snapshots>
        		</repository>
        	</repositories>
        </project>

        Comment


        • #5
          ClassPathXmlJobLoaderwas renamed AutomaticJobRegistrar.

          Comment

          Working...
          X