One of the crucial attention-grabbing learnings I had within the outdated SEA Tecnologia is the creation of check utilities .
Take a look at utilities are a strategy to reuse code in unit assessments. Normally, that is performed utilizing setUp
or @Earlier than
strategies, however this has some disadvantages. For instance, in a check case, we are able to have the next initialization:
personal Handle deal with;
personal AddressDAO addressDAO;
@Earlier than
public void setUp() {
deal with = new Handle();
deal with.setStreet("Rua fulano");
deal with.setNumber("123/A");
addressDAO = new AddressDAO();
}
This initialization works properly within the check under…
@Take a look at
public void testGetAllAddresses(){
addressDAO.addAddress(deal with);
Listing<Handle> addresses = addressDAO.getAllAddresses();
assertEquals(1, addresses.measurement());
assertEquals("Rua fulano", addresses.get(0).getStreet());
assertEquals("123/A", addresses.get(0).getNumber());
}
Nonetheless, within the following check, the created object is a waste, just isn’t used in any respect:
@Take a look at
public void testGetNoAddress() {
Listing<Handle> addresses = addressDAO.getAllAddresses();
assertEquals(0, addresses.measurement());
}
Within the subsequent following check, we’ve code redundancy. We additionally should determine whether or not the opposite object needs to be created within the @Earlier than
methodology or within the check methodology:
@Take a look at
public void testGetAllAddressesMoreThanOne() {
addressDAO.addAddress(deal with);
Handle address2 = new Handle();
address2.setStreet("Outra rua");
address2.setNumber("111");
addressDAO.addAddress(address2);
Listing<Handle> addresses = addressDAO.getAllAddresses();
assertEquals(1, addresses.measurement());
assertEquals("Rua fulano", addresses.get(0).getStreet());
assertEquals("123/A", addresses.get(0).getNumber());
}
These inconveniences are minor when in comparison with the duty of making a community of dependencies. For instance, to check a category Particular person
that provides a check case Handle
in one other check case, we should have one @Earlier than
just like this:
personal Particular person particular person;
personal Handle deal with;
personal PersonDAO personDAO;
@Earlier than
public void setUp() {
deal with = new Handle();
deal with.setStreet("Rua fulano");
deal with.setNumber("123/A");
particular person = new Particular person();
particular person.setName("João");
particular person.setAddress(deal with);
personDAO = new PersonDAO();
}
The code for creating addresses was duplicated, and it’s tough to create the dependencies. In these examples, we see easy circumstances, however it’s simple to see how the scenario would get difficult.
We clear up this drawback by writing a category to create these objects. This class can be one thing like this:
public class TestUtil {
public static Handle utilCreateAddress(String avenue, String quantity) {
Handle deal with = new Handle();
deal with.setStreet("Rua fulano");
deal with.setNumber("123/A");
return deal with;
}
public static Particular person utilCreatePerson(String title, Handle deal with) {
Particular person particular person = new Particular person();
particular person.setName(title);
particular person.setAddress(deal with);
return particular person;
}
}
Our check circumstances prolonged TestUtil
, making object creation simpler:
public class TestAddress2 extends TestUtil {
personal AddressDAO addressDAO = new AddressDAO();
@Take a look at
public void testGetAllAddresses() {
Handle deal with = utilCreateAddress("Rua fulano", "123/A");
addressDAO.addAddress(deal with);
Listing<Handle> addresses = addressDAO.getAllAddresses();
assertEquals(1, addresses.measurement());
assertEquals("Rua fulano", addresses.get(0).getStreet());
assertEquals("123/A", addresses.get(0).getNumber());
}
@Take a look at
public void testGetNoAddress() {
Listing<Handle> addresses = addressDAO.getAllAddresses();
assertEquals(0, addresses.measurement());
}
@Take a look at
public void testGetAllAddressesMoreThanOne() {
Handle deal with = utilCreateAddress("Rua fulano", "123/A");
Handle address2 = utilCreateAddress("Outra rua", "111");
addressDAO.addAddress(deal with);
addressDAO.addAddress(address2);
Listing<Handle> addresses = addressDAO.getAllAddresses();
assertEquals(2, addresses.measurement());
assertEquals("Rua fulano", addresses.get(0).getStreet());
assertEquals("123/A", addresses.get(0).getNumber());
}
}
As we additionally ceaselessly wanted some particular object with only one or two parameters to be outlined, we created strategies variants:
public static Handle utilCreateAddress() {
return utilCreateAddress("Qualquer", "Qualquer");
}
public static Particular person utilCreatePerson() {
return utilCreatePerson("José", utilCreateAddress());
}
We discovered that in a considerably complicated challenge, with giant networks of object dependencies. Using these check utilities made it attainable to observe TDD on the system. It was thrilling to find that, to create that doc that relied on seven different paperwork and 5 – 6 customers, all you needed to do was name a way.
In fact, there may be extra to our testing utilities than has been written right here, and there could also be much more that we have not even performed. (For instance, it could be attention-grabbing to jot down check utilities for particular courses, as a substitute of 1 gigantic utility.) Nonetheless, as the thought may be very easy, we hope this primary step motivates you to consider the subject. Till later!
(This put up was initially printed as “Test Utilities, or set-up methods considered harmful” in Suspension of Disbelief. It was initially posted in an former employer’s blog. As the unique put up went offline however the matter stays related, I made a decision to republish it.)