Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Unit Test With Mockito

Unit Test With Mockito

This talk was presented at the Blrdroid Women's day meet-up 10th March 2018.
I covered "Unit Test with Mockito". I have mention GitHub link (https://github.com/vrushaliraut/MvpMockitoWeatherApp) for repository which i have presented with this slides.
In repository I have created some examples with - Mockito Unit test cases.

Vrushali Raut

March 10, 2018
Tweet

More Decks by Vrushali Raut

Other Decks in Programming

Transcript

  1. Testing Framework • How do you test it? • Manually

    test an application • Automate your app
  2. What is Mockito • What is Mockito? • Open source

    Testing framework • Creation of Mock object in Automated unit test
  3. Mockito - Basics • Create stub • Use Mock Service

    • Use When…thenReturn of Mockito • Annotation’s • @Mock • @Rule
  4. Test Doubles • Test double is an object that can

    stand in for the real object in a test
  5. Stubs • Has no logic - Return What you tell

    • Can be used - when you need an object, 
 to return a specific values in order to get your code under test into a certain condition
  6. Mocks • A Mock has expectations about the call
 •

    Used to test interaction between objects
  7. Mocks • Useful in cases - • No visible state

    changes or Return result that you can verify
  8. Fakes • Fake doesn’t use mocking framework • Lightweight implementation

    of API • Behaves like real implementation • an in-memory database
  9. Fakes - When to use • When you can’t use

    a real implementation in your test • If real implementation is too slow or talks over the Network
  10. Mockito - Basic Example • Stub • Mock Service •

    Use When…thenReturn of Mockito • Annotation’s • @Mock • @Rule
  11. Enable Annotations • Annotate the JUnit test with a runner

    - • MockitoJUnitRunner. • Programmatically
  12. Mockito Annotation • @Mock • Does so class of an

    Type, Not from actual instance • @Spy • Wrap the existing instance
  13. Argument Matchers • Mockito verifies argument values in Java style.

    • equals() • Allows flexible verification or stubbing
  14. Argument Matchers • Mockito Argument Matcher • any • argThat

    • eq • same • Argumentcaptor.captor()
  15. GitHub Reference • In this Github Repository I have write

    down some examples of mockito test cases • https://github.com/vrushaliraut/ MvpMockitoWeatherApp
  16. Reference :- • https://static.javadoc.io/org.mockito/mockito-core/2.15.0/org/mockito/Mockito.html • https://blog.pragmatists.com/test-doubles-fakes-mocks-and-stubs-1a7491dfa3da • https://www.youtube.com/watch?v=eILy4p99ac8 • http://www.baeldung.com/mockito-spy

    • https://testing.googleblog.com/2013/07/testing-on-toilet-know-your-test- doubles.html • http://www.baeldung.com/mockito-annotations • https://stackoverflow.com/questions/22822512/how-do-mockito-matchers-work