Print
MockingExample

PicoUnit mocking approach combines many of the best features of JMock and EasyMock.

PicoUnit attempts, like JMock, to express expectations in a form that can be read as English sentences, PicoUnit also expresses those expectations in a type safe way, as does EasyMock.

PicoUnit injects mock collaborators in much the same way it injects ordinary dependancies: through method parameters.

package example.collaborator;

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

import picounit.Mocker;
import picounit.Test;

public class PersonMapperTest implements Test {
  private PersonMapper personMapper;

  private Database mockDatabase;

  public void mock(Database mockDatabase) {
    this.personMapper = new PersonMapper(mockDatabase);

    this.mockDatabase = mockDatabase;
  }

  public void testSavingAPersonInsertsARowIntoPeopleTable(Mocker should) {
    should.call(mockDatabase.insert("insert into people (name) values('Fred Dibner')"))
      .andReturn(true)
      .because("The PersonMapper should use a Database to save a Person");

    should.performAboveWhen();

    Person fred = new Person("Fred Dibner");
    personMapper.save(fred);
  }
}

In the example above the test declares a dependancy on a Mock Database by specifying a Database parameter in the mock method. The Test also declares a dependancy on a 'Mocker' service, this service is used to express expectations against the injected mock collaborators.

Powered by Atlassian Confluence