Beispiel #1
0
 @Test
 public void shouldRemoveStubbingToString() throws Exception {
   IMethods mockTwo = mock(IMethods.class);
   when(mockTwo.toString()).thenReturn("test");
   reset(mockTwo);
   assertContains("Mock for IMethods", mockTwo.toString());
 }
Beispiel #2
0
 @Test
 public void shouldResetMultipleMocks() {
   mock.simpleMethod();
   mockTwo.simpleMethod();
   reset(mock, mockTwo);
   verifyNoMoreInteractions(mock, mockTwo);
 }
Beispiel #3
0
  @Test
  public void shouldUseCustomObjectMatcher() {
    when(mock.oneArg(argThat(new ContainsFoo()))).thenReturn("foo");

    assertEquals("foo", mock.oneArg("foo"));
    assertEquals(null, mock.oneArg("bar"));
  }
Beispiel #4
0
  @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'));
  }
Beispiel #5
0
 @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"));
 }
Beispiel #6
0
  @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());
  }
Beispiel #9
0
 @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());
  }
Beispiel #11
0
 @Test
 public void shouldMaintainPreviousDefaultAnswer() {
   // given
   mock = mock(IMethods.class, RETURNS_MOCKS);
   // when
   reset(mock);
   // then
   assertNotNull(mock.iMethodsReturningMethod());
 }
Beispiel #12
0
 @Test
 public void shouldResetOngoingStubbingSoThatMoreMeaningfulExceptionsAreRaised() {
   mock(IMethods.class);
   mock.booleanReturningMethod();
   reset(mock);
   try {
     when(null).thenReturn("anything");
     fail();
   } catch (MissingMethodInvocationException e) {
   }
 }
Beispiel #13
0
  @Test
  public void shouldCustomMatcherPrintDescriptionBasedOnName() {
    mock.simpleMethod("foo");

    try {
      verify(mock).simpleMethod(containsTest());
      fail();
    } catch (AssertionError e) {
      assertThat(e).hasMessageContaining("<String that contains xxx>");
    }
  }
Beispiel #14
0
  @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");
    }
  }
Beispiel #15
0
 @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());
 }
Beispiel #18
0
  @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"));
  }
Beispiel #19
0
 @Test
 public void shouldRemoveAllInteractions() throws Exception {
   mock.simpleMethod(1);
   reset(mock);
   verifyZeroInteractions(mock);
 }