Java Reference
In-Depth Information
Listing 12-12. Testing the Saving and Retrieving of a Ticker Using TickerDao
package com.apress.springbatch.statement.dao;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.math.BigDecimal;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import com.apress.springbatch.statement.domain.Ticker;
@ContextConfiguration(locations = {"/test-context.xml"})
public class TickerDaoIntegrationTest extends
AbstractTransactionalJUnit4SpringContextTests {
private TickerDao tickerDao;
public void testTickerSaveRoundTrip() {
Ticker ticker = new Ticker();
ticker.setPrice(new BigDecimal("222.22"));
Ticker result = tickerDao.findTickerBySymbol("MTM");
assertEquals(222.22, result.getPrice().doubleValue(), 0);
assertEquals("MTM", result.getTicker());
assertTrue(result.getId() >= 0);
The test shown in Listing 12-12 begins by creating a new Ticker object to be saved. You then use the
tickerDao provided by Spring to save it and subsequently retrieve it. Finally, you validate that the data
you saved matches the data that was retrieved and that the id was set, signifying that it truly was saved to
the database.
When you execute this test, a new instance of HSQLDB is launched, the database schema is created,
and your objects are bootstrapped and injected, all prior the execution of the test method. The test
method is executed in its own transaction, which is rolled back at the end of the test, leaving the
database pristine for the next test method to be executed.
Integration tests like testTickerSaveRoundTrip can be hugely valuable when you're developing a
system. The ability to determine if things are being wired correctly, if SQL is correct, and even if the
Search WWH ::

Custom Search