Print
InjectionExample

In PicoUnit when you need the service of another component you obtain that component by declaring a dependancy on it, much like in Constructor Dependancy Injection.

Here's an example test that is verifying a database persistence layer. The tests makes use of two injected dependancies, a Database and a verification object: Verify.

package example.dependancy;

import example.model.Database;
import example.model.Person;
import example.model.PersonMapper;

import picounit.Test;
import picounit.Verify;

public class PersonMapperTest implements Test {
  private final PersonMapper personMapper;

  private final Database database;

  public PersonMapperTest(Database database) {
    this.personMapper = new PersonMapper(database);

    this.database = database;
  }

  public void testSavingAPersonAddsOneMorePersonToTheDatabase(Verify verify) {
    Person fred = new Person("Fred Dibner");

    int previousPopulation = countPeople();

    personMapper.save(fred);

    verify.equal(previousPopulation + 1, countPeople());
  }

  private int countPeople() {
    return database.queryCount("select count(*) from people");
  }
}

The test declares the two dependancies in two locations, in the constructor and in the test method, there are two additional locations that can be used too: setUp and tearDown.

This test is a example of an integration test, PicoUnit can also be used to perform Collaboration Tests

Powered by Atlassian Confluence