Skip to content

Instantly share code, notes, and snippets.

@CliveEvans
Created June 29, 2010 11:54
Show Gist options
  • Save CliveEvans/457120 to your computer and use it in GitHub Desktop.
Save CliveEvans/457120 to your computer and use it in GitHub Desktop.
public class Helper {
public void addInterface(SomeInterface some) {
}
}
@RunWith(MockitoJUnitRunner.class)
public class MockitoAnyVersusIsATest {
@Mock
private Helper helper;
@Test
public void shouldVerifyCalledWithAnyArgumentsAtAll() throws Exception {
new ObjectUnderTest(helper);
verify(helper, times(2)).addInterface(any(ImplementationOne.class)); // class parameter is used to make the systems happy at compile time
}
@Test
public void shouldVerifyArgumentsWereOfRightType() throws Exception {
new ObjectUnderTest(helper);
verify(helper).addInterface(isA(ImplementationOne.class));
verify(helper).addInterface(isA(ImplementationTwo.class));
}
}
@RunWith(MockitoJUnitRunner.class)
public class MockitoAnyVersusIsATest {
@Mock
private Helper helper;
@Test
public void shouldVerifyCalledWithAnyArgumentsAtAll() throws Exception {
new ObjectUnderTest(helper);
verify(helper)).addInterface(any(ImplementationOne.class));
verify(helper)).addInterface(any(ImplementationTwo.class));
}
}
public class ObjectUnderTest {
public ObjectUnderTest(Helper helper) {
helper.addInterface(new ImplementationOne());
helper.addInterface(new ImplementationTwo());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment