org.mockito.exceptions.verification.WantedButNotInvoked Java Examples

The following examples show how to use org.mockito.exceptions.verification.WantedButNotInvoked. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Reporter.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void wantedButNotInvoked(DescribedInvocation wanted, List<? extends DescribedInvocation> invocations) {
    String allInvocations;
    if (invocations.isEmpty()) {
        allInvocations = "Actually, there were zero interactions with this mock.\n";
    } else {
        StringBuilder sb = new StringBuilder("\nHowever, there were other interactions with this mock:\n");
        for (DescribedInvocation i : invocations) {
            sb.append(i.toString())
                    .append("\n")
                    .append(i.getLocation())
                    .append("\n\n");
        }
        allInvocations = sb.toString();
    }

    String message = createWantedButNotInvokedMessage(wanted);
    throw new WantedButNotInvoked(message + allInvocations);
}
 
Example #2
Source File: Reporter.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void wantedButNotInvoked(PrintableInvocation wanted, List<? extends PrintableInvocation> invocations) {
    String allInvocations;
    if (invocations.isEmpty()) {
        allInvocations = "Actually, there were zero interactions with this mock.\n";
    } else {
        StringBuilder sb = new StringBuilder("\nHowever, there were other interactions with this mock:\n");
        for (PrintableInvocation i : invocations) {
             sb.append(i.getLocation());
             sb.append("\n");
        }
        allInvocations = sb.toString();
    }

    String message = createWantedButNotInvokedMessage(wanted);
    throw new WantedButNotInvoked(message + allInvocations);
}
 
Example #3
Source File: DescriptiveMessagesWhenVerificationFailsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
@Ignore("issue 380 related")
public void should_print_method_name_and_arguments_of_other_interactions_of_same_method() throws Exception {
    try {
        mock.forByte((byte) 25);
        mock.forByte((byte) 12);

        verify(mock).forByte((byte) 42);
        fail();
    } catch (WantedButNotInvoked e) {
        System.out.println(e);
        assertContains("iMethods.forByte(42)", e.getMessage());
        assertContains("iMethods.forByte(25)", e.getMessage());
        assertContains("iMethods.forByte(12)", e.getMessage());
    }
}
 
Example #4
Source File: MatchersTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void shouldArrayEqualsDealWithNullArray() throws Exception {
    Object[] nullArray = null;
    when(mock.oneArray(aryEq(nullArray))).thenReturn("null");

    assertEquals("null", mock.oneArray(nullArray));

    mock = mock(IMethods.class);

    try {
        verify(mock).oneArray(aryEq(nullArray));
        fail();
    } catch (WantedButNotInvoked e) {
        assertContains("oneArray(null)", e.getMessage());
    }
}
 
Example #5
Source File: TestNonAggregatingLogHandler.java    From big-c with Apache License 2.0 6 votes vote down vote up
/**
 * Function to verify that the DeletionService object received the right
 * requests.
 * 
 * @param delService the DeletionService mock which we verify against
 * 
 * @param user the user name to use when verifying the deletion
 * 
 * @param timeout amount in milliseconds to wait before we decide the calls
 * didn't come through
 * 
 * @param matchPaths the paths to match in the delete calls
 * 
 * @throws WantedButNotInvoked if the calls could not be verified
 */
static void testDeletionServiceCall(DeletionService delService, String user,
    long timeout, Path... matchPaths) {

  long verifyStartTime = System.currentTimeMillis();
  WantedButNotInvoked notInvokedException = null;
  boolean matched = false;
  while (!matched && System.currentTimeMillis() < verifyStartTime + timeout) {
    try {
      verify(delService).delete(eq(user), (Path) eq(null),
        Mockito.argThat(new DeletePathsMatcher(matchPaths)));
      matched = true;
    } catch (WantedButNotInvoked e) {
      notInvokedException = e;
      try {
        Thread.sleep(50l);
      } catch (InterruptedException i) {
      }
    }
  }
  if (!matched) {
    throw notInvokedException;
  }
  return;
}
 
