Easymock

1,571 views
1,309 views

Published on

Published in: Technology, Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,571
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
69
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Easymock

  1. 1. EasyMock Mock object framework for java development By Franck Benault Created 16/11/2014 Last updated 11/11/2014
  2. 2. EasyMock plan
  3. 3. Unit test in java ● Main framework : Junit and TestNG ● Database management (fill or check content) – DbUnit – DbSetup ● Mock object frameworks – EasyMock – Mockito
  4. 4. Goal of EasyMock ● On the web site of EasyMock (www.easymock.org) – Great (unit) testing includes isolation – Isolation involves mock objects – EasyMock makes mocking easier
  5. 5. What is a mock
  6. 6. Mock objects ● Unit tests are white box tests – The developers is allowed to look at the code ● Mock objects replaces the objects linked to the object we want to test ● It becomes possible to test more path (exceptions, errors) ● We have to change a little the implementation to make the mocking easier
  7. 7. Mock objects ● Unit tests are white box tests – The developers is allowed to look at the code ● Mock objects replaces the objects linked to the object we want to test ● It becomes possible to test more path (exceptions, errors) ● We have to change a little the implementation to make the mocking easier
  8. 8. Features of Mock frameworks ● Main features – Value returning for a stubbed method – Exception throwing for a stubbed method – Invocation check method – Method arguments check – Invocation order for one mock check ● Advance features – Stubbing of equals() and hashCode() methods – ...
  9. 9. Features of Mock frameworks ● Main features – Value returning for a stubbed method – Exception throwing for a stubbed method – Invocation check method – Method arguments check – Invocation order for one mock check ● Advance features – Stubbing of equals() and hashCode() methods – ...
  10. 10. Main mock frameworks (in java) ● Open projects well know – EasyMock – Jmockit – Mockito – Jmock
  11. 11. EasyMock live cycle of mock objects ● Create a mock ● Set up your expectation ● Set the mock to replay mode – Call your code under test ● Verify that your expectation have been set
  12. 12. EasyMock create mock (strict of nice) ● Create a mock – CreateNiceMock() ● Unexpected returns an empty value (0 or null) – CreateMock() ● The order of the call is not checked – CreateStrictMock() ● The order of the call is checked
  13. 13. EasyMock expection/replay/verify EntityManager emMock = EasyMock.createNiceMock(EntityManager.class); EntityTransaction entityTransactionMock = EasyMock.createNiceMock(EntityTransaction.class); EasyMock.expect(emMock.getTransaction()).andReturn(entityT ransactionMock).times(2); EasyMock.replay(entityTransactionMock); EasyMock.verify(entityTransactionMock);
  14. 14. EasyMock How to inject the mock StudentManager StudentQuery
  15. 15. EasyMock How to inject the mock @RunWith(EasyMockRunner.class) public class StudentManagerMockTest { @TestSubject //where to inject mocks private static StudentManager studentManager; @Mock(type = MockType.NICE) //mock to inject private StudentQuery studentQueryMock = EasyMock.createNiceMock(StudentQuery.class); .../...

×