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.

Mocking in Java with Mockito

38,109 views

Published on

Published in: Technology
  • My brother found Custom Writing Service ⇒ HelpWriting.net ⇐ and ordered a couple of works. Their customer service is outstanding, never left a query unanswered.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Copy the same step by step strategy I used to pass my maths gcse with ease. Visit to learn more. ▲▲▲ http://t.cn/AirraVnG
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • I didn't think failing my maths back in high school would come back to haunt me but it did! After graduating from Edge Hill University, I was looking to undertake a PGCE in primary teaching. However, one of the requirements was a pass in GCSE maths so I had no choice but to re-take it. After failing it twice, my confidence was very low and I didn't think I could qualify for the PGCE. After running a Google search on passing GCSE maths, I came across Jeevan's revision system. The feedback it received looked very good so I decided to give it a try... And it was one of the best decisions I've ever made. After going through Jeevan's guide, I managed to grasp the entire subject and I passed my next GCSE maths exam, with ease. He (Jeevan) provided me with all the tools I needed to prepare for the exam. Together with his guidance, my pass was a foregone conclusion. Thank you so much Jeevan! You have potentially changed my life. I recommend anyone seeking a pass grade in GCSE maths to purchase this fantastic package!♣♣♣ http://t.cn/AirraVnG
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • I didn't think failing my maths back in high school would come back to haunt me but it did! After graduating from Edge Hill University, I was looking to undertake a PGCE in primary teaching. However, one of the requirements was a pass in GCSE maths so I had no choice but to re-take it. After failing it twice, my confidence was very low and I didn't think I could qualify for the PGCE. After running a Google search on passing GCSE maths, I came across Jeevan's revision system. The feedback it received looked very good so I decided to give it a try... And it was one of the best decisions I've ever made. After going through Jeevan's guide, I managed to grasp the entire subject and I passed my next GCSE maths exam, with ease. He (Jeevan) provided me with all the tools I needed to prepare for the exam. Together with his guidance, my pass was a foregone conclusion. Thank you so much Jeevan! You have potentially changed my life. I recommend anyone seeking a pass grade in GCSE maths to purchase this fantastic package!◆◆◆ http://t.cn/AirrSv7D
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • I've been struggling with maths for the past 2 years now. In the first mock paper I took, I got a 'U' grade. At this point, I felt hopeless. How was I ever going to pass my maths?! I did a Google search and came across Jeevan's stuff on Youtube. I was in a desperate situation so I had no choice but to purchase your material... Jeevan's resources are amazing. They have really helped me out. I am much more confident with my maths and I am able to answer past papers within the time limit. I recently had a mock exam at school and I achieved a 'D' grade! A massive improvement since my last score. If I continue to make this progress, there is no doubt that I will achieve my pass grade in my final exam. Thanks for your amazing resources Jeevan. They are so much better than the other revision guides I wasted money on! ◆◆◆ http://ishbv.com/jeevan91/pdf
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Mocking in Java with Mockito

  1. 1. A mocking framework for Java http://www.mockito.org/ Richard Paul - http://www.rapaul.com/
  2. 2. State vs Interaction testing State testing asserts properties on an object e.g. assertEquals(4, item.getCount()); Interaction testing verifies the interactions between objects. e.g. Did my controller correctly call my service. Mockito provides a framework for interactions testing.
  3. 3. Test Doubles Terminology defined by Gerard Meszaros. See: http://tinyurl.com/testdoubles Test Stub Hand coded object used for testing. Mock object (e.g. EasyMock) Expectations configured before calling the method under test. Test Spy (e.g. Mockito) Verification occurs after the method under test has been called.
  4. 4. Mockito Example Simple MVC example: Item - simple POJO (has a name and ID). ItemController - handles requests from the browser. ItemService - service the ItemController delegates to. ItemNotFoundException Following slides detail testing the ItemController using Mockito. Note: the controller is implemented using a Springframework 2.5 annotation style, with annotations omitted for clarity.
  5. 5. View Item - using when Eclipse then generates the viewItem method signature in ItemController.java
  6. 6. Test Failure Then implement viewItem to pass the test
  7. 7. Delete Item - using verify Eclipse then generates the deleteItem method signature in ItemController.java
  8. 8. Wanted, but not invoked
  9. 9. Exceptional Cases - using thenThrow Allows simple testing of exceptional cases with similar syntax to thenReturn .
  10. 10. Initialising mocks in Mockito Allows access to when, verify etc without the ... Mockito.prefix ...
  11. 11. Default Values Mockito provides default values (null, 0, empty list, etc) for method calls that have not been defined. This allows you to define only the interactions that are relevant to your test. See slides by Szczepan Faber http://tinyurl.com/5cxg64 Slides 8-31 Why ignore interactions not directly related to a test case? (slide 29) quot;because aggressive validation makes the tests brittlequot; quot;because I have to fix tests even when the code is not brokenquot; quot;... can increase noisequot; quot;or lead to overspecificationquot;
  12. 12. More features... Verify how many times a method is called. e.g. verify(itemService, never()).deleteItem(3); verify(itemService, atLeastOnce()).deleteItem(3); verify(itemService, times(3)).deleteItem(3); Verify the order calls are made using InOrder Chain subsequent calls to return different values. when(itemService.getItem(3)) .thenReturn(item1) .thenReturn(item2); Argument matchers when(service.get(eq(1), anyFloat()).thenReturn(quot;xquot;); Can mock concrete classes (uses cglib)
  13. 13. Comments, queries, suggestions or theories? Slides + code examples will be up on http://www.rapaul.com

×