I have been looking for a refined way to create integration tests for DAO layer (Java, Spring, Hibernate), mostly for generic CRUD operations (readAll(), getById(), insert(), update(), delete(). etc.), for several months. What I've found so far is either 1) too laborious approach with the amount of code comparable with real source code or 2) Too complicated/unreadable/inflexible approach to do it. One attempt that I've undertaken so far is the following: If we have say generic DAO interface like this one:
public interface GenericDAO<T> {
Long insert(T t);
void update(T t);
List<T> getAll();
T getById(Long id);
void delete(T t);
}
Then we could propose some template method (because all the tests are performed randomly by default) to perform all those CRUD oeration, which is applicable to every class:
@Transactional
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/test-context.xml"})
public abstract class AbstractDAOTest {
@Test
public void test() {
insert();
read();
readAll();
update();
delete();
}
protected abstract void init();
protected abstract void insert();
protected abstract void read();
protected abstract void readAll();
protected abstract void update();
protected abstract void delete();
}
Then we have Spring's test config with sample data and implement all these methods in real class-successor.
This is still not nice to me. (only one actual method to test, time-consuming to create test-context, etc.)
So, what is the most recognisible and triend-and-true approcah to create integration tests for DAO layer? Thank you in advance.
Aucun commentaire:
Enregistrer un commentaire