How to use ExpectedException.none method of org.mockito.internal.verification.checkers.NumberOfInvocationsCheckerTest class

Best Mockito code snippet using org.mockito.internal.verification.checkers.NumberOfInvocationsCheckerTest.ExpectedException.none

Source:NumberOfInvocationsCheckerTest.java Github

copy

Full Screen

1/**2 * Copyright (c) 2007 Mockito contributors3 * This program is made available under the terms of the MIT License.4 */5package org.mockito.internal.verification.checkers;6import java.util.Arrays;7import java.util.Collections;8import java.util.List;9import org.hamcrest.Description;10import org.hamcrest.TypeSafeMatcher;11import org.junit.Rule;12import org.junit.Test;13import org.junit.rules.ExpectedException;14import org.junit.rules.TestName;15import org.junit.runner.RunWith;16import org.mockito.Mock;17import org.mockito.exceptions.verification.NeverWantedButInvoked;18import org.mockito.exceptions.verification.TooLittleActualInvocations;19import org.mockito.exceptions.verification.TooManyActualInvocations;20import org.mockito.internal.invocation.InvocationMatcher;21import org.mockito.invocation.Invocation;22import org.mockito.junit.MockitoJUnitRunner;23import org.mockitousage.IMethods;24@RunWith(MockitoJUnitRunner.class)25public class NumberOfInvocationsCheckerTest {26 private InvocationMatcher wanted;27 private List<Invocation> invocations;28 @Mock29 private IMethods mock;30 @Rule31 public ExpectedException exception = ExpectedException.none();32 @Rule33 public TestName testName = new TestName();34 @Test35 public void shouldReportTooLittleActual() throws Exception {36 wanted = buildSimpleMethod().toInvocationMatcher();37 invocations = Arrays.asList(buildSimpleMethod().toInvocation(), buildSimpleMethod().toInvocation());38 exception.expect(TooLittleActualInvocations.class);39 exception.expectMessage("mock.simpleMethod()");40 exception.expectMessage("Wanted 100 times");41 exception.expectMessage("But was 2 times");42 NumberOfInvocationsChecker.checkNumberOfInvocations(invocations, wanted, 100);43 }44 @Test45 public void shouldReportAllInvocationsStackTrace() throws Exception {46 wanted = buildSimpleMethod().toInvocationMatcher();47 invocations = Arrays.asList(buildSimpleMethod().toInvocation(), buildSimpleMethod().toInvocation());48 exception.expect(TooLittleActualInvocations.class);49 exception.expectMessage("mock.simpleMethod()");50 exception.expectMessage("Wanted 100 times");51 exception.expectMessage("But was 2 times");52 exception.expectMessage(NumberOfInvocationsCheckerTest.containsTimes("-> at", 3));53 NumberOfInvocationsChecker.checkNumberOfInvocations(invocations, wanted, 100);54 }55 @Test56 public void shouldNotReportWithLastInvocationStackTraceIfNoInvocationsFound() throws Exception {57 invocations = Collections.emptyList();58 wanted = buildSimpleMethod().toInvocationMatcher();59 exception.expect(TooLittleActualInvocations.class);60 exception.expectMessage("mock.simpleMethod()");61 exception.expectMessage("Wanted 100 times");62 exception.expectMessage("But was 0 times");63 exception.expectMessage(NumberOfInvocationsCheckerTest.containsTimes("-> at", 1));64 NumberOfInvocationsChecker.checkNumberOfInvocations(invocations, wanted, 100);65 }66 @Test67 public void shouldReportWithAllInvocationsStackTrace() throws Exception {68 Invocation first = buildSimpleMethod().toInvocation();69 Invocation second = buildSimpleMethod().toInvocation();70 Invocation third = buildSimpleMethod().toInvocation();71 invocations = Arrays.asList(first, second, third);72 wanted = buildSimpleMethod().toInvocationMatcher();73 exception.expect(TooManyActualInvocations.class);74 exception.expectMessage(("" + (first.getLocation())));75 exception.expectMessage(("" + (second.getLocation())));76 exception.expectMessage(("" + (third.getLocation())));77 NumberOfInvocationsChecker.checkNumberOfInvocations(invocations, wanted, 2);78 }79 @Test80 public void shouldReportTooManyActual() throws Exception {81 Invocation first = buildSimpleMethod().toInvocation();82 Invocation second = buildSimpleMethod().toInvocation();83 invocations = Arrays.asList(first, second);84 wanted = buildSimpleMethod().toInvocationMatcher();85 exception.expectMessage("Wanted 1 time");86 exception.expectMessage("But was 2 times");87 NumberOfInvocationsChecker.checkNumberOfInvocations(invocations, wanted, 1);88 }89 @Test90 public void shouldReportNeverWantedButInvoked() throws Exception {91 Invocation first = buildSimpleMethod().toInvocation();92 invocations = Arrays.asList(first);93 wanted = buildSimpleMethod().toInvocationMatcher();94 exception.expect(NeverWantedButInvoked.class);95 exception.expectMessage("Never wanted here");96 exception.expectMessage("But invoked here");97 exception.expectMessage(("" + (first.getLocation())));98 NumberOfInvocationsChecker.checkNumberOfInvocations(invocations, wanted, 0);99 }100 @Test101 public void shouldMarkInvocationsAsVerified() throws Exception {102 Invocation invocation = buildSimpleMethod().toInvocation();103 assertThat(invocation.isVerified()).isFalse();104 invocations = Arrays.asList(invocation);105 wanted = buildSimpleMethod().toInvocationMatcher();106 NumberOfInvocationsChecker.checkNumberOfInvocations(invocations, wanted, 1);107 assertThat(invocation.isVerified()).isTrue();108 }109 private static class StringContainsNumberMatcher extends TypeSafeMatcher<String> {110 private final String expected;111 private final int amount;112 StringContainsNumberMatcher(String expected, int amount) {113 this.expected = expected;114 this.amount = amount;115 }116 public boolean matchesSafely(String text) {117 int lastIndex = 0;118 int count = 0;119 while (lastIndex != (-1)) {120 lastIndex = text.indexOf(expected, lastIndex);121 if (lastIndex != (-1)) {122 count++;123 lastIndex += expected.length();124 }125 } 126 return count == (amount);127 }128 public void describeTo(Description description) {129 description.appendText((((("containing '" + (expected)) + "' exactly ") + (amount)) + " times"));130 }131 }132}...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful