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

  • Mocking Step Scoped ItemReader

    Hi All,
    I need to mock a webservice rest client (webService.fetchFeed()) injected into my custom ItemReader to test a step.
    1. CustomeReader implements ItemReader<DataSet>
    2. Testing my step using below code.
    JobExecution exec = jobLauncherTestUtils.launchStep("step1", jobParameters);

    Code snapshot:
    CustomJobMockTest.java
    @Autowired
    private JobLauncherTestUtils jobLauncherTestUtils;

    @Autowired
    private ItemReader<DataSet> reader;

    private WebService webService;

    @Test
    @DirtiesContext
    public void testLaunchJob() throws Exception {
    webService = mock(WebService.class);

    when(webService.exportFeed(anyString())).thenRetur n(new DataSet())
    .thenReturn(null);

    JobParameters jobParameters = new JobParametersBuilder().addString("statusId", "STAT_123").toJobParameters();

    JobExecution exec = jobLauncherTestUtils.launchStep("webProcessor", jobParameters);

    assertEquals(BatchStatus.COMPLETED, exec.getStatus());

    }

    custom-context.xml
    <bean id="customReader" class="com.batch.custom.CustomeReader " scope="step">
    <property name="providerId" value="#{jobParameters['statusId']}" />
    <property name="webService" ref="webService"/>
    </bean>

    CustomReader.java
    @Override
    public WatchlistDataSet read() throws Exception {

    logger.info("Entity Reader=>"+this);

    return webService.exportFeed(providerId);

    }

    I need to inject the mocked webService into the CustomReader bean created when we launch the job. As my reader bean is step scoped, I am not getting hold of the reader. The reader instance in CustomJobMockTest.java is different that the instance available inside my MyReader.java

    Could you please guide me on the approach to achieve this Step testing.
    Last edited by sam.tiku; Jun 30th, 2013, 07:27 AM.

  • #2
    See section 10.4 - Testing Step Scoped Components, http://static.springsource.org/sprin...l/testing.html

    Comment

    Working...
    X