rickygrimes
rickygrimes

Reputation: 2716

JUnit test case to insert records into database

I want to write a JUnit test case to insert records into my Oracle database. But I want to insert only if the records don't exist. I already have a method in my dao class that is doing the insert, which I will be calling in my JUnit test case. Any idea on how to do it only if a record doesn't exist? Is there something in JUnit like there is assertNotNull, assertTrue that does insert only if the record doesn't exist?

Upvotes: 1

Views: 6920

Answers (2)

Tom Jonckheere
Tom Jonckheere

Reputation: 1648

I would check in my unit test if the record exists, if it doesn't exists I would insert it and verify (assert) that the record is inserted.

Pseudo code:

@Test
public void testInsert(){
Record record = ... // creation of the record
assertNull(recordDao.checkRecordExists(record)) // fails if there is a record
recordDao.insertRecord(record);
assertEquals(record, recordDao.fetchRecord(record)); // fetch record by Id and assert it eguals the inserted record.
}

Upvotes: 1

hankd
hankd

Reputation: 649

Make two tests: one where the record does not exist, and assert that it was inserted.

Make another where the record does exist, and assert that the insert did not happen.

Upvotes: 0

Related Questions