java - Creating test data: domain builder -
i want create test data tests underlying in-memory database. common approach create test_data.sql file , create test objects inserts. , reference these objects in java test.
i read in growing object-oriented software, guided tests, tests don't need go deep in details. example, if test wants existing user new status, don't need create user , fill it's fields. should tell, wants user new status , other fields should filled default values.
so want helper methods, one:
user user = user(userstatus.new); // inserts user in database // ... use persistent user instance in test
but database schema has tables , came following code:
domain domain = new domainbuilder().user(userstatus.new).agreement().account().account().build();
doman class:
public class domain { private user user; private agreement agreement; private account account; // getters/setters }
this code creates user, agreement(with fk user), 2 accounts(with fk agreement) , returns domain object, contains these entities.
so code can setup test data particular test in 1 line.
this test data generation approach has following advantages on sql:
i don't stick table columns, if column/constraint added/removed , don't need change test data generation in test.
it's more concise way, sql: can setup test data in 1 line whereas sql approach requires writing insert each object.
it's less time consuming. yes, first start using approach have spend time implementing such domain builder, once done, save time.
centralized objects creation. if database schema changed, have change object creation logic in 1 place.
you don't need stick java constants user_with_new_status_id.
my question is:
does come approach , libraries/tools/conventions use ?
update: use such approach in few test types:
repositories tests (e.g. repository filters objects correctly, or returns objects list in correct order).
business logic + database integration test(unit tests good, need 100% sure business logic , repository logic consistent
have checked dbunit?
it extension of junit targeted database-driven projects that, among other things, puts database known state between test runs.
the data set simple xml file.
Comments
Post a Comment