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.

Like this presentation? Why not share!

Using Mockito

on

  • 8,525 views

Slides used to introduce Mockito

Slides used to introduce Mockito

Statistics

Views

Total Views
8,525
Views on SlideShare
8,521
Embed Views
4

Actions

Likes
5
Downloads
232
Comments
0

1 Embed 4

http://www.linkedin.com 4

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

CC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • out of reach: * too hard to setup * Singletons * Interfaces without proper classes

Using Mockito Using Mockito Presentation Transcript

  • Using Mockito
  • 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/
  • Outline
    • mock (ClassToMock.class)
    • when (methodCall)
      • thenReturn (value)
      • thenThrow (Throwable)
    • verify (mock).method(args)
    • @Mock
    • initMocks (this)
    • assertThat (obj, matcher)
    • Eclipse IDE tips
  • Why Mockito - and when? Use Mockito to get ”smart” fake implementations of classes or interfaces out of your reach.
  • Classical 3A Test @Test public void test() throws Exception { // Arrange // Act // Assert }
  • 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()); }
  • 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(); }
  • when(...).then*(...) when ( methodIsCalled ). thenReturn ( aValue ); when ( methodIsCalled ). thenThrow ( anException );
  • Real Code Properties properties = mock (Properties.class); when (properties.get(” shoeSize ”)). thenReturn (” 42 ”); String result = properties.get(” shoeSize ”); assertEquals(” 42 ”, result);
  • 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 ”);
  • when(...) when ( methodIsCalled ) aMockObject.method(arguments...) properties.get(”42”) properties.get(anyString()) properties.get(eq(”42”))
  • thenReturn(value) Properties properties = mock(Properties.class); when (properties.get(” shoeSize ”)) . thenReturn (” 42 ”)); String value = properties.get(” shoeSize ”); assertEquals(” 42 ”, value);
  • 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! :) }
  • 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”));
  • Creating Mock Objects ClassToMock mockObject = mock ( ClassToMock .class); egentligen: Mockito. mock ( ClassToMock .class);
  • Creating Mock Objects ClassToMock mockObject = mock(ClassToMock.class); @Mock ClassToMock mockObject; @Before public void setup() { MockitoAnnotations.initMocks(this); }
  • Verifying Invocation On Mocks // simple ”equals” verify(properties).get( ”property” ); // matchers verify(properties).get( eq(”property”) );
  • The IDE Can Help 1
    • Window » Preferences » Java » Editor » Content Assistant » Favorites
      • org.junit.Assert.* assertTrue()
      • org.mockito.Mockito.* mock()
      • org.mockito.MockitoAnnotations @Mock
      • org.mockito.BDDMockito.* given().then*()
  • 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} }
  • The IDE Can Help 3
    • Window » Preferences » Java » Editor » Templates
    tdd @Test public void ${test} throws Exception { // Arrange // Act // Assert }
  • 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(); } }