【问题标题】:Issues with MySQL increment column and DBunit datasetsMySQL 增量列和 DBunit 数据集的问题
【发布时间】:2014-04-04 17:19:22
【问题描述】:

我正在尝试使用 dbUnit 为 MySQL 中的一个表编写一个集成测试,该表有一个带有 autoincrement 的列。 集成测试如下:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={
    JdbcRepositoryConfiguration.class, 
    DbUnitConnectionConfiguration.class
})
@TestExecutionListeners({ 
    DependencyInjectionTestExecutionListener.class,
    DirtiesContextTestExecutionListener.class,
    DbUnitTestExecutionListener.class
})
@DirtiesContext(classMode=ClassMode.AFTER_CLASS)
@DbUnitConfiguration(databaseConnection="dbUnitConnection")
public class IntegrationTest {
    @Autowired private JdbcRepositoryConfiguration configuration;

    private Loader loader;

    @Before
    public void setup() throws JSchException {
        loader = new Loader(configuration.jdbcTemplate());
    }

    @Test
    @DatabaseSetup("classpath:dataset.xml")
    public void loads() throws Exception {
        assertThat(loader.load(), contains("something"));
    }
}

对于没有increment 列的表,我有相同的集成测试结构,并且测试工作正常。 dataset.xml 看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<dataset>
    <sometable 
        id="1"
        regexp="something"
        descr="descr"
    />

</dataset>

调试我可以看到设置数据所采取的操作是删除所有数据并执行插入,更具体地说:

插入某个表(id、regexp、descr)值(?、?、?)

我得到的错误是:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'regexp, descr) values (1, 'something', 'descr')' at line 1

为了完整起见,DbUnitConfiguration.class 具有以下 spring bean 设置:

@Bean
public IDatabaseConnection dbUnitConnection() throws SQLException, DatabaseUnitException, JSchException {
    Connection dbConn = configuration.jdbcTemplate().getDataSource().getConnection();
    IDatabaseConnection connection = new DatabaseConnection(dbConn) {
        @Override
        public void close() throws SQLException {}
    };
    DatabaseConfig dbConfig = connection.getConfig();
    dbConfig.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new MySqlDataTypeFactory());
    return connection;
}

【问题讨论】:

    标签: java mysql jdbctemplate dbunit spring-test-dbunit


    【解决方案1】:

    原来与自动增量无关。

    由于列regexp 是 MySQL 中的保留字,因此引发了错误。

    要解决此问题,dbUnit 设置必须具有以下行:

    dbConfig.setProperty(DatabaseConfig.PROPERTY_ESCAPE_PATTERN , "`?`");
    

    现在测试就可以了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多