DAO在使用dbunit时保存方法的问题

huangapple go评论82阅读模式
英文:

Problem with DAO save method when using dbunit

问题

我有一个测试添加群组到数据库的类:

class GroupDAOTest extends TestCase {

    private IDatabaseTester databaseTester;
    private GroupDao groupDao;

    @BeforeEach
    protected void setUp() throws Exception
    {
        databaseTester = new JdbcDatabaseTester("org.postgresql.Driver",
                "jdbc:postgresql://localhost:5432/database_school", "principal", "school");
        String file = getClass().getClassLoader().getResource("preparedDataset.xml").getFile();
        IDataSet dataSet = new FlatXmlDataSetBuilder().build(new File(file));
        databaseTester.setDataSet(dataSet);
        databaseTester.setSetUpOperation(DatabaseOperation.CLEAN_INSERT);
        databaseTester.onSetup();
        groupDao = new GroupDao();
    }

    @Test
    void add() throws Exception {
        groupDao.save(new Group("NEW_GROUP"));

        IDataSet databaseDataSet = databaseTester.getConnection().createDataSet();
        ITable actualTable = databaseDataSet.getTable("groups");

        String file = getClass().getClassLoader().getResource("GroupDao/add.xml").getFile();
        IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(new File(file));
        ITable expectedTable = expectedDataSet.getTable("groups");

        Assertion.assertEquals(expectedTable, actualTable);
    }
}

这里的方法 "groupDao.save(new Group("NEW_GROUP"));" 必须添加一个 id = 4,name = "NEW_GROUP" 的群组。一开始测试通过了,但是当我多次运行它时,群组被添加了,但是出于某种原因 id 值每次递增了一。在某些运行中,它看起来是这样的:

[![enter image description here][1]][1]

检查了 groupDao.save() 方法 - 一切都正常,尝试了改变 databaseTester.setSetUpOperation(DatabaseOperation ***),但没有帮助。你能告诉我问题出在哪里吗?也许我有什么地方没有理解清楚?

以及我 DAO 方法的代码:

@Override
public void save(Group group) {
    try (Connection connection = connectionProvider.getConnection();
         PreparedStatement statement = connection.prepareStatement(SAVE_NEW_RECORD)) {
        statement.setString(1, group.getName());
        statement.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

还有表的模式:

CREATE TABLE groups
(
    group_id   serial PRIMARY KEY,
    group_name VARCHAR(10) UNIQUE NOT NULL
);

<details>
<summary>英文:</summary>

I have a class that tests adding a group to a database:

class GroupDAOTest extends TestCase {

private IDatabaseTester databaseTester;
private GroupDao groupDao;

@BeforeEach
protected void setUp() throws Exception
{
    databaseTester = new JdbcDatabaseTester(&quot;org.postgresql.Driver&quot;,
            &quot;jdbc:postgresql://localhost:5432/database_school&quot;, &quot;principal&quot;, &quot;school&quot;);
    String file = getClass().getClassLoader().getResource(&quot;preparedDataset.xml&quot;).getFile();
    IDataSet dataSet = new FlatXmlDataSetBuilder().build(new File(file));
    databaseTester.setDataSet(dataSet);
    databaseTester.setSetUpOperation(DatabaseOperation.CLEAN_INSERT);
    databaseTester.onSetup();
    groupDao = new GroupDao();
}

@Test
void add() throws Exception {
    groupDao.save(new Group(&quot;NEW_GROUP&quot;));

    IDataSet databaseDataSet = databaseTester.getConnection().createDataSet();
    ITable actualTable = databaseDataSet.getTable(&quot;groups&quot;);

    String file = getClass().getClassLoader().getResource(&quot;GroupDao/add.xml&quot;).getFile();
    IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(new File(file));
    ITable expectedTable = expectedDataSet.getTable(&quot;groups&quot;);

    Assertion.assertEquals(expectedTable, actualTable);
}

And here is the method &quot;groupDao.save (new Group (&quot; NEW_GROUP &quot;));&quot; must add a group with id = 4, name = &quot;NEW_GROUP&quot;. Once the test passed, but when I ran it again and again, the group was added, but for some reason the id grew by one. And for some launch it was already like this:

DAO在使用dbunit时保存方法的问题


Checked groupDao.save () - everything is fine, tried changing databaseTester.setSetUpOperation (DatabaseOperation ***), but it didn&#39;t help.
Can you tell me where the problem is, maybe I&#39;m just not clearing something?

And just in case my dao method:

@Override
public void save(Group group) {
try (Connection connection = connectionProvider.getConnection();
PreparedStatement statement = connection.prepareStatement(SAVE_NEW_RECORD)) {
statement.setString(1, group.getName());
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}

And table schema:

CREATE TABLE groups
(
    group_id   serial PRIMARY KEY,
    group_name VARCHAR(10) UNIQUE NOT NULL
);


  [1]: https://i.stack.imgur.com/FSH2j.png

</details>


# 答案1
**得分**: 0

&gt; 一开始测试通过了,但是当我一遍又一遍地运行它时,虽然组被添加了,但由于某种原因 id 增长了一。

问题不在于你的代码或配置。PostgreSQL 的序列字段类型是自增的。每次将行保存到表中时,它都会将字段值加1。

请改用 dbUnit 的 ValueComparer 断言,而不是您当前使用的仅进行相等性比较的断言方法。
http://dbunit.sourceforge.net/datacomparisons/valuecomparer.html

<details>
<summary>英文:</summary>

&gt; Once the test passed, but when I ran it again and again, the group was added, but for some reason the id grew by one.

The issue is not with your code or configuration.  PostgreSQL serial field type is auto-increment.  It adds 1 to the field value each time saving row to the table.

Use the dbUnit ValueComparer assertion instead to compare with greater than or equal to instead of the assertion method you currently using which compares only on equality.
http://dbunit.sourceforge.net/datacomparisons/valuecomparer.html


</details>



huangapple
  • 本文由 发表于 2020年8月21日 00:08:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63509085.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定