easymock 101

20
EasyMock 101 A 30 Minute Review by Matthew McCullough

Upload: matthew-mccullough

Post on 13-Jan-2015

3.740 views

Category:

Education


3 download

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

Page 1: Easymock 101

EasyMock 101A 30 Minute Review by Matthew McCullough

Page 2: Easymock 101

❶ What Is Mocking?

Page 3: Easymock 101

• A fake object

• Allows for more modular testing

• Records and verifies expectations of calls

Are You Mocking Me?

Page 4: Easymock 101

❷ Creating A Mock

Page 5: Easymock 101

IEnumeration<Short> iEnumIntfMock = EasyMock.createMock(EnumeratedAttribute.class);

Page 6: Easymock 101

❸ Expectations

Page 7: Easymock 101

• 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

Page 8: Easymock 101

EasyMock.expect(iEnumIntfMock.getNumberOfRows());

Expect A Call

Page 9: Easymock 101

EasyMock.expect(iEnumIntfMock.getNumberOfRows()).andReturn(5);

Expect A Return

Page 10: Easymock 101

❹ Replaying

Page 11: Easymock 101

• Prime the pump to walk through expected call sequence

• Provide return values in same sequence

• Listen to usage of call parameters

Replay

Page 12: Easymock 101

EasyMock.replay(iEnumIntfMock);

Replay Kickoff

Page 13: Easymock 101

❺ Using The ClassUnderTest

Page 14: Easymock 101

• Call all functions as you normally would

• Pass in mock where injectable

• Consider how mock can be passed into internal references

Testing

Page 15: Easymock 101

int numOfRows = iEnumIntfMock.getNumberOfRows();

Assert.assertTrue("Number of rows does not match", numOfRows > 0);

Testing

Page 16: Easymock 101

❻ Verifying

Page 17: Easymock 101

• Called after all operations with the mock are done

• Compares the usage to the recording

Page 18: Easymock 101

EasyMock.verify(iEnumIntfMock);

Verifying

Page 19: Easymock 101

❼ Resources

Page 20: Easymock 101

• EasyMock.org

• Groovy Mockinghttp://docs.codehaus.org/display/GROOVY/Groovy+Mocks

Resources