@Test public void shouldRemoveStubbingToString() throws Exception { IMethods mockTwo = mock(IMethods.class); when(mockTwo.toString()).thenReturn("test"); reset(mockTwo); assertContains("Mock for IMethods", mockTwo.toString()); }
@Test public void shouldResetMultipleMocks() { mock.simpleMethod(); mockTwo.simpleMethod(); reset(mock, mockTwo); verifyNoMoreInteractions(mock, mockTwo); }
@Test public void shouldUseCustomObjectMatcher() { when(mock.oneArg(argThat(new ContainsFoo()))).thenReturn("foo"); assertEquals("foo", mock.oneArg("foo")); assertEquals(null, mock.oneArg("bar")); }
@Test public void shouldUseCustomCharMatcher() { when(mock.oneArg(charThat(new IsSorZ()))).thenReturn("foo"); assertEquals("foo", mock.oneArg('s')); assertEquals("foo", mock.oneArg('z')); assertEquals(null, mock.oneArg('x')); }
@Test public void shouldRemoveAllStubbing() throws Exception { when(mock.objectReturningMethod(isA(Integer.class))).thenReturn(100); when(mock.objectReturningMethod(200)).thenReturn(200); reset(mock); assertNull(mock.objectReturningMethod(200)); assertEquals("default behavior should return null", null, mock.objectReturningMethod("blah")); }
@Test public void shouldUseCustomBooleanMatcher() { when(mock.oneArg(booleanThat(new IsAnyBoolean()))).thenReturn("foo"); assertEquals("foo", mock.oneArg(true)); assertEquals("foo", mock.oneArg(false)); assertEquals(null, mock.oneArg("x")); }
@Test public void shouldGetAllInvocationsInOrder() throws Exception { mockOne.simpleMethod(100); mockTwo.simpleMethod(200); mockOne.simpleMethod(300); List<Invocation> invocations = finder.find(asList(mockOne, mockTwo)); assertEquals(3, invocations.size()); assertArgumentEquals(100, invocations.get(0)); assertArgumentEquals(200, invocations.get(1)); assertArgumentEquals(300, invocations.get(2)); }
@Test public void shouldReadConfigurationClassFromClassPath() { ConfigurationAccess.getConfig() .overrideDefaultAnswer( new Answer<Object>() { public Object answer(InvocationOnMock invocation) { return "foo"; } }); IMethods mock = mock(IMethods.class); assertEquals("foo", mock.simpleMethod()); }
@Test public void shouldStubbingNotBeTreatedAsInteraction() { when(mock.simpleMethod("one")).thenThrow(new RuntimeException()); doThrow(new RuntimeException()).when(mock).simpleMethod("two"); reset(mock); verifyZeroInteractions(mock); }
@Test public void shouldNotCountDuplicatedInteractions() throws Exception { mockOne.simpleMethod(100); List<Invocation> invocations = finder.find(asList(mockOne, mockOne, mockOne)); assertEquals(1, invocations.size()); }
@Test public void shouldMaintainPreviousDefaultAnswer() { // given mock = mock(IMethods.class, RETURNS_MOCKS); // when reset(mock); // then assertNotNull(mock.iMethodsReturningMethod()); }
@Test public void shouldResetOngoingStubbingSoThatMoreMeaningfulExceptionsAreRaised() { mock(IMethods.class); mock.booleanReturningMethod(); reset(mock); try { when(null).thenReturn("anything"); fail(); } catch (MissingMethodInvocationException e) { } }
@Test public void shouldCustomMatcherPrintDescriptionBasedOnName() { mock.simpleMethod("foo"); try { verify(mock).simpleMethod(containsTest()); fail(); } catch (AssertionError e) { assertThat(e).hasMessageContaining("<String that contains xxx>"); } }
@Test public void shouldAnonymousCustomMatcherPrintDefaultDescription() { mock.simpleMethod("foo"); try { verify(mock) .simpleMethod( (String) argThat( new ArgumentMatcher<Object>() { public boolean matches(Object argument) { return false; } })); fail(); } catch (AssertionError e) { assertThat(e).hasMessageContaining("<custom argument matcher>").hasMessageContaining("foo"); } }
@Test public void testInjectMocks() throws Exception { when(mock.simpleMethod(1)).thenReturn("1"); mock.simpleMethod(2); }
@Test(expected = NotAMockException.class) public void shouldScreamWhenWholeMethodPassedToVerifyNoMoreInteractions() { verifyNoMoreInteractions(mock.byteReturningMethod()); }
@Test(expected = NotAMockException.class) public void shouldScreamWhenWholeMethodPassedToVerify() { verify(mock.booleanReturningMethod()); }
@Test public void shouldUseCustomPrimitiveNumberMatchers() { when(mock.oneArg(byteThat(new IsZeroOrOne<Byte>()))).thenReturn("byte"); when(mock.oneArg(shortThat(new IsZeroOrOne<Short>()))).thenReturn("short"); when(mock.oneArg(intThat(new IsZeroOrOne<Integer>()))).thenReturn("int"); when(mock.oneArg(longThat(new IsZeroOrOne<Long>()))).thenReturn("long"); when(mock.oneArg(floatThat(new IsZeroOrOne<Float>()))).thenReturn("float"); when(mock.oneArg(doubleThat(new IsZeroOrOne<Double>()))).thenReturn("double"); assertEquals("byte", mock.oneArg((byte) 0)); assertEquals("short", mock.oneArg((short) 1)); assertEquals("int", mock.oneArg(0)); assertEquals("long", mock.oneArg(1L)); assertEquals("float", mock.oneArg(0F)); assertEquals("double", mock.oneArg(1.0)); assertEquals(null, mock.oneArg(2)); assertEquals(null, mock.oneArg("foo")); }
@Test public void shouldRemoveAllInteractions() throws Exception { mock.simpleMethod(1); reset(mock); verifyZeroInteractions(mock); }