easymock 101
DESCRIPTION
A quick review of the EasyMock mocking (testing) framework and how it can make your testing more productive and less tightly coupled by Matthew McCullough.TRANSCRIPT
EasyMock 101A 30 Minute Review by Matthew McCullough
❶ What Is Mocking?
• A fake object
• Allows for more modular testing
• Records and verifies expectations of calls
Are You Mocking Me?
❷ Creating A Mock
IEnumeration<Short> iEnumIntfMock = EasyMock.createMock(EnumeratedAttribute.class);
❸ Expectations
• Expect method is called on mock
• Expect sequence of method calls on mock(call createStrictMock())
• Expect parameters to methods calls on mock
Expecting A Lot
EasyMock.expect(iEnumIntfMock.getNumberOfRows());
Expect A Call
EasyMock.expect(iEnumIntfMock.getNumberOfRows()).andReturn(5);
Expect A Return
❹ Replaying
• Prime the pump to walk through expected call sequence
• Provide return values in same sequence
• Listen to usage of call parameters
Replay
EasyMock.replay(iEnumIntfMock);
Replay Kickoff
❺ Using The ClassUnderTest
• Call all functions as you normally would
• Pass in mock where injectable
• Consider how mock can be passed into internal references
Testing
int numOfRows = iEnumIntfMock.getNumberOfRows();
Assert.assertTrue("Number of rows does not match", numOfRows > 0);
Testing
❻ Verifying
• Called after all operations with the mock are done
• Compares the usage to the recording
EasyMock.verify(iEnumIntfMock);
Verifying
❼ Resources
• EasyMock.org
• Groovy Mockinghttp://docs.codehaus.org/display/GROOVY/Groovy+Mocks
Resources