Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Easy testing with EasyMock (Java 5) Ürgo Ringo Aqris Software
Developer tests - big theory <ul><li>Why/when </li><ul><li>short feedback loop </li></ul><li>Classic vs mock testing </li>...
EasyMock mock types (1) <ul><li>normal mocks </li><ul><li>expect – replay – verify </li></ul></ul>@ Before   public   void...
EasyMock mock types (2) <ul><li>strict mocks </li><ul><li>checks order of calls
useful if test contains many different or complex scenarios (bad style?)  </li></ul></ul>userDao  = EasyMock. createStrict...
EasyMock mock types (3) <ul><li>nice mocks </li><ul><li>useful if default return value is OK
Warning: are you in “getting it pass” mode?
if cannot implement nice equals for some argument (but there is better alternative!) </li></ul></ul>filterDao  = createNic...
Argument matchers <ul><li>when to use </li><ul><li>if default equals based check doesn't work </li></ul><li>predefined mat...
aryEq(X value)
Upcoming SlideShare
Loading in …5
×

Unit testing with Easymock

5,441 views

Published on

Published in: Economy & Finance
  • Be the first to comment

Unit testing with Easymock

  1. 1. Easy testing with EasyMock (Java 5) Ürgo Ringo Aqris Software
  2. 2. Developer tests - big theory <ul><li>Why/when </li><ul><li>short feedback loop </li></ul><li>Classic vs mock testing </li><ul><li>http://martinfowler.com/articles/mocksArentStubs.html </li></ul><li>Failing legacy tests </li><ul><li>check if can be fixed; if not separate from working ones </li></ul></ul>
  3. 3. EasyMock mock types (1) <ul><li>normal mocks </li><ul><li>expect – replay – verify </li></ul></ul>@ Before public void setUp() { userDao = EasyMock. createMock (UserDao. class ); validator = new UserValidator( userDao ); } @ Test public void duplicateLoginNameIsInvalid() throws Exception { User user = ... errors = new BindException(user, &quot;User&quot; ); EasyMock.expect( userDao .isValidLoginName(user)).andReturn( false ); EasyMock.replay( userDao ); validator .validate(user, errors); assertTrue(errors.toString(), errors.hasErrors()); EasyMock.verify( userDao ); }
  4. 4. EasyMock mock types (2) <ul><li>strict mocks </li><ul><li>checks order of calls
  5. 5. useful if test contains many different or complex scenarios (bad style?) </li></ul></ul>userDao = EasyMock. createStrictMock (UserDao. class );
  6. 6. EasyMock mock types (3) <ul><li>nice mocks </li><ul><li>useful if default return value is OK
  7. 7. Warning: are you in “getting it pass” mode?
  8. 8. if cannot implement nice equals for some argument (but there is better alternative!) </li></ul></ul>filterDao = createNiceMock(FilterDao. class ); //expect(filterDao.getFilterId(&quot;unique-filter-name&quot;, USER_ID)).andReturn(null); replay( filterDao ); validator.validate(filter, errors); assertFalse(errors.hasErrors()); verify( filterDao );
  9. 9. Argument matchers <ul><li>when to use </li><ul><li>if default equals based check doesn't work </li></ul><li>predefined matchers </li><ul><li>eq(X value)
  10. 10. aryEq(X value)
  11. 11. isNull(), notNull()
  12. 12. ... </li></ul></ul>
  13. 13. Custom argument matchers <ul><li>useful if object equals doesn't work </li></ul>mockSender .send(eqMessageText( simpleMessage )); replay ( mockSender ); sender .sendAccountApprovedNotification( user , password); verify ( mockSender ); private SimpleMailMessage eqMessageText( final SimpleMailMessage expected) { EasyMock. reportMatcher ( new IArgumentMatcher() { public void appendTo(StringBuffer buffer) { } public boolean matches(Object argument) { SimpleMailMessage actual = (SimpleMailMessage) argument; assertEquals (expected.getText(), actual.getText()); assertEquals (expected.getSubject(), actual.getSubject()); assertEquals (expected.getFrom(), actual.getFrom()); assertEquals (expected.getTo()[0], actual.getTo()[0]); return true ; } }); return expected; }
  14. 14. D efine answer at runtime <ul><li>IAnwer interface </li></ul>@Test public void showErrorsOnPage1() throws Exception { StepHandler stepHandler = createMock(StepHandler.class); ... stepHandler.validate( eq ( step0 .getModel()), (Errors) notNull ()); expectLastCall ().andAnswer( new IAnswer<Object>() { public Object answer() { Errors errors = ((Errors) getCurrentArguments ()[1]); errors.reject( &quot;Error occurred in one step&quot; ); return null ; } }); replay (stepHandler); ... verify (stepHandler); }
  15. 15. EasyMock more exotic stuff <ul><li>for implementing stubs </li><ul><li>asStub() / asStubReturn()
  16. 16. asStubAnswer </li></ul><li>mock only some methods </li><ul><li>createMock(Class<T>, Method[]) </li></ul></ul>
  17. 17. EasyMock meets Unitils import static org.unitils.easymock.EasyMockUnitils.*; public class MockingTest extends UnitilsJUnit4 { @Mock private UserDao userDao ; @ Before public void setUp() { //userDao = createMock (UserDao. class ); validator = new UserValidator( userDao ); } @ Test public void duplicateLoginNameIsInvalid() throws Exception { User user = ... errors = new BindException(user, &quot;User&quot; ); expect( userDao .isValidLoginName(user)).andReturn( false ); //replay(userDao); replay (); validator .validate(user, errors); assertTrue(errors.toString(), errors.hasErrors()); //verify(userDao); verify (); } } <ul><li>http://www.unitils.org/ </li></ul>

×