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

  • BeanNotOfRequiredTypeException when Testing Step Scoped Component

    I'm following the steps outlined at to test a Step scoped bean. I've added the two listeners and created a StepExecution.

    @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
    	StepScopeTestExecutionListener.class })
    public class VehiclePagingItemReaderStepContextTest {
    	VehiclePagingItemReader<List<Vehicle>> itemReader;
    	public StepExecution getStepExecution() {
    		JobParameters jobParameters = new JobParametersBuilder()
    			.addString("myParameter", "parameter-1")
    		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.

    	<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" />
    What am I missing?

  • #2
    I think it is something related to this thread,

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

    Not sure. Still pondering...


    • #3
      Yup, that was it.

      VehiclePagingItemReader extends AbstractPagingItemReader
      AbstractPagingItemReader extends AbstractItemCountingItemStreamItemReader
      AbstractItemCountingItemStreamItemReader implements ItemStreamReader

      So, I changed this:

      	VehiclePagingItemReader<List<Vehicle>> itemReader;
      to this:

      	ItemStreamReader<List<Vehicle>> itemReader
      And all is well.


      • #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


        • #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

          	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.

          	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 !=
          		assert null ==