Announcement Announcement Module
Collapse
No announcement yet.
BeanNotOfRequiredTypeException when Testing Step Scoped Component Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • BeanNotOfRequiredTypeException when Testing Step Scoped Component

    I'm following the steps outlined at http://static.springsource.org/sprin...l/testing.html to test a Step scoped bean. I've added the two listeners and created a StepExecution.

    Code:
    @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
    	StepScopeTestExecutionListener.class })
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations={"/InventoryFetcherJob-test-mock-config.xml"})
    public class VehiclePagingItemReaderStepContextTest {
    
    	@Resource
    	VehiclePagingItemReader<List<Vehicle>> itemReader;
    
    	public StepExecution getStepExecution() {
    		JobParameters jobParameters = new JobParametersBuilder()
    			.addString("myParameter", "parameter-1")
    			.toJobParameters();
    		StepExecution execution = MetaDataInstanceFactory.createStepExecution(jobParameters);
    		return execution;
    	}
    But when I execute the test, I get this error.

    Caused by: org.springframework.beans.factory.BeanNotOfRequire dTypeException: Bean named 'itemReader' must be of type [com.integration.batch.reader.VehiclePagingItemRead er], but was actually of type [com.sun.proxy.$Proxy17]

    Here is my bean definition.

    Code:
    	<bean id="itemReader" class="com.integration.batch.reader.VehiclePagingItemReader" scope="step" >
    	    <property name="myParameter" value="#{stepExecutionContext[myParameter]}" />
    	    <property name="pageSize" value="${pageSize}" />
    	    <property name="vehicleReadService" ref="vehicleReadService" />
        </bean>
    What am I missing?

  • #2
    I think it is something related to this thread, http://stackoverflow.com/questions/3...ith-spring-aop.

    "Proxy classes will always implement all interfaces they were created with."

    Not sure. Still pondering...

    Comment


    • #3
      Yup, that was it.

      VehiclePagingItemReader extends AbstractPagingItemReader
      AbstractPagingItemReader extends AbstractItemCountingItemStreamItemReader
      AbstractItemCountingItemStreamItemReader implements ItemStreamReader

      So, I changed this:

      Code:
      	@Resource
      	VehiclePagingItemReader<List<Vehicle>> itemReader;
      to this:

      Code:
      	@Resource
      	ItemStreamReader<List<Vehicle>> itemReader
      And all is well.

      Comment


      • #4
        Close, but no cigar. I now have an instance of ItemStreamReader but I still really want VehiclePagingItemReader

        First test passes, second test fails.

        assert itemReader instanceof ItemStreamReader
        assert itemReader instanceof VehiclePagingItemReader

        Comment


        • #5
          Figured it out for real this time. Step scoped beans are pretty interesting. As the stackoverflow commenter stated, Spring carries out Step scoped beans via Proxy objects. The proxy implements all of the interfaces they were created with. Sadly, the method that I needed to test, readPage() was not part of any interface. So I made a new Interface called PagingItemReader and it contains one method, readPage(page, pageSize).

          I updated my VehiclePagingItemReader to implement this new interface and then changed my wiring to receive a PagingItemReader bean

          Code:
                  @Resource
          	PagingItemReader<List<Vehicle>> itemReader
          I could then have a unit test that paged through the results. My itemReader was reading results from a Mock Service that had 11 items in it. The contract for the ItemReader says that when it reaches the end of the line, it has to return null, so I tested that condition as well.

          Code:
          	public void testReadPage() {
          		int page = 0
          		int pageSize = 5
          		
          		List<Map<String, Vehicle>> vehicles = itemReader.readPage(page, pageSize)
          		
          		assert pageSize == vehicles.size()
          		11.times {
          			assert null != itemReader.read()
          		}
          		
          		assert null == itemReader.read()
          	}

          Comment

          Working...
          X