下面列出了怎么用org.mockito.exceptions.verification.VerificationInOrderFailure的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void should_fail_at_updating_tax_factor_and_transfer_tax_in_specified_order_due_to_greedy_at_least() throws Exception {
// given
Person person = new Person();
// when
systemUnderTest.transferTaxFor(person);
// then
InOrder inOrder = Mockito.inOrder(taxService);
inOrder.verify(taxService).updateTaxFactor(eq(person), anyDouble());
inOrder.verify(taxService, atLeastOnce()).transferTaxFor(person);
try {
inOrder.verify(taxService).updateTaxFactor(eq(person), anyDouble());
inOrder.verify(taxService).transferTaxFor(person);
} catch (VerificationInOrderFailure verificationInOrderFailure) {}
}
@Test
public void shouldVerifyInOrderUsingMatcher() {
mockOne.simpleMethod(1);
mockOne.simpleMethod(2);
mockTwo.differentMethod();
mockOne.simpleMethod(3);
mockOne.simpleMethod(4);
verify(mockOne, times(4)).simpleMethod(anyInt());
inOrder.verify(mockOne, times(2)).simpleMethod(anyInt());
inOrder.verify(mockTwo).differentMethod();
inOrder.verify(mockOne, times(2)).simpleMethod(anyInt());
try {
inOrder.verify(mockOne, times(3)).simpleMethod(anyInt());
fail();
} catch (VerificationInOrderFailure e) {}
}
@Test
public void shouldAllowVerifyingInteractionNeverHappenedInOrder() throws Exception {
mock.add("one");
mock.add("two");
InOrder inOrder = inOrder(mock);
inOrder.verify(mock, never()).add("xxx");
inOrder.verify(mock).add("one");
inOrder.verify(mock, never()).add("one");
try {
inOrder.verify(mock, never()).add("two");
fail();
} catch (VerificationInOrderFailure e) {}
}
@Test
public void shouldAllowVerifyingInteractionNeverHappenedInOrder() throws Exception {
mock.add("one");
mock.add("two");
InOrder inOrder = inOrder(mock);
inOrder.verify(mock, never()).add("xxx");
inOrder.verify(mock).add("one");
inOrder.verify(mock, never()).add("one");
try {
inOrder.verify(mock, never()).add("two");
fail();
} catch (VerificationInOrderFailure e) {}
}
@Test
public void shouldPrintMethodThatWasNotInvoked() {
inOrder.verify(one).simpleMethod(1);
inOrder.verify(one).simpleMethod(11);
inOrder.verify(two, times(2)).simpleMethod(2);
inOrder.verify(three).simpleMethod(3);
try {
inOrder.verify(three).simpleMethod(999);
fail();
} catch (VerificationInOrderFailure e) {
String actualMessage = e.getMessage();
String expectedMessage =
"\n" +
"Verification in order failure" +
"\n" +
"Wanted but not invoked:" +
"\n" +
"iMethods.simpleMethod(999);";
assertContains(expectedMessage, actualMessage);
}
}
@Test
public void shouldPrintMethodThatWasNotInvoked() {
inOrder.verify(one).simpleMethod(1);
inOrder.verify(one).simpleMethod(11);
inOrder.verify(two, times(2)).simpleMethod(2);
inOrder.verify(three).simpleMethod(3);
try {
inOrder.verify(three).simpleMethod(999);
fail();
} catch (VerificationInOrderFailure e) {
String actualMessage = e.getMessage();
String expectedMessage =
"\n" +
"Verification in order failure" +
"\n" +
"Wanted but not invoked:" +
"\n" +
"iMethods.simpleMethod(999);";
assertContains(expectedMessage, actualMessage);
}
}
@Test
public void shouldVerifySingleMockInOrderAndNotInOrder() {
mockOne = mock(IMethods.class);
inOrder = inOrder(mockOne);
mockOne.simpleMethod(1);
mockOne.simpleMethod(2);
verify(mockOne).simpleMethod(2);
verify(mockOne).simpleMethod(1);
inOrder.verify(mockOne).simpleMethod(2);
try {
inOrder.verify(mockOne).simpleMethod(1);
fail();
} catch (VerificationInOrderFailure e) {}
}
@Test
public void shouldVerifyInOrderWhenTwoChunksAreEqual() {
mockOne.simpleMethod();
mockOne.simpleMethod();
mockTwo.differentMethod();
mockOne.simpleMethod();
mockOne.simpleMethod();
inOrder.verify(mockOne, times(2)).simpleMethod();
inOrder.verify(mockTwo).differentMethod();
inOrder.verify(mockOne, times(2)).simpleMethod();
try {
inOrder.verify(mockOne, atLeastOnce()).simpleMethod();
fail();
} catch (VerificationInOrderFailure e) {}
}
@Test
public void shouldVerifyInOrderUsingMatcher() {
mockOne.simpleMethod(1);
mockOne.simpleMethod(2);
mockTwo.differentMethod();
mockOne.simpleMethod(3);
mockOne.simpleMethod(4);
verify(mockOne, times(4)).simpleMethod(anyInt());
inOrder.verify(mockOne, times(2)).simpleMethod(anyInt());
inOrder.verify(mockTwo).differentMethod();
inOrder.verify(mockOne, times(2)).simpleMethod(anyInt());
try {
inOrder.verify(mockOne, times(3)).simpleMethod(anyInt());
fail();
} catch (VerificationInOrderFailure e) {}
}
@Test
public void shouldFailOnLastAndFirstInWrongOrder() {
inOrder.verify(mockOne).simpleMethod(4);
try {
inOrder.verify(mockOne).simpleMethod(1);
fail();
} catch (VerificationInOrderFailure e) {}
}
@Test
public void shouldFailWhenMiddleMethodVerifiedFirstInAtLeastOnceMode() {
inOrder.verify(mockTwo, atLeastOnce()).simpleMethod(2);
try {
inOrder.verify(mockOne).simpleMethod(1);
fail();
} catch (VerificationInOrderFailure e) {
}
}
public void wantedButNotInvokedInOrder(PrintableInvocation wanted, PrintableInvocation previous) {
throw new VerificationInOrderFailure(join(
"Verification in order failure",
"Wanted but not invoked:",
wanted.toString(),
new Location(),
"Wanted anywhere AFTER following interaction:",
previous.toString(),
previous.getLocation(),
""
));
}
public void tooLittleActualInvocationsInOrder(Discrepancy discrepancy, PrintableInvocation wanted, Location lastActualLocation) {
String message = createTooLittleInvocationsMessage(discrepancy, wanted, lastActualLocation);
throw new VerificationInOrderFailure(join(
"Verification in order failure:" + message
));
}
public void noMoreInteractionsWantedInOrder(Invocation undesired) {
throw new VerificationInOrderFailure(join(
"No interactions wanted here:",
new Location(),
"But found this interaction:",
undesired.getLocation(),
""
));
}
@Test
public void shouldFailOnSecondMethodBecauseDifferentMethodWanted() {
inOrder.verify(mockOne, times(1)).simpleMethod(1);
try {
inOrder.verify(mockTwo, times(2)).oneArg(true);
fail();
} catch (VerificationInOrderFailure e) {
}
}
@Test
public void shouldPointStackTraceToPreviousVerified() {
inOrder.verify(mock, atLeastOnce()).simpleMethod(anyInt());
inOrder.verify(mockTwo).simpleMethod(anyInt());
try {
inOrder.verify(mock).simpleMethod(999);
fail();
} catch (VerificationInOrderFailure e) {
assertContains("fourth(", e.getMessage());
}
}
@Test
public void shouldPointToThirdMethod() {
inOrder.verify(mock, atLeastOnce()).simpleMethod(anyInt());
try {
inOrder.verify(mockTwo).simpleMethod(999);
fail();
} catch (VerificationInOrderFailure e) {
assertContains("third(", e.getMessage());
}
}
@Test
public void shouldPointToSecondMethod() {
inOrder.verify(mock).simpleMethod(anyInt());
inOrder.verify(mockTwo).simpleMethod(anyInt());
try {
inOrder.verify(mockTwo, times(3)).simpleMethod(999);
fail();
} catch (VerificationInOrderFailure e) {
assertContains("second(", e.getMessage());
}
}
@Test
public void shouldPointToSecondMethodBecauseOfTooManyActualInvocations() {
inOrder.verify(mock).simpleMethod(anyInt());
try {
inOrder.verify(mockTwo, times(0)).simpleMethod(anyInt());
fail();
} catch (VerificationInOrderFailure e) {
assertContains("second(", e.getMessage());
}
}
@Test(expected = VerificationInOrderFailure.class)
public void verifyBadOrder() throws Exception {
MockitoSession session = mockitoSession().mockStatic(EchoClass.class).startMocking();
try {
EchoClass.echo("marco!");
EchoClass.echo("polo");
StaticInOrder echoInOrder = inOrder(staticMockMarker(EchoClass.class));
echoInOrder.verify(() -> EchoClass.echo(eq("polo")));
echoInOrder.verify(() -> EchoClass.echo(eq("marco!")));
} finally {
session.finishMocking();
}
}
@Test
public void shouldPointStackTraceToPreviousInvocation() {
inOrder.verify(mock, times(2)).simpleMethod(anyInt());
inOrder.verify(mockTwo, times(2)).simpleMethod(anyInt());
try {
inOrder.verify(mock).simpleMethod(999);
fail();
} catch (VerificationInOrderFailure e) {
assertContains("secondChunk(", e.getMessage());
}
}
@Test
public void shouldPointToThirdInteractionBecauseAtLeastOnceUsed() {
inOrder.verify(mock, atLeastOnce()).simpleMethod(anyInt());
try {
inOrder.verify(mockTwo).simpleMethod(999);
fail();
} catch (VerificationInOrderFailure e) {
assertContains("thirdChunk(", e.getMessage());
}
}
@Test
public void shouldPointToThirdChunkWhenTooLittleActualInvocations() {
inOrder.verify(mock, times(2)).simpleMethod(anyInt());
inOrder.verify(mockTwo, times(2)).simpleMethod(anyInt());
inOrder.verify(mock, atLeastOnce()).simpleMethod(anyInt());
try {
inOrder.verify(mockTwo, times(3)).simpleMethod(999);
fail();
} catch (VerificationInOrderFailure e) {
assertContains("thirdChunk(", e.getMessage());
}
}
@Test
public void shouldPointToFourthChunkBecauseTooManyActualInvocations() {
inOrder.verify(mock, atLeastOnce()).simpleMethod(anyInt());
try {
inOrder.verify(mockTwo, times(0)).simpleMethod(anyInt());
fail();
} catch (VerificationInOrderFailure e) {
assertContains("fourthChunk(", e.getMessage());
}
}
@Test
public void shouldSayNoInteractionsWanted() throws Exception {
//when
mock.simpleMethod();
//then
InOrder inOrder = inOrder(mock);
try {
inOrder.verifyNoMoreInteractions();
fail();
} catch(VerificationInOrderFailure e) {
assertContains("No interactions wanted", e.getMessage());
}
}
@Test
public void shouldFailToVerifyNoMoreInteractionsInOrder() throws Exception {
//when
mock.simpleMethod();
mock.simpleMethod(10);
mock.otherMethod();
//then
InOrder inOrder = inOrder(mock);
inOrder.verify(mock).simpleMethod(10);
try {
inOrder.verifyNoMoreInteractions();
fail();
} catch(VerificationInOrderFailure e) {}
}
@Test
public void shouldFailToVerifyNoMoreInteractionsInOrderWithMultipleMocks() throws Exception {
//when
mock.simpleMethod();
mock2.simpleMethod();
mock.otherMethod();
//then
InOrder inOrder = inOrder(mock, mock2);
inOrder.verify(mock2).simpleMethod();
try {
inOrder.verifyNoMoreInteractions();
fail();
} catch(VerificationInOrderFailure e) {}
}
@Test
public void shouldFailOnLastInvocationTooEarly() {
inOrder.verify(mockThree).simpleMethod(4);
verify(mockThree).simpleMethod(4);
verify(mockTwo).simpleMethod(2);
try {
inOrder.verify(mockOne, atLeastOnce()).simpleMethod(1);
fail();
} catch (VerificationInOrderFailure e) {}
}
@Test
public void shouldPrintTooManyInvocations() {
inOrder.verify(one).simpleMethod(1);
inOrder.verify(one).simpleMethod(11);
try {
inOrder.verify(two, times(1)).simpleMethod(2);
fail();
} catch (VerificationInOrderFailure e) {
String actualMessage = e.getMessage();
String expectedMessage =
"\n" +
"Verification in order failure:" +
"\n" +
"iMethods.simpleMethod(2);" +
"\n" +
"Wanted 1 time:" +
"\n" +
"-> at";
assertContains(expectedMessage, actualMessage);
String expectedCause =
"\n" +
"But was 2 times. Undesired invocation:" +
"\n" +
"-> at";
assertContains(expectedCause, e.getMessage());
}
}
@Test
public void shouldPrintTooManyInvocations() {
inOrder.verify(one).simpleMethod(1);
inOrder.verify(one).simpleMethod(11);
try {
inOrder.verify(two, times(1)).simpleMethod(2);
fail();
} catch (VerificationInOrderFailure e) {
String actualMessage = e.getMessage();
String expectedMessage =
"\n" +
"Verification in order failure:" +
"\n" +
"iMethods.simpleMethod(2);" +
"\n" +
"Wanted 1 time:" +
"\n" +
"-> at";
assertContains(expectedMessage, actualMessage);
String expectedCause =
"\n" +
"But was 2 times. Undesired invocation:" +
"\n" +
"-> at";
assertContains(expectedCause, e.getMessage());
}
}