Monday, September 17, 2012

EasyMock Partial Mocks FTW. (in progress)

Ever wanted to mock certain methods in a class but not all?
Unit Testing in Java got a whole lot easier when I discovered this trick.


ClassToMock classToMockPartialMock = EasyMock
.createMockBuilder(ClassToMock.class) //create builder first
.addMockedMethod("meth1")
.addMockedMethod("meth2")
//note unspecified methods are not mocked and can be called directly!
.addMockedMethod("meth4")
.createMock();

Simple and no extensions necessary.

No comments:

Post a Comment