0
TESTING WITH MOCKS                       Macon Pegram  Blog: http://blogs.captechconsulting.com/blog/author/Macon%20Pegram...
SOUND FAMILIAR?I’d rather not change that functionThis defect keeps coming backIt’s impossible to unit test our projectThe...
UNIT TESTING:        WHY BOTHER?Detect / Avoid defectsValidate Bug FixesRefactor without hesitationForces you to think abo...
WHAT DO WE TEST?Testing State  Does it have the expected values after execution?  Usually via “assertXXX”Verifying Behavio...
OBSTACLES TO          TESTINGMethod under test:  Has void return type  Runs slow  Has complicated dependencies (IE: EJB,  ...
TYPICAL SOLUTIONSRun Integration TestsIn container testingRoll your own “stub”Don’t test
DON’T BE THESE GUYS
TERMINOLOGYtest double - pretend object used for testing (encompassesall of the below)dummy - object passed around but not...
STUB VS. MOCKStubs  Preserve and test state  You write the  implementations.Mocks  Simulate Behavior  Validate behavior
BEHAVIOR MATTERSHow many of you haveworked with a system likethis?How many of you haveseen a method like this?Did you test...
WHY MOCK:              ISOLATIONApplication is complexReal object may be slowReal object is difficult to setupWriting isol...
WHY MOCK: PROJECT  CONSTRAINTSReal implementation not available  3rd party code  Not yet developedLimited time for test de...
MOCKS MAKE YOU        THINKI have to create a lot of mocks to test.  Class / method has too many  responsibilities?My Mock...
MOCKS MAKE YOU         THINKI can’t mock that dependency  Does your class have tightly bound  dependencies, instead of dep...
MOCKRUNNERDoes not require a container!Supports JavaEE 1.3 - 5  Servlets, filters, tags, JNDI, JMS, JDBC, EJBs  Does not s...
SOAP-UI “MOCKS”Generate web service stub from WSDL  Note: This is a stub.. not a mock!Use Groovy to script responsesExport...
EASYMOCKThe original dynamic mocking framework.Active developmentv3.0 added many features that addressed it’s shortcomings...
MOCKITONext Generation mocking frameworkAlready supports Hamcrest for argument matchers.No replayClaims to produce more “r...
EASYMOCK VS             MOCKITOEasymock  All interactions must be set as expectations  Explicit “replay” stepMockito  Mock...
POWERMOCKExtends EasyMock and MockitoUses custom classloaderMock: static methods, constructors, final methods/classes,and ...
ASIDE: HAMCRESTLibrary of Matchers  contains(), closeTo(), endsWith(), equalTo(),  typeCompatibleWith()Makes Assertions an...
NICE VS STRICT MOCKSNice Mocks:  Provides default behaviors/returns if  none defined for a method    IE: nulls for Objects...
STRICTS ARE BETTER(??)Developer is required to understand themethod being testedCreates the perception of a “brittle” test...
MOCK TESTING FLOWCreate your mocksDefine ExpectationsReset/Replay the MockRun your testVerification
CREATE MOCKS  Often done in @Before or setUp() methodpublic class AuthenticationFilterTest extends EasyMockSupport {	 priv...
SET EXPECTATIONSMethod and Input ParametersOutcomes           expect(mockFilterConfig.getInitParameter(eq("authenticationU...
REPLAY/TEST/VERIFY    Replay - Tell the mock you’re ready to test    Run your Test    Traditional Asserts and Mock Behavio...
SPYS / PARTIAL            MOCKSReal methods on class are used unlessexplicitly mockedUseful for:  3rd party libraries  Int...
BLACK OR WHITEBOX?Traditional Testing tests input/output ofpublics  How do you unit test a void?Verifying branching logic ...
TESTABLE DESIGNTrue? “Testable Design is Good Design”  Most tools don’t support testing private methods or final  classes....
LET’S LOOK AT SOMECODESimple Mock Setup:com.ctv.mocksamples.servlet.UserPrincipalRequestWrapperTestBasic Behavior/Assertio...
JAVA FRAMEWORKSMockrunner: http://mockrunner.sourceforge.net/SOAP-UI: http://soapui.org/EasyMock: http://easymock.org/Mock...
.NET FRAMEWORKSRhino Mocks:  http://www.ayende.com/projects/rhino-mocks.aspxEasyMock.net  http://sourceforge.net/projects/...
RESOURCES“Mocks aren’t Stubs” - Martin Fowler  http://www.martinfowler.com/articles/mocksArentStubs.htmlQuestioning “testa...
RESOURCESJava Mocking Framework Comparison  http://www.sizovpoint.com/2009/03/java-mock-  frameworks-comparison.htmlEasyMo...
RESOURCESxUnit concepts and definitions  http://xunitpatterns.com/Test%20Double.htmlStrict vs Nice Mocks  Pro Strict: http...
PRESENTATION CODEYou can pull down the code from this presentation onGithub  https://github.com/mpegram3rd/testing-with-mo...
Upcoming SlideShare
Loading in...5
×

Testing w-mocks

1,477

Published on

This is an introduction to unit testing using various mocking frameworks. It covers why you should consider using a mocking framework, how to apply mocks in your unit testing, and a high level comparison of various tools for mocking/stub creation. The distinction between mocks and stubs is clearly defined.

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

  • Be the first to like this

No Downloads
Views
Total Views
1,477
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
77
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Transcript of "Testing w-mocks"

    1. 1. TESTING WITH MOCKS Macon Pegram Blog: http://blogs.captechconsulting.com/blog/author/Macon%20Pegram Code: https://github.com/mpegram3rd/testing-with-mocks
    2. 2. SOUND FAMILIAR?I’d rather not change that functionThis defect keeps coming backIt’s impossible to unit test our projectThe backend/database is not availableSetting up my test environment takes too longI have a testing specific Spring context
    3. 3. UNIT TESTING: WHY BOTHER?Detect / Avoid defectsValidate Bug FixesRefactor without hesitationForces you to think about thedesignKeeps you out of thedebugger
    4. 4. WHAT DO WE TEST?Testing State Does it have the expected values after execution? Usually via “assertXXX”Verifying Behavior Did the Method Under Test do what I expected?
    5. 5. OBSTACLES TO TESTINGMethod under test: Has void return type Runs slow Has complicated dependencies (IE: EJB, Servlet APIs, Persistence APIs)Hard to Create scenarios (Network Exception)
    6. 6. TYPICAL SOLUTIONSRun Integration TestsIn container testingRoll your own “stub”Don’t test
    7. 7. DON’T BE THESE GUYS
    8. 8. TERMINOLOGYtest double - pretend object used for testing (encompassesall of the below)dummy - object passed around but not usedfake - simple working implementation of a dependencystub - canned answers to calls within tests.mock - objects pre-programmed with expectations. Used to verify “behavior” http://xunitpatterns.com/Test%20Double.html
    9. 9. STUB VS. MOCKStubs Preserve and test state You write the implementations.Mocks Simulate Behavior Validate behavior
    10. 10. BEHAVIOR MATTERSHow many of you haveworked with a system likethis?How many of you haveseen a method like this?Did you test it?Why not?
    11. 11. WHY MOCK: ISOLATIONApplication is complexReal object may be slowReal object is difficult to setupWriting isolated tests is hard
    12. 12. WHY MOCK: PROJECT CONSTRAINTSReal implementation not available 3rd party code Not yet developedLimited time for test developmentFluid Requirements
    13. 13. MOCKS MAKE YOU THINKI have to create a lot of mocks to test. Class / method has too many responsibilities?My Mocks need mocks Is the method too dependent on internal details?
    14. 14. MOCKS MAKE YOU THINKI can’t mock that dependency Does your class have tightly bound dependencies, instead of dependency injection? Are statics being used?
    15. 15. MOCKRUNNERDoes not require a container!Supports JavaEE 1.3 - 5 Servlets, filters, tags, JNDI, JMS, JDBC, EJBs Does not support EJB 3.0Supports Struts 1.1 - 1.3Manual Dependency Management :-(
    16. 16. SOAP-UI “MOCKS”Generate web service stub from WSDL Note: This is a stub.. not a mock!Use Groovy to script responsesExport as WAR file. These seem to work well for testing/code validation Does not hold up well under extended load!
    17. 17. EASYMOCKThe original dynamic mocking framework.Active developmentv3.0 added many features that addressed it’s shortcomingswhen compared with Mockito. Extend EasyMockSupport (replayAll(), verifyAll())Hamcrest argument matchers via bridge libraryGood documentation
    18. 18. MOCKITONext Generation mocking frameworkAlready supports Hamcrest for argument matchers.No replayClaims to produce more “readable” (BDD style) code andfailuresGreat documentation with examples
    19. 19. EASYMOCK VS MOCKITOEasymock All interactions must be set as expectations Explicit “replay” stepMockito Mocks are “nice” by default (verify what you want) No replay
    20. 20. POWERMOCKExtends EasyMock and MockitoUses custom classloaderMock: static methods, constructors, final methods/classes,and private methods
    21. 21. ASIDE: HAMCRESTLibrary of Matchers contains(), closeTo(), endsWith(), equalTo(), typeCompatibleWith()Makes Assertions and Mock argument matchers moreliterateWorks directly with Mockito argument matchersEasyMock supported via a bridge Matcher http://code.google.com/p/hamcrest/
    22. 22. NICE VS STRICT MOCKSNice Mocks: Provides default behaviors/returns if none defined for a method IE: nulls for Objects, 0 for numbersStrict Mocks: Undefined/expected behavior fails the test Variant: Order matters
    23. 23. STRICTS ARE BETTER(??)Developer is required to understand themethod being testedCreates the perception of a “brittle” testcase. This brittleness is a positive Failure is success!Nice Mocks allow tests to pass when theyshouldn’t. Strict Mock Semantics is the Only Way to Mock: http://bit.ly/bj4dSF
    24. 24. MOCK TESTING FLOWCreate your mocksDefine ExpectationsReset/Replay the MockRun your testVerification
    25. 25. CREATE MOCKS Often done in @Before or setUp() methodpublic class AuthenticationFilterTest extends EasyMockSupport { private AuthenticationFilter filter; private AuthenticationProvider mockAuthProvider; private FilterConfig mockFilterConfig; @Before public void setUp() throws Exception { mockFilterConfig = createMock(FilterConfig.class); mockAuthProvider = createMock(AuthenticationProvider.class); filter = new AuthenticationFilter(mockAuthProvider); }....}
    26. 26. SET EXPECTATIONSMethod and Input ParametersOutcomes expect(mockFilterConfig.getInitParameter(eq("authenticationURL"))) .andReturn(expectedURL); expect(mockFilterConfig.getServletContext()) Return values .andThrow (new ServletException("Something broke!")); expect(mockFilterConfig.getInitParameter(anyObject(String.class))) .andReturn(expectedURL) Exceptions .times(1,4);Invocation Count
    27. 27. REPLAY/TEST/VERIFY Replay - Tell the mock you’re ready to test Run your Test Traditional Asserts and Mock Behavior Validation// Prepare mocks for validationreplayAll(); // Execute testfilter.init(mockFilterConfig);// Traditional JUnit Assertion (with Hamcrest Matcher)assertThat(filter.getAuthenticationURL(), equalTo(expectedURL)); // Validates expected behavior of mocks occurred. If getInitParam() on the// mockFilter did not get called this would fail.verifyAll();
    28. 28. SPYS / PARTIAL MOCKSReal methods on class are used unlessexplicitly mockedUseful for: 3rd party libraries Interim refactoring of legacy codeThe need to do this more often than notsuggests design issues in the code.
    29. 29. BLACK OR WHITEBOX?Traditional Testing tests input/output ofpublics How do you unit test a void?Verifying branching logic in privatemethods is challenging from the publicmethod.Single public method calling multipleprivates can be hard to verify. http://www.quora.com/Should-you-unit-test-private-methods-on-a-class
    30. 30. TESTABLE DESIGNTrue? “Testable Design is Good Design” Most tools don’t support testing private methods or final classes. Should test tools govern your design? Should you write tests to safely refactor code or refactor code to be able to write tests?Perhaps: “Good Design is always Testable” http://blog.jayway.com/2009/04/01/questioning-
    31. 31. LET’S LOOK AT SOMECODESimple Mock Setup:com.ctv.mocksamples.servlet.UserPrincipalRequestWrapperTestBasic Behavior/Assertions:com.ctv.mocksample.servlet.AuthenticationFilterTestStrict vs Loose:com.ctv.mocksample.servlet.AuthenticationFilterTestPowerMocking Statics: com.ctv.model.UserTestPowerMocking privates: com.ctv.model.OrderPowerMockTest
    32. 32. JAVA FRAMEWORKSMockrunner: http://mockrunner.sourceforge.net/SOAP-UI: http://soapui.org/EasyMock: http://easymock.org/Mockito: http://mockito.org/http://code.google.com/p/mockito/Powermock: http://code.google.com/p/powermock/
    33. 33. .NET FRAMEWORKSRhino Mocks: http://www.ayende.com/projects/rhino-mocks.aspxEasyMock.net http://sourceforge.net/projects/easymocknet/NMock http://www.nmock.org/
    34. 34. RESOURCES“Mocks aren’t Stubs” - Martin Fowler http://www.martinfowler.com/articles/mocksArentStubs.htmlQuestioning “testable design” http://blog.jayway.com/2009/04/01/questioning-testable- design/“Testing the Entire Stack” - Neal Ford http://nealford.com/downloads/conferences/2010/ Testing_the_Entire_Stack(Neal_Ford).pdf
    35. 35. RESOURCESJava Mocking Framework Comparison http://www.sizovpoint.com/2009/03/java-mock- frameworks-comparison.htmlEasyMock / Mockito Comparisons http://code.google.com/p/mockito/wiki/ MockitoVSEasyMock http://blog.octo.com/en/easymock-facts-fallacies/
    36. 36. RESOURCESxUnit concepts and definitions http://xunitpatterns.com/Test%20Double.htmlStrict vs Nice Mocks Pro Strict: http://bit.ly/bj4dSF Design vs Maintainability: http://stackoverflow.com/questions/2864796/ easymock-vs-mockito-design-vs-maintainability
    37. 37. PRESENTATION CODEYou can pull down the code from this presentation onGithub https://github.com/mpegram3rd/testing-with-mocks
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×