Skip to content

Instantly share code, notes, and snippets.

@mzakyalvan
Last active January 7, 2019 03:47
Show Gist options
  • Save mzakyalvan/8c8aba8e031487ef4a74b8f2523cc496 to your computer and use it in GitHub Desktop.
Save mzakyalvan/8c8aba8e031487ef4a74b8f2523cc496 to your computer and use it in GitHub Desktop.
package com.tiket.poc.swagger;
import java.util.List;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
public class MockitoVerifyParametersTests {
@Test
public void test() {
ComponentUnderTest componentUnderTest = Mockito.mock(ComponentUnderTest.class);
ArgumentCaptor<String> valueCaptors = ArgumentCaptor.forClass(String.class);
Mockito.when(componentUnderTest.parseValue(valueCaptors.capture()))
.thenAnswer(invocation -> {
String arg = invocation.getArgument(0);
return Integer.parseInt(arg);
});
MatcherAssert.assertThat(componentUnderTest.parseValue("1"), Matchers.is(1));
MatcherAssert.assertThat(componentUnderTest.parseValue("2"), Matchers.is(2));
MatcherAssert.assertThat(componentUnderTest.parseValue("3"), Matchers.is(3));
List<String> arguments = valueCaptors.getAllValues();
MatcherAssert.assertThat(arguments, Matchers.contains("1", "2", "3"));
Mockito.verify(componentUnderTest, Mockito.times(3)).parseValue(Mockito.anyString());
}
static interface ComponentUnderTest {
int parseValue(String value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment