0

の列を持つ MySQL のテーブルに対して、dbUnit を使用して統合テストを作成しようとしています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;
}
4

1 に答える 1

0

自動インクリメントとは何の関係もないことがわかりました。

regexp列が MySQL の予約語であるため、エラーがスローされました。

これを回避するには、dbUnit セットアップに次の行を含める必要があります。

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

そして、テストは今まさに機能しています。

于 2014-04-05T10:55:46.973 に答える