patternjavaMinor
JUnit Assert.assertSame
Viewed 0 times
assertjunitassertsame
Problem
I am using JUnit, Hibernate and Spring Framework. I wrote a simple test to test whether entity was updated correctly or not.
Is this correct way to test if entity was updated? Could it be done better?
Is this correct way to test if entity was updated? Could it be done better?
@Test
public void testUpdated(){
ConfirmationEntity confirmationEntity = confirmationDao.find(1L);
confirmationEntity.setConfirmationString("0123456789");
confirmationEntity.setConfirmationStringValidUntil(new LocalDateTime());
confirmationDao.update(confirmationEntity);
ConfirmationEntity confirmationEntityUpdated = confirmationDao.find(1L);
Assert.assertSame(
"Expected values to be the same.",
confirmationEntity,
confirmationEntityUpdated
);
}Solution
This test, on its own, is not enough. Additionally, the test is called
What it does is:
At no point in this process have you actually tested that the values you think you changed on the object, actually did change.
Additionally, you never tested whether those changes were persisted to the database/store.
What I would expect a test like this to do, is to:
That is a lot of things to change, which means the test, on it's own, is not enough, and should be included with a bunch of other tests, each one testing different parts of the process.
testUpdated, but it does not actually test that the value is updated.What it does is:
- find an object based on a key value
- it modifies some non-key content
- it 'persists' the changes
- it finds an object for the same key as before
- it tests that the object it found the second time is the identical object to the first.
At no point in this process have you actually tested that the values you think you changed on the object, actually did change.
Additionally, you never tested whether those changes were persisted to the database/store.
What I would expect a test like this to do, is to:
- get the object using the key
- make the changes
- assert that the values have changed to the new values.
- persist the changes
- assert that a persist happened (some timestamp in the database maybe)?
- use an independent mechanism to check that the values in the database match the values in the object (a different database connection, a different DAO instance, something).
- ensure that finding the same key value finds the same instance as before.
That is a lot of things to change, which means the test, on it's own, is not enough, and should be included with a bunch of other tests, each one testing different parts of the process.
Context
StackExchange Code Review Q#49771, answer score: 4
Revisions (0)
No revisions yet.