What would the junit test be when i have the following method:
@Override
public void saveLastSuccesfullLogin(final User user) {
gebruiker.setLastLogin(new Date());
storeUser(user);
}
submethode storeUser:
@Override
public void storeUser(final User user) {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.merge(user);
em.getTransaction().commit();
em.close();
}
The problem i have is the date, being set for the entity user and then stored. Im using junit and easymock.
Try pulling the new Date() into a method with default access specifier like below
@Override
public void saveLastSuccesfullLogin(final User user) {
gebruiker.setLastLogin(getDate());
storeUser(user);
}
Date getDate() {
return new Date();
}
In your test class override the class as below using a mock or stubbed date.
<ClassUnderTest> classUnderTest = new <ClassUnderTest> () {
@Override
Date getDate() {
return mockDate;
}
}
In this way you can assert the date value easily as it is going to be stubbed out.
What's the problem with the Date? That you don't know what it is to assert later? A few alternatives:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With