Binge Eater
Binge Eater

Reputation: 23

Problem with DAO save method when using dbunit

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("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);
    }

And here is the method "groupDao.save (new Group (" NEW_GROUP "));" must add a group with id = 4, name = "NEW_GROUP". 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:

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

Checked groupDao.save () - everything is fine, tried changing databaseTester.setSetUpOperation (DatabaseOperation ***), but it didn't help. Can you tell me where the problem is, maybe I'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 );

Upvotes: 0

Views: 154

Answers (1)

Jeff
Jeff

Reputation: 953

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

Upvotes: 0

Related Questions