我正在使用DbUnit在postgreSql数据库上运行一些测试。为了能够运行我的测试,我通过在每次测试之前重新填充数据库表,运行一个干净的插入,使数据库进入一个众所周知的状态。因此,我使用下面的 FlatXmlDataSet 定义(与附加的SQL模式进行比较)。
但是,如果我运行 testCreateAvatar()测试用例,由于状态代码不匹配导致异常,这是由于sql插入失败导致的,因为已经存在主键( id字段)。查看我的数据库向我显示,测试数据集的插入不会更新相应的* avatars_id_seq *和* users_id_seq *序列表,这些表用于生成id字段(postgresql生成自动增量值的机制)。
这意味着,如果我在 FlatXmlDataSet 定义中定义静态ID,则不会更新自动增量值。所以我的问题是如何改变这种行为或者自己设置自动增量值(使用DbUnit)。
头像创建测试用例
@Test
public void testCreateAvatar() throws Exception {
// Set up the request url.
final HttpPost request = new HttpPost(
"http://localhost:9095/rest/avatars");
// Setup the JSON blob, ...
JSONObject jsonAvatar = new JSONObject();
jsonAvatar.put("imageUrl", "images/dussel.jpg");
// ... add it to the post request ...
StringEntity input = new StringEntity(jsonAvatar.toString());
input.setContentType("application/json");
request.setEntity(input);
// ... and execute the request.
final HttpResponse response = HttpClientBuilder.create().build()
.execute(request);
// Verify the result.
assertThat(response.getStatusLine().getStatusCode(),
equalTo(HttpStatus.SC_CREATED));
// Fetch dussel duck from the database ...
Avatar dussel = getServiceObjDao().queryForFirst(
getServiceObjDao().queryBuilder().where()
.eq("image_url", "images/dussel.jpg")
.prepare());
// ... and verify that the object was created correctly.
assertThat(dussel, notNullValue());
assertThat("images/dussel.jpg", equalTo(dussel.getImageUrl()));
}
DbUnit数据集
<?xml version='1.0' encoding='UTF-8'?>
<dataset>
<!-- Avatars -->
<avatars
id="1"
image_url="images/donald.jpg" />
<avatars
id="2"
image_url="images/daisy.jpg" />
<!-- Users -->
<users
id = "1"
name = "Donald Duck"
email = "donald.duck@entenhausen.de"
password = "quack" />
<users
id = "2"
name = "Daisy Duck"
email = "daisy.duck@entenhausen.de"
password = "flower" />
</dataset>
用户和头像表架构
CREATE TABLE avatars (
id BIGSERIAL PRIMARY KEY,
cdate TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
mdate TIMESTAMP,
image_url VARCHAR(200),
UNIQUE (image_url)
);
CREATE TABLE users (
id BIGSERIAL PRIMARY KEY,
cdate TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
mdate TIMESTAMP,
name VARCHAR(160) NOT NULL,
email VARCHAR (355) UNIQUE NOT NULL,
password VARCHAR(30) NOT NULL,
avatar_id BIGINT,
UNIQUE (name),
CONSTRAINT user_avatar_id FOREIGN KEY (avatar_id)
REFERENCES avatars (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
);
答案 0 :(得分:3)
您可以使用setval设置序列的值,例如
SELECT SETVAL('sequence_name', 1000);
其中sequence_name是序列的名称,在表中使用/ dt的psql中可见,1000是您要将其设置为的值。您可能希望将其设置为表中Id的最大值。
我真正不知道的是如何让DbUnit发出这个SQL。
答案 1 :(得分:3)
下面的函数查找数据库中的所有序列,从序列名称中提取相应表的名称,最后根据相应表中的最大id值更新序列的当前值。由于还没有更好的解决方案,这似乎是要走的路。希望,这有助于某人。
基于harmic建议的简单解决方案
@Before
public void resetSequence() {
Connection conn = null;
try {
// Establish a database connection.
conn = DriverManager.getConnection(
this.props.getProperty("database.jdbc.connectionURL"),
this.props.getProperty("database.jdbc.username"),
this.props.getProperty("database.jdbc.password"));
// Select all sequence names ...
Statement seqStmt = conn.createStatement();
ResultSet rs = seqStmt.executeQuery("SELECT c.relname FROM pg_class c WHERE c.relkind = 'S';");
// ... and update the sequence to match max(id)+1.
while (rs.next()) {
String sequence = rs.getString("relname");
String table = sequence.substring(0, sequence.length()-7);
Statement updStmt = conn.createStatement();
updStmt.executeQuery("SELECT SETVAL('" + sequence + "', (SELECT MAX(id)+1 FROM '" + table + "'));");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
}
}
}