Example #6
Source File: DescriptiveMessagesWhenVerificationFailsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void should_print_method_name() {
    try {
        verify(mock).simpleMethod();
        fail();
    } catch (WantedButNotInvoked e) {
        String actualMessage = e.getMessage();
        String expectedMessage =
                "\n" +
                "Wanted but not invoked:" +
                "\n" +
                "iMethods.simpleMethod();" +
                "\n" +
                "-> at";
        assertContains(expectedMessage, actualMessage);
    }
}
 
Example #7
Source File: VerificationUsingMatchersTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void shouldVerifyUsingSameMatcher() {
    Object one = new String("1243");
    Object two = new String("1243");
    Object three = new String("1243");

    assertNotSame(one, two);
    assertEquals(one, two);
    assertEquals(two, three);

    mock.oneArg(one);
    mock.oneArg(two);
    
    verify(mock).oneArg(same(one));
    verify(mock, times(2)).oneArg(two);
    
    try {
        verify(mock).oneArg(same(three));
        fail();
    } catch (WantedButNotInvoked e) {}
}
 
Example #8
Source File: DescriptiveMessagesOnVerificationInOrderErrorsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void shouldPrintVerificationInOrderErrorAndShowWantedOnly() {
    try {
        inOrder.verify(one).differentMethod();
        fail();
    } catch (WantedButNotInvoked e) {
        String expected = 
                "\n" +
                "Wanted but not invoked:" +
                "\n" +
                "iMethods.differentMethod();" +
                "\n" +
                "-> at"; 
        
        assertContains(expected, e.getMessage());
    }
}
 
Example #9
Source File: DescriptiveMessagesOnVerificationInOrderErrorsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void shouldPrintVerificationInOrderErrorAndShowWantedOnly() {
    try {
        inOrder.verify(one).differentMethod();
        fail();
    } catch (WantedButNotInvoked e) {
        String expected = 
                "\n" +
                "Wanted but not invoked:" +
                "\n" +
                "iMethods.differentMethod();" +
                "\n" +
                "-> at"; 
        
        assertContains(expected, e.getMessage());
    }
}
 
Example #10
Source File: VerificationUsingMatchersTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void shouldVerifyUsingSameMatcher() {
    Object one = new String("1243");
    Object two = new String("1243");
    Object three = new String("1243");

    assertNotSame(one, two);
    assertEquals(one, two);
    assertEquals(two, three);

    mock.oneArg(one);
    mock.oneArg(two);
    
    verify(mock).oneArg(same(one));
    verify(mock, times(2)).oneArg(two);
    
    try {
        verify(mock).oneArg(same(three));
        fail();
    } catch (WantedButNotInvoked e) {}
}
 
Example #11
Source File: DescriptiveMessagesWhenVerificationFailsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void shouldPrintMethodName() {
    try {
        verify(mock).simpleMethod();
        fail();
    } catch (WantedButNotInvoked e) {
        String actualMessage = e.getMessage();
        String expectedMessage =
                "\n" +
                "Wanted but not invoked:" +
                "\n" +
                "iMethods.simpleMethod();" +
                "\n" +
                "-> at";
        assertContains(expectedMessage, actualMessage);
    }
}
 
Example #12
Source File: MatchersTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void shouldArrayEqualsDealWithNullArray() throws Exception {
    Object[] nullArray = null;
    when(mock.oneArray(aryEq(nullArray))).thenReturn("null");

    assertEquals("null", mock.oneArray(nullArray));

    mock = mock(IMethods.class);

    try {
        verify(mock).oneArray(aryEq(nullArray));
        fail();
    } catch (WantedButNotInvoked e) {
        assertContains("oneArray(null)", e.getMessage());
    }
}
 
Example #13
Source File: TestNonAggregatingLogHandler.java    From hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * Function to verify that the DeletionService object received the right
 * requests.
 * 
 * @param delService the DeletionService mock which we verify against
 * 
 * @param user the user name to use when verifying the deletion
 * 
 * @param timeout amount in milliseconds to wait before we decide the calls
 * didn't come through
 * 
 * @param matchPaths the paths to match in the delete calls
 * 
 * @throws WantedButNotInvoked if the calls could not be verified
 */
static void testDeletionServiceCall(DeletionService delService, String user,
    long timeout, Path... matchPaths) {

  long verifyStartTime = System.currentTimeMillis();
  WantedButNotInvoked notInvokedException = null;
  boolean matched = false;
  while (!matched && System.currentTimeMillis() < verifyStartTime + timeout) {
    try {
      verify(delService).delete(eq(user), (Path) eq(null),
        Mockito.argThat(new DeletePathsMatcher(matchPaths)));
      matched = true;
    } catch (WantedButNotInvoked e) {
      notInvokedException = e;
      try {
        Thread.sleep(50l);
      } catch (InterruptedException i) {
      }
    }
  }
  if (!matched) {
    throw notInvokedException;
  }
  return;
}
 
Example #14
Source File: OnlyVerificationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldFailIfMethodWasNotInvoked() {
	mock.clear();
	try {
		verify(mock, only()).get(0);
		fail();
	} catch (WantedButNotInvoked e) {}
}
 
Example #15
Source File: DescriptiveMessagesWhenVerificationFailsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void should_print_method_name_and_arguments_of_other_interactions_with_different_methods() throws Exception {
    try {
        mock.arrayMethod(new String[] {"a", "b", "c"});
        mock.forByte((byte) 25);

        verify(mock).threeArgumentMethod(12, new Foo(), "xx");
        fail();
    } catch (WantedButNotInvoked e) {
        System.out.println(e);
        assertContains("iMethods.threeArgumentMethod(12, foo, \"xx\")", e.getMessage());
        assertContains("iMethods.arrayMethod([\"a\", \"b\", \"c\"])", e.getMessage());
        assertContains("iMethods.forByte(25)", e.getMessage());
    }
}
 
Example #16
Source File: DescriptiveMessagesWhenVerificationFailsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void should_print_method_name_and_arguments() {
    try {
        verify(mock).threeArgumentMethod(12, new Foo(), "xx");
        fail();
    } catch (WantedButNotInvoked e) {
        assertContains("iMethods.threeArgumentMethod(12, foo, \"xx\")", e.getMessage());
    }
}
 
Example #17
Source File: DescriptiveMessagesWhenVerificationFailsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void should_print_method_name_when_verifying_at_least_once() throws Exception {
    try {
        verify(mock, atLeastOnce()).twoArgumentMethod(1, 2);
        fail();
    } catch (WantedButNotInvoked e) {
        assertContains("twoArgumentMethod(1, 2)", e.getMessage());
    }
}
 
Example #18
Source File: DescriptiveMessagesWhenVerificationFailsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void should_print_method_when_matcher_used() throws Exception {
    try {
        verify(mock, atLeastOnce()).twoArgumentMethod(anyInt(), eq(100));
        fail();
    } catch (WantedButNotInvoked e) {
        String actualMessage = e.getMessage();
        String expectedMessage =
            "\n" +
            "Wanted but not invoked:" +
            "\n" +
            "iMethods.twoArgumentMethod(<any>, 100);";
        assertContains(expectedMessage, actualMessage);
    }
}
 
Example #19
Source File: DescriptiveMessagesWhenVerificationFailsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
    public void should_print_interactions_on_mock_when_ordinary_verification_fail() throws Exception {
        mock.otherMethod();
        mock.booleanReturningMethod();
        
        try {
            verify(mock).simpleMethod();
            fail();
        } catch (WantedButNotInvoked e) {
//            assertContains("")
        }
    }
 
