【问题标题】:jdbc update query no working on unit test with derby database and SpringJUnit4ClassRunnerjdbc 更新查询无法使用 derby 数据库和 SpringJUnit4ClassRunner 进行单元测试
【发布时间】:2016-02-27 00:09:49
【问题描述】:

我在尝试进行单元测试时遇到问题。

测试类很简单,比如:

@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;
    }
}

服务:

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 重要部分:

<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> 

jdbc 连接属性:

<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> 

起初我认为问题与提交问题有关,但我尝试将自动提交属性的值设置为“true”并手动调用testDataSource.getConnection().commit(),但它不起作用。代码和方法运行良好,但测试没有更新 derby 数据库。在其他测试类中,数据在同一数据库中预设为dbUnit,并且代码有效。这个答案here 给出了我检查的可能问题的一般列表,我正在读取和写入相同模式中的相同表。我错过了什么吗?

【问题讨论】:

    标签: java unit-testing jdbc derby springjunit4classrunner


    【解决方案1】:

    尝试设置&lt;com:autocommit arg="true" /&gt;

    【讨论】:

      【解决方案2】:

      作为answer 我发布的问题,我验证了自动提交,如果我正在写入正确的数据库,但没有检查明显:你不能更新没有寄存器的表! 查询UPDATE MY_TABLE SET DT_UPDATE_OPERATION = ? 应用于空表,计数查询将始终返回 0。我刚刚配置测试以使 DbUnit 将状态从 xml 文件导入数据库。给您添麻烦了。

      【讨论】:

        猜你喜欢
        • 2018-11-14
        • 2013-09-02
        • 2010-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-23
        相关资源
        最近更新 更多