Minimalistic Java mocking framework
Java
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.settings
src
.classpath
.gitignore
.project
LICENSE
README.md
pom.xml

README.md

DIY-mock

Minimalistic Java mocking framework

DIYMock mocker = new DIYMock();

interface Car {
  public String drive();
  public String park();
}

Car car = new Car() {
 public String drive() {
   return "rolling...";
 }

 public String park() {
   return "stalled.";
 }
};

// Original
assertTrue("rolling...".equals( car.drive()));
assertNotNull(car.park());
// Create a mock, for now all methods return null
Car carMock = mocker.mock(Car.class);
// Intercept method .drive() and make it return 'proxy roll!'
mocker.intercept("drive", "proxy roll!");
// Confirm the result of method drive
assertTrue("proxy roll!".equals(carMock.drive()));
// Method park returns null because it was not intercepted or delegated.
assertNull(carMock.park());

// Delegate method calls to the object car
mocker.delegate(Car.class, car);
// Now park() returns 'stalled.' because it was delegated to car.park()
assertTrue("stalled.".equals(carMock.park()));
// The interception prevails over delegation
assertTrue("proxy roll!".equals(carMock.drive()));