Example #20
Source File: VerificationInOrderTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldMessagesPointToProperMethod() {
    mockTwo.differentMethod();
    mockOne.simpleMethod();
    
    try {
        inOrder.verify(mockOne, atLeastOnce()).differentMethod();
        fail();
    } catch (WantedButNotInvoked e) {
        assertContains("differentMethod()", e.getMessage());
    }
}
 
Example #21
Source File: OrdinaryVerificationPrintsAllInteractionsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldNotShowAllInteractionsOnDifferentMock() throws Exception {
    differentMockInteraction();
    firstInteraction();
    
    try {
        verify(mock).simpleMethod();
        fail();
    } catch (WantedButNotInvoked e) {
        assertContains("firstInteraction(", e.getMessage());
        assertNotContains("differentMockInteraction(", e.getMessage());
    }
}
 
Example #22
Source File: OrdinaryVerificationPrintsAllInteractionsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldShowAllInteractionsOnMockWhenOrdinaryVerificationFail() throws Exception {
    firstInteraction();
    secondInteraction();
    
    try {
        verify(mock).simpleMethod();
        fail();
    } catch (WantedButNotInvoked e) {
        assertContains("However, there were other interactions with this mock", e.getMessage());
        assertContains("firstInteraction(", e.getMessage());
        assertContains("secondInteraction(", e.getMessage());
    }
}
 
Example #23
Source File: ExactNumberOfTimesVerificationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldDetectActualInvocationsCountIsMoreThanZero() throws Exception {
    verify(mock, times(0)).clear();
    try {
        verify(mock, times(15)).clear();
        fail();
    } catch (WantedButNotInvoked e) {}
}
 
Example #24
Source File: StackTraceFilteringTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldFilterStackTraceOnVerify() {
    try {
        verify(mock).simpleMethod();
        fail();
    } catch (WantedButNotInvoked e) {
        assertThat(e, hasFirstMethodInStackTrace("shouldFilterStackTraceOnVerify"));
    }
}
 
Example #25
Source File: OverloadingPuzzleTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldUseArgumentTypeWhenOverloadingPuzzleDetected() throws Exception {
    Sub sub = mock(Sub.class);
    setMockWithDowncast(sub);
    say("Hello");
    try {
        verify(sub).say("Hello");
        fail();
    } catch (WantedButNotInvoked e) {}
}
 
Example #26
Source File: SmartNullsStubbingTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldNotThrowSmartNullPointerOnToString() {
    Object smartNull = mock.objectReturningMethod();
    try {
        verify(mock).simpleMethod(smartNull);
        fail();
    } catch (WantedButNotInvoked e) {}
}
 
Example #27
Source File: MatchersTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void deltaMatcherPrintsItself() {
    try {
        verify(mock).oneArg(eq(1.0D, 0.1D));
        fail();
    } catch (WantedButNotInvoked e) {
        assertContains("eq(1.0, 0.1)", e.getMessage());
    }
}
 
Example #28
Source File: CapturingArgumentsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void should_print_captor_matcher() {
    //given
    ArgumentCaptor<Person> person = ArgumentCaptor.forClass(Person.class);
    
    try {
        //when
        verify(emailService).sendEmailTo(person.capture());
        fail();
    } catch(WantedButNotInvoked e) {
        //then
        assertContains("<Capturing argument>", e.getMessage());
    }
}
 
Example #29
Source File: BasicVerificationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldDetectWhenOverloadedMethodCalled() throws Exception {
    IMethods mockThree = mock(IMethods.class);
    
    mockThree.varargs((Object[]) new Object[] {});
    try {
        verify(mockThree).varargs((String[]) new String[] {});
        fail();
    } catch(WantedButNotInvoked e) {}
}
 
Example #30
Source File: OrdinaryVerificationPrintsAllInteractionsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldNotShowAllInteractionsHeaderWhenNoOtherInteractions() throws Exception {
    try {
        verify(mock).simpleMethod();
        fail();
    } catch (WantedButNotInvoked e) {
        assertContains("there were zero interactions with this mock.", e.getMessage());
    }
}