Java Reference
In-Depth Information
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.when;
import java.math.BigDecimal;
import java.util.ArrayList;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.batch.item.ItemReader;
import com.apress.springbatch.statement.dao.TickerDao;
import com.apress.springbatch.statement.domain.Customer;
import com.apress.springbatch.statement.domain.Statement;
import com.apress.springbatch.statement.domain.Transaction;
public class CustomerStatementReaderTest {
private CustomerStatementReader reader;
private TickerDao tickerDao;
private ItemReader<Customer> customerReader;
public void setUp() {
reader = new CustomerStatementReader();
The three attributes of the test class are the class under test ( CustomerStatementReader ) and the two
dependencies ( TickerDao and the ItemReader). By using the @Mock annotation, you tell Mockito that
these attributes should be mocked for your test. When the test is executed, Mockito creates a proxy for
each of these for your test to use.
In the setup method, you do two things. First you initialize the mocks with Mockito's
MockitoAnnotations.initMocks method. This method initializes all the objects you previously indicated
with a mock object for you to use. This is a quick and easy way to create the mock objects you need in
the future.
The next thing you do in the setup method is create a new instance of the class to test. By creating
this class here, you can be sure that each test method contains a clean instance of the class under test.
This prevents any residual state in the test object from one method from having an impact on your other
test methods. After you create CustomerStatementReader , you inject the mock objects the same way
Spring would do it for you on bootstrapping the application.
Because you now have a new instance of the object under test and a fresh set of mock objects to
satisfy your dependencies on the Spring Batch framework as well as the database, you can write your test
Search WWH ::

Custom Search