javaunit-testingjdbcderbyspringjunit4classrunner

jdbc update query no working on unit test with derby database and SpringJUnit4ClassRunner


I am having problems trying to do an unit test.

The test class is simple like:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/job-runner-context.xml})
public class AtualizarDataServiceTest {

    @Autowired
    DataSource testDataSource;

    @Autowired
    Service service;

    @Before
    public void setUp() throws DatabaseUnitException, SQLException{
        service.setDataSource(testDataSource);
    }

    @Test
    public final void testUpdateDate() throws SQLException {
        assertTrue(verifyDate());
        service.updateDate();
        assertFalse(verifyDate()); //Assert brokes here
    }

    private boolean verifyDate(){
        SimpleJdbcTemplate consultaTemplate = new SimpleJdbcTemplate(testDataSource);
        int count = consultaTemplate.queryForInt("SELECT COUNT(*) FROM MY_TABLE");
        return count == 0;
    }
}

The service:

public class Service {

    private DataSource dataSource;

    public void updateDate(){
        SimpleJdbcTemplate jdbcTemplate = new SimpleJdbcTemplate(getDataSource());
        String query = "UPDATE MY_TABLE SET DT_UPDATE_OPERATION = ?";
        jdbcTemplate.update(query, new Object[]{new java.sql.Date(Calendar.getInstance().getTime().getTime())});
    }

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

}

job-runner-context.xml important pieces:

<context:annotation-config/>
    <context:component-scan base-package="my.package"/>

    <bean class="my.package.Service"/>

    <bean id="testDataSource" class="com.read.only.MyBasicDataSource" destroy-method="close" lazy-init="true">
        <property name="jdbcReference" value="derby" />
    </bean> 

the jdbc connection properties:

<com:jdbcReference name="derby" type="DATABASE">
        <com:credential user="" password="" />
        <com:autocommit arg="false" />
        <com:databaseConfig driverClassName="org.apache.derby.jdbc.EmbeddedDriver"
            url="jdbc:derby:dbderby;create=true" databaseName="ANY" />
    </com:jdbcReference> 

At fist I thought the problem was related to commit issues but I tried set the value of autocommit properties to "true" and also manually call testDataSource.getConnection().commit() but it didn't work. The code and methods are working fine but the test isn't updating the derby database. In other test classes data is preset with dbUnit in the same database and the code works. This answer here gives an general list of possible problems I checked and I am reading and writing to the same tables in the same schemas. Am I missing something?


Solution

  • As the answer to the question I posted I verified the autocommit and if I was writing to the right database, but didn't check the obvious: you can't update a table with no registers! The query UPDATE MY_TABLE SET DT_UPDATE_OPERATION = ? was applied to an empty table and the count query would always return 0. I just configured the test to make DbUnit import an state to the database from a xml file. Sorry for the trouble.