Question: Is there anything you could do in order to improve the test?
I am using Spring Framework 4, Hibernate 4, JUnit 4. All DAO unit tests inherit from TestDaoSetup class:
@ContextConfiguration(locations={
"classpath:/config/test/spring.xml",
"classpath:/config/test/hibernate.xml"
})
@TransactionConfiguration
@Transactional
public class TestDaoSetup {
}
One of unit tests:
@RunWith(SpringJUnit4ClassRunner.class)
public class CityDaoImplTest extends TestDaoSetup {
@Rule
public ExpectedException exception = ExpectedException.none();
@Autowired
private CityDao cityDao;
@Autowired
private CountryDao countryDao;
@Test
public void create_Created(){
CountryEntity countryEntity = countryDao.find(1L);
CityEntity cityEntity = new CityEntity();
cityEntity.setName("Košice");
cityEntity.setCountryEntity(countryEntity);
cityDao.create(cityEntity);
Assert.assertNotNull("Expected not null value.",cityDao.find(cityEntity.getId()));
}
@Test
public void create_NoCountryAssociation_ExceptionThrown(){
exception.expect(ConstraintViolationException.class);
CityEntity cityEntity = new CityEntity();
cityEntity.setName("Košice");
cityDao.create(cityEntity);
cityDao.flush();
}
@Test
public void create_ExistingNameSameCountry_ExceptionThrown() {
exception.expect(ConstraintViolationException.class);
CountryEntity countryEntity = countryDao.find(1L);
CityEntity cityEntity = new CityEntity();
cityEntity.setName("Bratislava");
cityEntity.setCountryEntity(countryEntity);
cityDao.create(cityEntity);
cityDao.flush();
}
@Test
public void create_ExistingNameDifferentCountry_Created(){
CountryEntity countryEntity = countryDao.find(2L);
CityEntity cityEntity = new CityEntity();
cityEntity.setName("Bratislava");
cityEntity.setCountryEntity(countryEntity);
cityDao.create(cityEntity);
Assert.assertNotNull("Expected not null value.", cityDao.find(cityEntity.getId()));
}
@Test
public void update_ExistingNameSameCountry_ExceptionThrown(){
exception.expect(ConstraintViolationException.class);
CityEntity cityEntity = cityDao.find(2L);
cityEntity.setName("Bratislava");
cityDao.update(cityEntity);
cityDao.flush();
}
@Test
public void update_ExistingNameDifferentCountry_Updated(){
CityEntity cityEntity = cityDao.find(3L);
cityEntity.setName("Bratislava");
cityDao.update(cityEntity);
Assert.assertEquals("Expected different value.", "Bratislava", cityDao.find(3L).getName());
}
@Test
public void delete_NotUsed_Deleted() {
CityEntity cityEntity = cityDao.find(4L);
cityDao.delete(cityEntity);
Assert.assertNull("Expected null value.", cityDao.find(cityEntity.getId()));
}
@Test
public void delete_Used_ExceptionThrown(){
exception.expect(ConstraintViolationException.class);
CityEntity cityEntity = cityDao.find(1L);
cityDao.delete(cityEntity);
cityDao.flush();
}
}
Question: Is there anything you could improve in the test?
EDIT:
To make test conditions more clear:
- There are separated configurations, one is specifically for testing purposes.
- There is database script which prepopulates the test database with test data before tests actually start.
- Data are the same for each test, because there is a rollback after each test.
- Test data are not prepopulated in each test class, but they are prepopulated in separate SQL file, which is executed before tests actually start.