Java Reference
In-Depth Information
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import com.apress.springbatch.statement.dao.TickerDao;
import com.apress.springbatch.statement.domain.Customer;
import com.apress.springbatch.statement.domain.Statement;
public class CustomerStatementReader implements ItemReader<Statement> {
private ItemReader<Customer> customerReader;
private TickerDao tickerDao;
public Statement read() throws Exception, UnexpectedInputException,
ParseException {
Customer customer = customerReader.read();
if(customer == null) {
return null;
} else {
Statement statement = new Statement();
statement.setCustomer(customer);
statement.setSecurityTotal(
tickerDao.getTotalValueForCustomer(customer.getId()));
statement.setStocks(tickerDao.getStocksForCustomer(customer.getId()));
return statement;
}
}
public void setCustomerReader(ItemReader<Customer> customerReader) {
this.customerReader = customerReader;
}
public void setTickerDao(TickerDao tickerDao) {
this.tickerDao = tickerDao;
}
}
The method you're testing for this class is obviously
read()
. This method requires two external
dependencies: an instance of an ItemReader (remember, you used a JdbcCursorItemReader in the actual
job) and a reference to your
TickerDao
. To test this method, you have two test methods, one for each of
the method's two execution branches (one for when the customer is
null
and one for when it isn't).
To start this test, let's create the test-case class and the
@Before
method so your objects are built for
later use. Listing 12-5 shows the test case with the
setup
method identified with the
@Before
annotation
and three class attributes.
Listing 12-5.
CustomerStatementReaderTest
package com.apress.springbatch.statement.reader;