コード例 #1
0
ファイル: ResetTest.java プロジェクト: nathansgreen/mockito
 @Test
 public void shouldRemoveStubbingToString() throws Exception {
   IMethods mockTwo = mock(IMethods.class);
   when(mockTwo.toString()).thenReturn("test");
   reset(mockTwo);
   assertContains("Mock for IMethods", mockTwo.toString());
 }
コード例 #2
0
ファイル: ResetTest.java プロジェクト: nathansgreen/mockito
 @Test
 public void shouldResetMultipleMocks() {
   mock.simpleMethod();
   mockTwo.simpleMethod();
   reset(mock, mockTwo);
   verifyNoMoreInteractions(mock, mockTwo);
 }
コード例 #3
0
ファイル: CustomMatchersTest.java プロジェクト: bric3/mockito
  @Test
  public void shouldUseCustomObjectMatcher() {
    when(mock.oneArg(argThat(new ContainsFoo()))).thenReturn("foo");

    assertEquals("foo", mock.oneArg("foo"));
    assertEquals(null, mock.oneArg("bar"));
  }
コード例 #4
0
ファイル: CustomMatchersTest.java プロジェクト: bric3/mockito
  @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'));
  }
コード例 #5
0
ファイル: ResetTest.java プロジェクト: nathansgreen/mockito
 @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"));
 }
コード例 #6
0
ファイル: CustomMatchersTest.java プロジェクト: bric3/mockito
  @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"));
  }
コード例 #7
0
  @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));
  }
コード例 #8
0
  @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());
  }
コード例 #9
0
ファイル: ResetTest.java プロジェクト: nathansgreen/mockito
 @Test
 public void shouldStubbingNotBeTreatedAsInteraction() {
   when(mock.simpleMethod("one")).thenThrow(new RuntimeException());
   doThrow(new RuntimeException()).when(mock).simpleMethod("two");
   reset(mock);
   verifyZeroInteractions(mock);
 }
コード例 #10
0
  @Test
  public void shouldNotCountDuplicatedInteractions() throws Exception {
    mockOne.simpleMethod(100);

    List<Invocation> invocations = finder.find(asList(mockOne, mockOne, mockOne));

    assertEquals(1, invocations.size());
  }
コード例 #11
0
ファイル: ResetTest.java プロジェクト: nathansgreen/mockito
 @Test
 public void shouldMaintainPreviousDefaultAnswer() {
   // given
   mock = mock(IMethods.class, RETURNS_MOCKS);
   // when
   reset(mock);
   // then
   assertNotNull(mock.iMethodsReturningMethod());
 }
コード例 #12
0
ファイル: ResetTest.java プロジェクト: nathansgreen/mockito
 @Test
 public void shouldResetOngoingStubbingSoThatMoreMeaningfulExceptionsAreRaised() {
   mock(IMethods.class);
   mock.booleanReturningMethod();
   reset(mock);
   try {
     when(null).thenReturn("anything");
     fail();
   } catch (MissingMethodInvocationException e) {
   }
 }
コード例 #13
0
ファイル: CustomMatchersTest.java プロジェクト: bric3/mockito
  @Test
  public void shouldCustomMatcherPrintDescriptionBasedOnName() {
    mock.simpleMethod("foo");

    try {
      verify(mock).simpleMethod(containsTest());
      fail();
    } catch (AssertionError e) {
      assertThat(e).hasMessageContaining("<String that contains xxx>");
    }
  }
コード例 #14
0
ファイル: CustomMatchersTest.java プロジェクト: bric3/mockito
  @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");
    }
  }
コード例 #15
0
 @Test
 public void testInjectMocks() throws Exception {
   when(mock.simpleMethod(1)).thenReturn("1");
   mock.simpleMethod(2);
 }
コード例 #16
0
 @Test(expected = NotAMockException.class)
 public void shouldScreamWhenWholeMethodPassedToVerifyNoMoreInteractions() {
   verifyNoMoreInteractions(mock.byteReturningMethod());
 }
コード例 #17
0
 @Test(expected = NotAMockException.class)
 public void shouldScreamWhenWholeMethodPassedToVerify() {
   verify(mock.booleanReturningMethod());
 }
コード例 #18
0
ファイル: CustomMatchersTest.java プロジェクト: bric3/mockito
  @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"));
  }
コード例 #19
0
ファイル: ResetTest.java プロジェクト: nathansgreen/mockito
 @Test
 public void shouldRemoveAllInteractions() throws Exception {
   mock.simpleMethod(1);
   reset(mock);
   verifyZeroInteractions(mock);
 }