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.

[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – Scrum Master at Axon Active Vietnam


Published on

Why do programmers hate writing Unit Tests? One big reason is object dependency. An object under testing may have dependencies on other complex objects, which might not have been implemented or been complicated when invoking.

Join the session refresh your thinking about Unit Testing and overview of mocking framework, as well as learn some practice/gotcha to write a real Unit Test, how to isolate the behavior of the object you want to test, how to simulate the behavior of the dependencies.


Speaker: Phat Vu – Scrum Master at Axon Active Vietnam

Published in: Software
  • Be the first to comment

  • Be the first to like this

[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – Scrum Master at Axon Active Vietnam

  2. 2. CONTENTS 1. About me 2. Unit testing refresh 3. Real unit testing 4. Mock framework 5. Mockito • Introduction • Practice / gotcha 6. Wrapping up 7. Q&A
  3. 3. GOALS • What’s real Unit testing, its importance • Mock objects • How you can write a real unit testing
  4. 4. My DNA
  5. 5. My DNA
  6. 6. My DNA Vũ Hồng Phát Software Engineer, 05 years Java, Database, Open-source Interested in • Programming • Agile/Scrum • High quality software development • Optimization • Testing phatvu.dng
  7. 7. Unit Testing (refresh)
  8. 8. • Do you know what’s unit testing? • Do you think it’s important? • Who will write it? • Do you enjoy when writing it? CHECK IN
  9. 9. Unit testing is a software development process in which the smallesttestable parts of an application, called units, are individually and independently scrutinized for proper operation. Unit testing is often automated but it can also be done manually. WHAT
  10. 10. WHY http://jesusw
  11. 11. WHY
  12. 12. WHY 1. Prove that your code actually works. 2. Form of sample code / documentation 3. Code refactoring 4. Forces you to plan before you code • Better design • Testable code 5. Integrate with Continuous Integration 6. It’s more fun to code with them than without
  13. 13. UNIT TEST FIRST? Problem (Unit testing) Solution (Implementation)
  15. 15. A SIMPLE PROBLEM Implement a service that accept 2 integer numbers, then return summary of them Example: Input : 1 and 2 Output : 3
  16. 16. Write unit test first
  17. 17. Then, implement Run the unit test
  18. 18. Real Unit Testing
  19. 19. DEPENDENCIES • A tested object usually talks to other objects known as collaborators / dependencies • These dependencies need to be created, so the tested object can be assigned to them in the test • Any hello world has no dependencies on outside classes • In the real world, software has dependencies MathService Calculator
  21. 21. REAL UNIT TESTING • The idea of unit testing is that we want to test our code without testing the dependencies • Unit testing verifies that the code being tested works, regardless of it's dependencies • Be able to test the code, no matter dependencies implementation • If the code I write works as designed and my dependencies work as designed, then they should work together as designed
  22. 22. EXAMPLE MathService has dependency : Calculator When testing MathService: • Do not test Calculator implementation. Assume they has been tested and works as designed • Do not wait for their implementation MathService Calculator MathService Calculator
  23. 23. Mocking framework
  24. 24. MOCK OBJECT In OOP, mock objects are simulated objects that mimic the behavior of real objects in controlled ways. https://en.w
  25. 25. CRASH TEST DUMMY https://en.w
  26. 26. GIVEN – WHEN - THEN Style of representing tests, part of BDD • Given defines the preconditions that hold before an event or operation • When identifies an event or operation • Then identities the post conditions that hold after the event or operation
  27. 27. GIVEN – WHEN - THEN • Given : the Login page is opening • When : I enter a correct user name & password, then click on Login button • Then : I am on the Home page
  28. 28. MOCKITO Is a mocking framework helpful in creating mocks and spies in a simple and intuitive way, while at the same time providing great control of the whole process “Really cleans up the unit test by not requiring expectations. Personally, I much prefer the Mockito API to the EasyMock API for that reason” - Hamlet D'Arcy
  29. 29. INSTALL Maven dependency
  31. 31. GETTING STARTED How can I test MathService.add() without concern on its dependency (Calculator implementation) ? I have to simulate Calculator as a mock object
  32. 32. GETTING STARTED @Mock - create a mock object must initialize this object with a MockitoAnnotations.initMocks(this) method call @InjectMocks - injects mocks into tested object automatically given(….).willReturn(….)
  34. 34. IMPROVE DESIGN Calculator <interface> MathService CalculatorImpl use implement
  35. 35. ARGUMENT MATCHERS Mockito provides a set of build-in matchers, such as: anyInt(), anyString() If you are using argument matchers, all arguments have to be provided by matchers
  37. 37. EXCEPTION Mock exception: doThrow...when
  38. 38. MULTIPLE CALLS Return different values for subsequent calls of the same method
  39. 39. SPY • A mock created as a proxy to an existing real object • Some methods can be stubbed • Some ones invoked in real • Partial mocking • Usually there is no reason (code smell) to spy on real objects
  40. 40. SPY
  41. 41. LIMITATIONS Mockito can not: • mock final classes • mock enums • mock final methods • mock static methods • mock private methods • mock hashCode() and equals() PowerMock or JMockit can be used to work with code that cannot be mocked with pure Mockito
  42. 42. GOTCHA
  45. 45. TEST COVERAGE A measurement of how many lines/blocks of your code are executed while the automated tests are running A useful tool for finding untested parts of a codebase 100% - it would smell of someone writing tests to make the coverage numbers happy, but not thinking about what they are doing Low coverage numbers, are a sign of trouble. But high numbers don't necessarily mean much Upper 80s or 90s
  46. 46. TEST COVERAGE http://martinfow
  47. 47. MOCK PRIVATE METHODS Why Mockito doesn't mock private methods? (
  48. 48. MOCK PRIVATE METHODS Refactoring Considerations • Low cohesion (has too many responsibilities) • Extract into separate class. Workaround Using Mockito • Changing private access modifier to default • Partially mock testing object by using spy • PowerMock or JMockIt (More :
  49. 49. Unit testing is testing units Units bare without dependencies. And this can be done with mocks SAYING IT SIMPLY
  50. 50. Q & A
  51. 51. REFERENCES