Created
October 19, 2018 12:53
-
-
Save mzakyalvan/480061edac13d7dc15896841aa543c10 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tiket.poc; | |
import org.hamcrest.MatcherAssert; | |
import org.hamcrest.Matchers; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import org.junit.rules.ExpectedException; | |
import org.mockito.Mockito; | |
public class MockitoTests { | |
@Rule | |
public ExpectedException expectedErrors = ExpectedException.none(); | |
@Test | |
public void test() { | |
ComponentUnderTest componentUnderTest = Mockito.mock(ComponentUnderTest.class); | |
Mockito.when(componentUnderTest.getValue()).thenThrow(new IllegalStateException("First call not allowed")) | |
.thenReturn(2).thenReturn(3).thenReturn(3); | |
expectedErrors.expect(IllegalStateException.class); | |
componentUnderTest.getValue(); | |
MatcherAssert.assertThat(componentUnderTest.getValue(), Matchers.is(2)); | |
MatcherAssert.assertThat(componentUnderTest.getValue(), Matchers.is(3)); | |
MatcherAssert.assertThat(componentUnderTest.getValue(), Matchers.is(4)); | |
Mockito.verify(componentUnderTest.getValue(), Mockito.times(4)); | |
} | |
static interface ComponentUnderTest { | |
int getValue(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment