testing - Dealing with code duplication in tests -


at work practice test driven development as possible. 1 thing end in though having set bunch of dto's , when these have complex structure becomes quite alot of code. problem code quite repettetive , feel distracts main purpose of test. instance using contrieved , condensed example (in java, junit + mockito):

class bookingservicetest {      private final static int hour_in_millis = 60 * 60 * 1000;     private final static int location_id = 1;     @mock     private bookinglocationdao bookinglocationdao;      @injectmocks     private bookingservice service = new bookingservice();      @test     public void yieldsawarningwhenabookingoverlapsanotherinthesamelocation() {         // part bit repetetive on many tests:         date = new date()         location location = new location()         location.setid(location_id);          booking existingbooking = new booking()         existingbooking.setstart(now);         existingbooking.setduration(hour_in_millis);         existingbooking.setlocation(location);         // here          when(bookinglocationdao.findbookingsatlocation(location_id))             .thenreturn(arrays.aslist(existingbooking));          // again setting booking :\         booking newbooking = new booking();         newbooking.setstart(now);         newbooking.setduration(hour_in_millis / 2);         newbooking.setlocation(location);                          // actual test...         bookingresult result = service.book(newbooking);          assertthat(result.getwarnings(), hassize(1));         assertthat(result.getwarnings().get(0).gettype(), is(bookingwarningtype.overlaping_booking));     }  } 

in example setup not complicated wouldn't think of much. however, when more complicated input required code setup of input methods tend grow. problem gets exacerbated similar input being used in several tests. refactoring setup code separate testutil class helps bit. problem is bit hard find these utility classes when writing new tests couple of month's later, leads duplication.

  1. what way of dealing kind of "complex" dtos in order minimize code duplication in test setups?
  2. how ensure extracted testutilities found when working similar code?
  3. am doing wrong? :) should build software in way avoid situation altogether? if so, how?

as erik rightly points out used patterns solve testdatabuilder , objectmother. these covered in depth in: mark seemans advanced unit testing course growing object oriented software guided tests, both good.

in practice find test data builder pattern leads better, more readable tests objectmother pattern except in simplest cases (as need surprising number of overloads objectmother pattern).

another trick can use bunch sets of setups test object builder pattern single methods e.g.

invoice invoicewithnopostcode = new invoicebuilder()     .withrecipient(new recipientbuilder()         .withaddress(new addressbuilder()             .withnopostcode()             .build())         .build())     .build(); 

could become:

new invoicebuilder().withnopostcode().build(); 

and in cases can lead simpler test setups, doesn't work in cases.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -