effort • Integrates into JUnit test cases • Test driven development • Achieve high code/branch coverage • Cobertura and other plugins Thursday, August 29, 13
for certain scenarios on the Mocked Object • Validate the expected behaviors • Enables granular testing • Can achieve 100% source & branch coverage Thursday, August 29, 13
Useful for commonly used mocks. // Reset between tests. simple = EasyMock.createMock(SimpleClass.class); } @Test public void testMethod() { // Mock specific to this test only Complex complex = EasyMock.createMock(Complex.class); ... } Thursday, August 29, 13
{ Complex complex = createMock(Complex.class); // 1) Simple expect - setup() is a void method complex.setup(); expectLastCall(); // Optional for a single call // 2) Back-to-back setup calls complex.setup(); complex.setup(); complex.setup(); // 3) Simplified version of 2) - they are equivalent complex.setup(); expectLastCall().times(3); } Thursday, August 29, 13
though should be required!! • Without, not all expectations have to be met for tests to pass • Inadvertently miss intended expects • Between each @Test mocks are reset Thursday, August 29, 13
mocked methods? • Specify a .equals() equivalent value • Matchers • aryEq(someArray), isA(xxx.class) • eq(xxx), anyPrimitive() - (int, long, boolean) “If you would like to use matchers in a call, you have to specify matchers for all arguments of the method call.” Thursday, August 29, 13
Inherit from testing class • @Override methods • Highly recommended for Groovy • Meta classing issues • Can specify method/constructor params, etc. Thursday, August 29, 13
constructor, large Map, etc. • Provides better checking than isA(xxx.class), anyXXX() ... • Don’t have to @Override a method call to grab its value (Remember Partial Mocks ex?) Thursday, August 29, 13
Connection connection = createMock(Connection.class); // Capturing a Map with many values, too hard to construct Capture<Map<String, String>> myCapture = new Capture<Map<String, String>>(); // Mocks method ‘boolean exchangeState(String, Map); expect(connection.sendResult(EasyMock.capture(myCapture)).andReturn(true); replay(complex); new MyTest().save(connection); // Validate contents of the capture Map<String, String> capturedMap = capture.getValue() Assert.assertEquals(5, capturedMap.size()); Assert.assertEquals(“Michie”, capturedMap.get(“Ken”); ... verify(complex); } Thursday, August 29, 13
Static methods/initializers • Constructors • Final classes and methods • Private methods • PLUS it integrates seamlessly into EasyMock! Thursday, August 29, 13
tests • Saves you time later! Promise! • Consolidate common setup methods • @Before is a nice place for Mocks/excepts • You can reset mocks in EasyMock Thursday, August 29, 13