DBunit数据集:列正则表达式是MySQL中的保留字

问题描述 投票:0回答:1

我正在尝试使用 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个回答
1
投票

事实证明与自动增量无关。

抛出错误是因为列

regexp
是MySQL中的保留字。

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

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

测试现在就可以了。

© www.soinside.com 2019 - 2024. All rights reserved.