Skip to content

Instantly share code, notes, and snippets.

@mzakyalvan
Created October 19, 2018 12:53
Show Gist options
  • Save mzakyalvan/480061edac13d7dc15896841aa543c10 to your computer and use it in GitHub Desktop.
Save mzakyalvan/480061edac13d7dc15896841aa543c10 to your computer and use it in GitHub Desktop.
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