• Like

Loading…

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

Using Mockito

  • 8,033 views
Published

Slides used to introduce Mockito

Slides used to introduce Mockito

Published in Education
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
8,033
On SlideShare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
243
Comments
0
Likes
7

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • out of reach: * too hard to setup * Singletons * Interfaces without proper classes

Transcript

  • 1. Using Mockito
  • 2. This work by Fredrik Wendt is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License http://creativecommons.org/licenses/by-nc-sa/3.0/
  • 3. Outline
    • mock (ClassToMock.class)
    • 4. when (methodCall)
      • thenReturn (value)
      • 5. thenThrow (Throwable)
    • verify (mock).method(args)
    • @Mock
    • 6. initMocks (this)
    • 7. assertThat (obj, matcher)
    • 8. Eclipse IDE tips
  • 9. Why Mockito - and when? Use Mockito to get ”smart” fake implementations of classes or interfaces out of your reach.
  • 10. Classical 3A Test @Test public void test() throws Exception { // Arrange // Act // Assert }
  • 11. Classical 3A Test @Test public void test() throws Exception { // Arrange UnitToTest testee = new UnitToTest(); Helper helper = new Helper(); // Act testee.doSomething(helper); // Assert assertTrue(helper.somethingHappened()); }
  • 12. Mockito ”Template” Usage @Test public void test() throws Exception { // Arrange, prepare behaviour Helper aMock = mock (Helper.class); when (aMock.isCalled()). thenReturn (true); // Act testee.doSomething(aMock); // Assert - verify interactions verify (aMock).isCalled(); }
  • 13. when(...).then*(...) when ( methodIsCalled ). thenReturn ( aValue ); when ( methodIsCalled ). thenThrow ( anException );
  • 14. Real Code Properties properties = mock (Properties.class); when (properties.get(” shoeSize ”)). thenReturn (” 42 ”); String result = properties.get(” shoeSize ”); assertEquals(” 42 ”, result);
  • 15. Real Code Properties properties = mock (Properties.class); when (properties.get(” shoeSize ”)). thenReturn (” 42 ”); String result = properties.get(” shoeSize ”); assertEquals(” 42 ”, result); // optional verify (properties).get(” shoeSize ”);
  • 16. when(...) when ( methodIsCalled ) aMockObject.method(arguments...) properties.get(”42”) properties.get(anyString()) properties.get(eq(”42”))
  • 17. thenReturn(value) Properties properties = mock(Properties.class); when (properties.get(” shoeSize ”)) . thenReturn (” 42 ”)); String value = properties.get(” shoeSize ”); assertEquals(” 42 ”, value);
  • 18. thenThrow(Exception) Properties properties = mock(Properties.class); when (properties.get(” shoooSize ”)) . thenThrow ( new IllegalArgumentException(...) ); try { properties.get(” shoooSize ”); fail(”shoooSize is misspelled”); } catch ( IllegalArgumentException e) { // good! :) }
  • 19. thenReturn(...) Properties properties = mock(Properties.class); when(properties.get(”shoeSize”)) .thenReturn(” 42 ”, ” 43 ”, ” 44 ”)); assertEquals(” 42 ”, properties.get(”shoeSize”)); assertEquals(” 43 ”, properties.get(”shoeSize”)); assertEquals(” 44 ”, properties.get(”shoeSize”)); assertEquals(” 44 ”, properties.get(”shoeSize”)); assertEquals(” 44 ”, properties.get(”shoeSize”)); assertEquals(” 44 ”, properties.get(”shoeSize”));
  • 20. Creating Mock Objects ClassToMock mockObject = mock ( ClassToMock .class); egentligen: Mockito. mock ( ClassToMock .class);
  • 21. Creating Mock Objects ClassToMock mockObject = mock(ClassToMock.class); @Mock ClassToMock mockObject; @Before public void setup() { MockitoAnnotations.initMocks(this); }
  • 22. Verifying Invocation On Mocks // simple ”equals” verify(properties).get( ”property” ); // matchers verify(properties).get( eq(”property”) );
  • 23. The IDE Can Help 1
    • Window » Preferences » Java » Editor » Content Assistant » Favorites
      • org.junit.Assert.* assertTrue()
      • 24. org.mockito.Mockito.* mock()
      • 25. org.mockito.MockitoAnnotations @Mock
      • 26. org.mockito.BDDMockito.* given().then*()
  • 27. The IDE Can Help 2
    • Window » Preferences » Java » Editor » Templates
    ${staticImport:importStatic( 'org.mockito.MockitoAnnotations. initMocks')} @${beforeAnnotation: newType(org.junit.Before)} public void ${setUp}() { initMocks(this); ${cursor} }
  • 28. The IDE Can Help 3
    • Window » Preferences » Java » Editor » Templates
    tdd @Test public void ${test} throws Exception { // Arrange // Act // Assert }
  • 29. public class EmptyMatcher extends TypeSafeMatcher<Collection <?>> { @Override public boolean matchesSafely(Collection<?> c) { return c.isEmpty(); } public void describeTo(Description desc) { desc.appendText(&quot;empty&quot;); } @Factory public static <T> Matcher<? super Collection<?>> isEmpty() { return new EmptyMatcher(); } }