java - One unit per case or one unit per assert(s) -
in unit-tests should write 1 unit per case or 1 unit per assert back upwards cost point of view? have next code
void methodundertest(resource resource) { if(!resource.hasvalue()) { value value = valueservice.getvalue(resource); resource.setvalue(value); } // resource.setlastupdatetime(new date()); // added in future db.persist(resource); email.send(resource); }
the commented line added in near future , think cost update units.
as far see there 2 ways test code.
write 2 units passing resource value , without value. in both tests verify db.persist , email.send called. when lastupdatetime added i'll have update both tests verify property set.
write separate unit tests: 1 checks db.persist called, other checks email.send, 3rd , 4th resource , without value. when lastupdatetime added write new unit.
i sec way because thought won't have touch working units. lot of code duplication because 4 tests same , utilize different asserts.
the first approach looks more right 'just 1 concept per unit test' point of view. aren't such tests hard maintain? adding new have revise existing tests , doesn't sound good.
is there best practise here should follow?
i propose set mutual stuff of tests setup() tests, have 1 assert per unit test, in sec way of testing.
when add together new line of code, add together 1 more test case single assert.
not modification of existing tests, not code duplication.
java unit-testing
No comments:
Post a Comment