How to use Mockito.mock method of org.mockito.internal.invocation.InvocationBuilder class

Best Mockito code snippet using org.mockito.internal.invocation.InvocationBuilder.Mockito.mock

Source:InvocationMatcherTest.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.invocation;6import static java.util.Arrays.asList;7import static org.assertj.core.api.Assertions.assertThat;8import static org.junit.Assert.assertEquals;9import static org.junit.Assert.assertFalse;10import static org.junit.Assert.assertTrue;11import static org.mockito.internal.matchers.Any.ANY;12import java.lang.reflect.Method;13import java.util.Arrays;14import java.util.HashMap;15import java.util.List;16import java.util.Map;17import org.assertj.core.api.Assertions;18import org.junit.Before;19import org.junit.Test;20import org.mockito.ArgumentMatcher;21import org.mockito.Mock;22import org.mockito.internal.matchers.CapturingMatcher;23import org.mockito.internal.matchers.Equals;24import org.mockito.internal.matchers.NotNull;25import org.mockito.invocation.Invocation;26import org.mockitousage.IMethods;27import org.mockitoutil.TestBase;28@SuppressWarnings("unchecked")29public class InvocationMatcherTest extends TestBase {30 private InvocationMatcher simpleMethod;31 @Mock private IMethods mock;32 @Before33 public void setup() {34 simpleMethod = new InvocationBuilder().mock(mock).simpleMethod().toInvocationMatcher();35 }36 @Test37 public void should_be_a_citizen_of_hashes() throws Exception {38 Invocation invocation = new InvocationBuilder().toInvocation();39 Invocation invocationTwo = new InvocationBuilder().args("blah").toInvocation();40 Map<InvocationMatcher, String> map = new HashMap<InvocationMatcher, String>();41 map.put(new InvocationMatcher(invocation), "one");42 map.put(new InvocationMatcher(invocationTwo), "two");43 assertEquals(2, map.size());44 }45 @Test46 public void should_not_equal_if_number_of_arguments_differ() throws Exception {47 InvocationMatcher withOneArg =48 new InvocationMatcher(new InvocationBuilder().args("test").toInvocation());49 InvocationMatcher withTwoArgs =50 new InvocationMatcher(new InvocationBuilder().args("test", 100).toInvocation());51 assertFalse(withOneArg.equals(null));52 assertFalse(withOneArg.equals(withTwoArgs));53 }54 @Test55 public void should_to_string_with_matchers() throws Exception {56 ArgumentMatcher m = NotNull.NOT_NULL;57 InvocationMatcher notNull =58 new InvocationMatcher(new InvocationBuilder().toInvocation(), asList(m));59 ArgumentMatcher mTwo = new Equals('x');60 InvocationMatcher equals =61 new InvocationMatcher(new InvocationBuilder().toInvocation(), asList(mTwo));62 assertThat(notNull.toString()).contains("simpleMethod(notNull())");63 assertThat(equals.toString()).contains("simpleMethod('x')");64 }65 @Test66 public void should_know_if_is_similar_to() throws Exception {67 Invocation same = new InvocationBuilder().mock(mock).simpleMethod().toInvocation();68 assertTrue(simpleMethod.hasSimilarMethod(same));69 Invocation different = new InvocationBuilder().mock(mock).differentMethod().toInvocation();70 assertFalse(simpleMethod.hasSimilarMethod(different));71 }72 @Test73 public void should_not_be_similar_to_verified_invocation() throws Exception {74 Invocation verified = new InvocationBuilder().simpleMethod().verified().toInvocation();75 assertFalse(simpleMethod.hasSimilarMethod(verified));76 }77 @Test78 public void should_not_be_similar_if_mocks_are_different() throws Exception {79 Invocation onDifferentMock =80 new InvocationBuilder().simpleMethod().mock("different mock").toInvocation();81 assertFalse(simpleMethod.hasSimilarMethod(onDifferentMock));82 }83 @Test84 public void should_not_be_similar_if_is_overloaded_but_used_with_the_same_arg()85 throws Exception {86 Method method = IMethods.class.getMethod("simpleMethod", String.class);87 Method overloadedMethod = IMethods.class.getMethod("simpleMethod", Object.class);88 String sameArg = "test";89 InvocationMatcher invocation =90 new InvocationBuilder().method(method).arg(sameArg).toInvocationMatcher();91 Invocation overloadedInvocation =92 new InvocationBuilder().method(overloadedMethod).arg(sameArg).toInvocation();93 assertFalse(invocation.hasSimilarMethod(overloadedInvocation));94 }95 @Test96 public void should_be_similar_if_is_overloaded_but_used_with_different_arg() throws Exception {97 Method method = IMethods.class.getMethod("simpleMethod", String.class);98 Method overloadedMethod = IMethods.class.getMethod("simpleMethod", Object.class);99 InvocationMatcher invocation =100 new InvocationBuilder().mock(mock).method(method).arg("foo").toInvocationMatcher();101 Invocation overloadedInvocation =102 new InvocationBuilder()103 .mock(mock)104 .method(overloadedMethod)105 .arg("bar")106 .toInvocation();107 assertTrue(invocation.hasSimilarMethod(overloadedInvocation));108 }109 @Test110 public void should_capture_arguments_from_invocation() throws Exception {111 // given112 Invocation invocation = new InvocationBuilder().args("1", 100).toInvocation();113 CapturingMatcher capturingMatcher = new CapturingMatcher();114 InvocationMatcher invocationMatcher =115 new InvocationMatcher(invocation, (List) asList(new Equals("1"), capturingMatcher));116 // when117 invocationMatcher.captureArgumentsFrom(invocation);118 // then119 assertEquals(1, capturingMatcher.getAllValues().size());120 assertEquals(100, capturingMatcher.getLastValue());121 }122 @Test123 public void should_match_varargs_using_any_varargs() throws Exception {124 // given125 mock.varargs("1", "2");126 Invocation invocation = getLastInvocation();127 InvocationMatcher invocationMatcher = new InvocationMatcher(invocation, (List) asList(ANY));128 // when129 boolean match = invocationMatcher.matches(invocation);130 // then131 assertTrue(match);132 }133 @Test134 public void should_capture_varargs_as_vararg() throws Exception {135 // given136 mock.mixedVarargs(1, "a", "b");137 Invocation invocation = getLastInvocation();138 CapturingMatcher m = new CapturingMatcher();139 InvocationMatcher invocationMatcher =140 new InvocationMatcher(invocation, Arrays.<ArgumentMatcher>asList(new Equals(1), m));141 // when142 invocationMatcher.captureArgumentsFrom(invocation);143 // then144 Assertions.assertThat(m.getAllValues()).containsExactly("a", "b");145 }146 @Test // like using several time the captor in the vararg147 public void should_capture_arguments_when_args_count_does_NOT_match() throws Exception {148 // given149 mock.varargs();150 Invocation invocation = getLastInvocation();151 // when152 InvocationMatcher invocationMatcher = new InvocationMatcher(invocation, (List) asList(ANY));153 // then154 invocationMatcher.captureArgumentsFrom(invocation);155 }156 @Test157 public void should_create_from_invocations() throws Exception {158 // given159 Invocation i = new InvocationBuilder().toInvocation();160 // when161 List<InvocationMatcher> out = InvocationMatcher.createFrom(asList(i));162 // then163 assertEquals(1, out.size());164 assertEquals(i, out.get(0).getInvocation());165 }166}...

Full Screen

Full Screen

Source:MissingInvocationCheckerTest.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 static java.util.Arrays.asList;7import static java.util.Collections.singletonList;8import static org.assertj.core.api.Assertions.assertThatThrownBy;9import java.util.ArrayList;10import java.util.List;11import org.junit.Test;12import org.mockito.ArgumentMatcher;13import org.mockito.Mock;14import org.mockito.exceptions.verification.WantedButNotInvoked;15import org.mockito.exceptions.verification.opentest4j.ArgumentsAreDifferent;16import org.mockito.internal.invocation.InterceptedInvocation;17import org.mockito.internal.invocation.InvocationBuilder;18import org.mockito.internal.invocation.InvocationMatcher;19import org.mockito.internal.invocation.MockitoMethod;20import org.mockito.internal.invocation.RealMethod;21import org.mockito.internal.invocation.mockref.MockReference;22import org.mockito.invocation.Invocation;23import org.mockito.invocation.Location;24import org.mockitousage.IMethods;25import org.mockitoutil.TestBase;26public class MissingInvocationCheckerTest extends TestBase {27 private InvocationMatcher wanted;28 private List<Invocation> invocations;29 @Mock private IMethods mock;30 @Test31 public void shouldPassBecauseActualInvocationFound() {32 wanted = buildSimpleMethod().toInvocationMatcher();33 invocations = asList(buildSimpleMethod().toInvocation());34 MissingInvocationChecker.checkMissingInvocation(invocations, wanted);35 }36 @Test37 public void shouldReportWantedButNotInvoked() {38 wanted = buildSimpleMethod().toInvocationMatcher();39 invocations = asList(buildDifferentMethod().toInvocation());40 assertThatThrownBy(41 () -> {42 MissingInvocationChecker.checkMissingInvocation(invocations, wanted);43 })44 .isInstanceOf(WantedButNotInvoked.class)45 .hasMessageContainingAll(46 "Wanted but not invoked:",47 "mock.simpleMethod()",48 "However, there was exactly 1 interaction with this mock:",49 "mock.differentMethod();");50 }51 @Test52 public void shouldReportWantedInvocationDiffersFromActual() {53 wanted = buildIntArgMethod(new InvocationBuilder()).arg(2222).toInvocationMatcher();54 invocations = asList(buildIntArgMethod(new InvocationBuilder()).arg(1111).toInvocation());55 assertThatThrownBy(56 () -> {57 MissingInvocationChecker.checkMissingInvocation(invocations, wanted);58 })59 .isInstanceOf(ArgumentsAreDifferent.class)60 .hasMessageContainingAll(61 "Argument(s) are different! Wanted:",62 "mock.intArgumentMethod(2222);",63 "Actual invocations have different arguments:",64 "mock.intArgumentMethod(1111);");65 }66 @Test67 public void shouldReportUsingInvocationDescription() {68 wanted = buildIntArgMethod(new CustomInvocationBuilder()).arg(2222).toInvocationMatcher();69 invocations =70 singletonList(71 buildIntArgMethod(new CustomInvocationBuilder()).arg(1111).toInvocation());72 assertThatThrownBy(73 () -> {74 MissingInvocationChecker.checkMissingInvocation(invocations, wanted);75 })76 .isInstanceOf(ArgumentsAreDifferent.class)77 .hasMessageContainingAll(78 "Argument(s) are different! Wanted:",79 "mock.intArgumentMethod(MyCoolPrint(2222));",80 "Actual invocations have different arguments:",81 "mock.intArgumentMethod(MyCoolPrint(1111));");82 }83 private InvocationBuilder buildIntArgMethod(InvocationBuilder invocationBuilder) {84 return invocationBuilder.mock(mock).method("intArgumentMethod").argTypes(int.class);85 }86 private InvocationBuilder buildSimpleMethod() {87 return new InvocationBuilder().mock(mock).simpleMethod();88 }89 private InvocationBuilder buildDifferentMethod() {90 return new InvocationBuilder().mock(mock).differentMethod();91 }92 static class CustomInvocationBuilder extends InvocationBuilder {93 @Override94 protected Invocation createInvocation(95 MockReference<Object> mockRef,96 MockitoMethod mockitoMethod,97 final Object[] arguments,98 RealMethod realMethod,99 Location location,100 int sequenceNumber) {101 return new InterceptedInvocation(102 mockRef, mockitoMethod, arguments, realMethod, location, sequenceNumber) {103 @Override104 public List<ArgumentMatcher> getArgumentsAsMatchers() {105 List<ArgumentMatcher> matchers = new ArrayList<>();106 for (final Object argument : getRawArguments()) {107 matchers.add(108 new ArgumentMatcher() {109 @Override110 public boolean matches(Object a) {111 return a == argument;112 }113 @Override114 public String toString() {115 return "MyCoolPrint(" + argument + ")";116 }117 });118 }119 return matchers;120 }121 };122 }123 }124}...

Full Screen

Full Screen

Source:ReporterTest.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.exceptions;6import static org.mockito.Mockito.mock;7import java.lang.reflect.Field;8import java.util.Collections;9import org.junit.Test;10import org.mockito.Mockito;11import org.mockito.exceptions.base.MockitoException;12import org.mockito.exceptions.verification.NoInteractionsWanted;13import org.mockito.exceptions.verification.TooFewActualInvocations;14import org.mockito.exceptions.verification.VerificationInOrderFailure;15import org.mockito.internal.invocation.InvocationBuilder;16import org.mockito.internal.stubbing.answers.Returns;17import org.mockito.invocation.Invocation;18import org.mockitousage.IMethods;19import org.mockitoutil.TestBase;20public class ReporterTest extends TestBase {21 @Test(expected = TooFewActualInvocations.class)22 public void should_let_passing_null_last_actual_stack_trace() throws Exception {23 throw Reporter.tooFewActualInvocations(24 new org.mockito.internal.reporting.Discrepancy(1, 2),25 new InvocationBuilder().toInvocation(),26 null);27 }28 @Test(expected = MockitoException.class)29 public void should_throw_correct_exception_for_null_invocation_listener() throws Exception {30 throw Reporter.methodDoesNotAcceptParameter("invocationListeners", "null vararg array");31 }32 @Test(expected = NoInteractionsWanted.class)33 public void34 can_use_mock_name_even_when_mock_bogus_default_answer_and_when_reporting_no_more_interaction_wanted()35 throws Exception {36 Invocation invocation_with_bogus_default_answer =37 new InvocationBuilder()38 .mock(mock(IMethods.class, new Returns(false)))39 .toInvocation();40 throw Reporter.noMoreInteractionsWanted(41 invocation_with_bogus_default_answer,42 Collections.<VerificationAwareInvocation>emptyList());43 }44 @Test(expected = VerificationInOrderFailure.class)45 public void46 can_use_print_mock_name_even_when_mock_bogus_default_answer_and_when_reporting_no_more_interaction_wanted_in_order()47 throws Exception {48 Invocation invocation_with_bogus_default_answer =49 new InvocationBuilder()50 .mock(mock(IMethods.class, new Returns(false)))51 .toInvocation();52 throw Reporter.noMoreInteractionsWantedInOrder(invocation_with_bogus_default_answer);53 }54 @Test(expected = MockitoException.class)55 public void56 can_use_print_mock_name_even_when_mock_bogus_default_answer_and_when_reporting_invalid_argument_position()57 throws Exception {58 Invocation invocation_with_bogus_default_answer =59 new InvocationBuilder()60 .mock(mock(IMethods.class, new Returns(false)))61 .toInvocation();62 throw Reporter.invalidArgumentPositionRangeAtInvocationTime(63 invocation_with_bogus_default_answer, true, 0);64 }65 @Test(expected = MockitoException.class)66 public void67 can_use_print_mock_name_even_when_mock_bogus_default_answer_and_when_reporting_wrong_argument_to_return()68 throws Exception {69 Invocation invocation_with_bogus_default_answer =70 new InvocationBuilder()71 .mock(mock(IMethods.class, new Returns(false)))72 .toInvocation();73 throw Reporter.wrongTypeOfArgumentToReturn(74 invocation_with_bogus_default_answer, "", String.class, 0);75 }76 @Test(expected = MockitoException.class)77 public void78 can_use_print_mock_name_even_when_mock_bogus_default_answer_and_when_reporting_delegate_method_dont_exists()79 throws Exception {80 Invocation dumb_invocation = new InvocationBuilder().toInvocation();81 IMethods mock_with_bogus_default_answer = mock(IMethods.class, new Returns(false));82 throw Reporter.delegatedMethodDoesNotExistOnDelegate(83 dumb_invocation.getMethod(), mock_with_bogus_default_answer, String.class);84 }85 @Test(expected = MockitoException.class)86 public void87 can_use_print_mock_name_even_when_mock_bogus_default_answer_and_when_reporting_delegate_method_has_wrong_return_type()88 throws Exception {89 Invocation dumb_invocation = new InvocationBuilder().toInvocation();90 IMethods mock_with_bogus_default_answer = mock(IMethods.class, new Returns(false));91 throw Reporter.delegatedMethodHasWrongReturnType(92 dumb_invocation.getMethod(),93 dumb_invocation.getMethod(),94 mock_with_bogus_default_answer,95 String.class);96 }97 @Test(expected = MockitoException.class)98 public void99 can_use_print_mock_name_even_when_mock_bogus_default_answer_and_when_reporting_injection_failure()100 throws Exception {101 IMethods mock_with_bogus_default_answer = mock(IMethods.class, new Returns(false));102 throw Reporter.cannotInjectDependency(103 someField(), mock_with_bogus_default_answer, new Exception());104 }105 private Field someField() {106 return Mockito.class.getDeclaredFields()[0];107 }108}...

Full Screen

Full Screen

Source:InvocationBuilder.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.invocation;6import static java.util.Arrays.asList;7import static org.mockito.internal.invocation.InterceptedInvocation.NO_OP;8import java.lang.reflect.Method;9import java.util.LinkedList;10import java.util.List;11import org.mockito.Mockito;12import org.mockito.internal.debugging.LocationImpl;13import org.mockito.internal.invocation.mockref.MockReference;14import org.mockito.internal.invocation.mockref.MockStrongReference;15import org.mockito.invocation.Invocation;16import org.mockito.invocation.Location;17import org.mockitousage.IMethods;18/**19 * Build an invocation.20 */21@SuppressWarnings("unchecked")22public class InvocationBuilder {23 private String methodName = "simpleMethod";24 private int sequenceNumber = 0;25 private Object[] args = new Object[] {};26 private Object mock = Mockito.mock(IMethods.class);27 private Method method;28 private boolean verified;29 private List<Class<?>> argTypes;30 private Location location;31 /**32 * Build the invocation33 * <p>34 * If the method was not specified, use IMethods methods.35 *36 * @return invocation37 */38 public Invocation toInvocation() {39 if (method == null) {40 if (argTypes == null) {41 argTypes = new LinkedList<Class<?>>();42 for (Object arg : args) {43 if (arg == null) {44 argTypes.add(Object.class);45 } else {46 argTypes.add(arg.getClass());47 }48 }49 }50 try {51 method =52 IMethods.class.getMethod(53 methodName, argTypes.toArray(new Class[argTypes.size()]));54 } catch (Exception e) {55 throw new RuntimeException(56 "builder only creates invocations of IMethods interface", e);57 }58 }59 Invocation i =60 createInvocation(61 new MockStrongReference<Object>(mock, false),62 new SerializableMethod(method),63 args,64 NO_OP,65 location == null ? new LocationImpl() : location,66 1);67 if (verified) {68 i.markVerified();69 }70 return i;71 }72 protected Invocation createInvocation(73 MockReference<Object> mockRef,74 MockitoMethod mockitoMethod,75 Object[] arguments,76 RealMethod realMethod,77 Location location,78 int sequenceNumber) {79 return new InterceptedInvocation(80 mockRef, mockitoMethod, arguments, realMethod, location, sequenceNumber);81 }82 public InvocationBuilder method(String methodName) {83 this.methodName = methodName;84 return this;85 }86 public InvocationBuilder seq(int sequenceNumber) {87 this.sequenceNumber = sequenceNumber;88 return this;89 }90 public InvocationBuilder args(Object... args) {91 this.args = args;92 return this;93 }94 public InvocationBuilder arg(Object o) {95 this.args = new Object[] {o};96 return this;97 }98 public InvocationBuilder mock(Object mock) {99 this.mock = mock;100 return this;101 }102 public InvocationBuilder method(Method method) {103 this.method = method;104 return this;105 }106 public InvocationBuilder verified() {107 this.verified = true;108 return this;109 }110 public InvocationMatcher toInvocationMatcher() {111 return new InvocationMatcher(toInvocation());112 }113 public InvocationBuilder simpleMethod() {114 return this.method("simpleMethod");115 }116 public InvocationBuilder differentMethod() {117 return this.method("differentMethod");118 }119 public InvocationBuilder argTypes(Class<?>... argTypes) {120 this.argTypes = asList(argTypes);121 return this;122 }123 public InvocationBuilder location(final String location) {124 this.location =125 new Location() {126 public String toString() {127 return location;128 }129 public String getSourceFile() {130 return "SomeClass";131 }132 };133 return this;134 }135}...

Full Screen

Full Screen

Source:NoMoreInteractionsTest.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;6import static java.util.Arrays.asList;7import static org.junit.Assert.*;8import static org.mockito.Mockito.mock;9import org.assertj.core.api.Assertions;10import org.junit.Test;11import org.mockito.exceptions.verification.NoInteractionsWanted;12import org.mockito.exceptions.verification.VerificationInOrderFailure;13import org.mockito.internal.creation.MockSettingsImpl;14import org.mockito.internal.invocation.InvocationBuilder;15import org.mockito.internal.invocation.InvocationMatcher;16import org.mockito.internal.stubbing.InvocationContainerImpl;17import org.mockito.internal.verification.api.VerificationDataInOrderImpl;18import org.mockito.invocation.Invocation;19import org.mockitousage.IMethods;20import org.mockitoutil.TestBase;21public class NoMoreInteractionsTest extends TestBase {22 InOrderContextImpl context = new InOrderContextImpl();23 @Test24 public void shouldVerifyInOrder() {25 // given26 NoMoreInteractions n = new NoMoreInteractions();27 Invocation i = new InvocationBuilder().toInvocation();28 assertFalse(context.isVerified(i));29 try {30 // when31 n.verifyInOrder(new VerificationDataInOrderImpl(context, asList(i), null));32 // then33 fail();34 } catch (VerificationInOrderFailure e) {35 }36 }37 @Test38 public void shouldVerifyInOrderAndPass() {39 // given40 NoMoreInteractions n = new NoMoreInteractions();41 Invocation i = new InvocationBuilder().toInvocation();42 context.markVerified(i);43 assertTrue(context.isVerified(i));44 // when45 n.verifyInOrder(new VerificationDataInOrderImpl(context, asList(i), null));46 // then no exception is thrown47 }48 @Test49 public void shouldVerifyInOrderMultipleInvoctions() {50 // given51 NoMoreInteractions n = new NoMoreInteractions();52 Invocation i = new InvocationBuilder().seq(1).toInvocation();53 Invocation i2 = new InvocationBuilder().seq(2).toInvocation();54 // when55 context.markVerified(i2);56 // then no exception is thrown57 n.verifyInOrder(new VerificationDataInOrderImpl(context, asList(i, i2), null));58 }59 @Test60 public void shouldVerifyInOrderMultipleInvoctionsAndThrow() {61 // given62 NoMoreInteractions n = new NoMoreInteractions();63 Invocation i = new InvocationBuilder().seq(1).toInvocation();64 Invocation i2 = new InvocationBuilder().seq(2).toInvocation();65 try {66 // when67 n.verifyInOrder(new VerificationDataInOrderImpl(context, asList(i, i2), null));68 fail();69 } catch (VerificationInOrderFailure e) {70 }71 }72 @Test73 public void noMoreInteractionsExceptionMessageShouldDescribeMock() {74 // given75 NoMoreInteractions n = new NoMoreInteractions();76 IMethods mock = mock(IMethods.class, "a mock");77 InvocationMatcher i = new InvocationBuilder().mock(mock).toInvocationMatcher();78 InvocationContainerImpl invocations = new InvocationContainerImpl(new MockSettingsImpl());79 invocations.setInvocationForPotentialStubbing(i);80 try {81 // when82 n.verify(new VerificationDataImpl(invocations, null));83 // then84 fail();85 } catch (NoInteractionsWanted e) {86 Assertions.assertThat(e.toString()).contains(mock.toString());87 }88 }89 @Test90 public void noMoreInteractionsInOrderExceptionMessageShouldDescribeMock() {91 // given92 NoMoreInteractions n = new NoMoreInteractions();93 IMethods mock = mock(IMethods.class, "a mock");94 Invocation i = new InvocationBuilder().mock(mock).toInvocation();95 try {96 // when97 n.verifyInOrder(new VerificationDataInOrderImpl(context, asList(i), null));98 // then99 fail();100 } catch (VerificationInOrderFailure e) {101 Assertions.assertThat(e.toString()).contains(mock.toString());102 }103 }104}...

Full Screen

Full Screen

Source:MissingInvocationInOrderCheckerTest.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 static java.util.Arrays.asList;7import static org.mockito.internal.verification.checkers.MissingInvocationChecker.checkMissingInvocation;8import java.util.List;9import org.junit.Before;10import org.junit.Rule;11import org.junit.Test;12import org.junit.rules.ExpectedException;13import org.mockito.Mock;14import org.mockito.exceptions.verification.VerificationInOrderFailure;15import org.mockito.exceptions.verification.WantedButNotInvoked;16import org.mockito.exceptions.verification.opentest4j.ArgumentsAreDifferent;17import org.mockito.internal.invocation.InvocationBuilder;18import org.mockito.internal.invocation.InvocationMatcher;19import org.mockito.internal.verification.InOrderContextImpl;20import org.mockito.internal.verification.api.InOrderContext;21import org.mockito.invocation.Invocation;22import org.mockito.junit.MockitoJUnit;23import org.mockito.junit.MockitoRule;24import org.mockitousage.IMethods;25public class MissingInvocationInOrderCheckerTest {26 private InvocationMatcher wanted;27 private List<Invocation> invocations;28 @Mock private IMethods mock;29 @Rule public ExpectedException exception = ExpectedException.none();30 @Rule public MockitoRule mockitoRule = MockitoJUnit.rule();31 private InOrderContext context = new InOrderContextImpl();32 @Before33 public void setup() {}34 @Test35 public void shouldPassWhenMatchingInteractionFound() throws Exception {36 invocations = asList(buildSimpleMethod().toInvocation());37 wanted = buildSimpleMethod().toInvocationMatcher();38 checkMissingInvocation(invocations, wanted, context);39 }40 @Test41 public void shouldReportWantedButNotInvoked() throws Exception {42 invocations = asList(buildDifferentMethod().toInvocation());43 wanted = buildSimpleMethod().toInvocationMatcher();44 exception.expect(WantedButNotInvoked.class);45 exception.expectMessage("Wanted but not invoked:");46 exception.expectMessage("mock.simpleMethod()");47 checkMissingInvocation(invocations, wanted, context);48 }49 @Test50 public void shouldReportArgumentsAreDifferent() throws Exception {51 invocations = asList(buildIntArgMethod().arg(1111).toInvocation());52 wanted = buildIntArgMethod().arg(2222).toInvocationMatcher();53 exception.expect(ArgumentsAreDifferent.class);54 exception.expectMessage("Argument(s) are different! Wanted:");55 exception.expectMessage("mock.intArgumentMethod(2222);");56 exception.expectMessage("Actual invocations have different arguments:");57 exception.expectMessage("mock.intArgumentMethod(1111);");58 checkMissingInvocation(invocations, wanted, context);59 }60 @Test61 public void shouldReportWantedDiffersFromActual() throws Exception {62 Invocation invocation1 = buildIntArgMethod().arg(1111).toInvocation();63 Invocation invocation2 = buildIntArgMethod().arg(2222).toInvocation();64 context.markVerified(invocation2);65 invocations = asList(invocation1, invocation2);66 wanted = buildIntArgMethod().arg(2222).toInvocationMatcher();67 exception.expect(VerificationInOrderFailure.class);68 exception.expectMessage("Verification in order failure");69 exception.expectMessage("Wanted but not invoked:");70 exception.expectMessage("mock.intArgumentMethod(2222);");71 exception.expectMessage("Wanted anywhere AFTER following interaction:");72 exception.expectMessage("mock.intArgumentMethod(2222);");73 checkMissingInvocation(invocations, wanted, context);74 }75 private InvocationBuilder buildIntArgMethod() {76 return new InvocationBuilder().mock(mock).method("intArgumentMethod").argTypes(int.class);77 }78 private InvocationBuilder buildSimpleMethod() {79 return new InvocationBuilder().mock(mock).simpleMethod();80 }81 private InvocationBuilder buildDifferentMethod() {82 return new InvocationBuilder().mock(mock).differentMethod();83 }84}...

Full Screen

Full Screen

Source:WarningsFinderTest.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.debugging;6import static java.util.Arrays.asList;7import static org.mockito.Mockito.only;8import static org.mockito.Mockito.verify;9import java.util.Arrays;10import org.junit.Test;11import org.mockito.Mock;12import org.mockito.internal.invocation.InvocationBuilder;13import org.mockito.internal.invocation.InvocationMatcher;14import org.mockito.invocation.Invocation;15import org.mockitousage.IMethods;16import org.mockitoutil.TestBase;17public class WarningsFinderTest extends TestBase {18 @Mock private IMethods mock;19 @Mock private FindingsListener listener;20 @Test21 public void shouldPrintUnusedStub() {22 // given23 Invocation unusedStub = new InvocationBuilder().simpleMethod().toInvocation();24 // when25 WarningsFinder finder =26 new WarningsFinder(asList(unusedStub), Arrays.<InvocationMatcher>asList());27 finder.find(listener);28 // then29 verify(listener, only()).foundUnusedStub(unusedStub);30 }31 @Test32 public void shouldPrintUnstubbedInvocation() {33 // given34 InvocationMatcher unstubbedInvocation =35 new InvocationBuilder().differentMethod().toInvocationMatcher();36 // when37 WarningsFinder finder =38 new WarningsFinder(39 Arrays.<Invocation>asList(),40 Arrays.<InvocationMatcher>asList(unstubbedInvocation));41 finder.find(listener);42 // then43 verify(listener, only()).foundUnstubbed(unstubbedInvocation);44 }45 @Test46 public void shouldPrintStubWasUsedWithDifferentArgs() {47 // given48 Invocation stub = new InvocationBuilder().arg("foo").mock(mock).toInvocation();49 InvocationMatcher wrongArg =50 new InvocationBuilder().arg("bar").mock(mock).toInvocationMatcher();51 // when52 WarningsFinder finder =53 new WarningsFinder(54 Arrays.<Invocation>asList(stub),55 Arrays.<InvocationMatcher>asList(wrongArg));56 finder.find(listener);57 // then58 verify(listener, only()).foundStubCalledWithDifferentArgs(stub, wrongArg);59 }60}...

Full Screen

Full Screen

Mockito.mock

Using AI Code Generation

copy

Full Screen

1import org.mockito.invocation.InvocationOnMock;2import org.mockito.stubbing.Answer;3import org.mockito.Mockito;4import org.mockito.internal.invocation.InvocationBuilder;5import org.mockito.internal.invocation.Invocation;6import org.mockito.internal.invocation.InvocationMatcher;7import org.mockito.internal.invocation.InvocationImpl;8import org.mockito.internal.invocation.InvocationMatcherImpl;9import

Full Screen

Full Screen

Mockito.mock

Using AI Code Generation

copy

Full Screen

1package org.mockito.internal.invocation;2import org.mockito.internal.invocation.InvocationBuilder;3import org.mockito.internal.invocation.InvocationMarker;4import org.mockito.internal.invocation.InvocationMatcher;5import org.mockito.internal.invocation.InvocationImpl;6import org.mockito.internal.invocation.Invocation;7import org.mockito.internal.invocation.InvocationContainer;8import org.mockito.internal.invocation.InvocationInfo;9import org.mockito.internal.invocation.InvocationChunk;10import org.mockito.internal.invocation.InvocationChunker;11import org.mockito.internal.invocation.InvocationChunkerImpl;12import org.mockito.internal.invocation.InvocationFactory;13import org.mockito.internal.invocation.InvocationFactoryImpl;14import org.mockito.internal.invocation.InvocationMatcherBinder;15import org.mockito.internal.invocation.InvocationMatcherBinderImpl;16import org.mockito.internal.invocation.InvocationOnMock;17import org.mockito.internal.invocation.InvocationOnMockImpl;18import org.mockito.internal.invocation.InvocationResolver;19import org.mockito.internal.invocation.InvocationResolverImpl;20import org.mockito.internal.invocation.InvocationSelector;21import org.mockito.internal.invocation.InvocationSelectorImpl;22import org.mockito.internal.invocation.InvocationStubber;23import org.mockito.internal.invocation.InvocationStubberImpl;24import org.mockito.internal.invocation.InvocationToString;25import org.mockito.internal.invocation.InvocationToStringImpl;26import org.mockito.internal.invocation.InvocationsFinder;27import org.mockito.internal.invocation.InvocationsFinderImpl;28import org.mockito.internal.invocation.InvocationsPrinter;29import org.mockito.internal.invocation.InvocationsPrinterImpl;30import org.mockito.internal.invocation.InvocationsProcessor;31import org.mockito.internal.invocation.InvocationsProcessorImpl;32import org.mockito.internal.invocation.InvocationsPr

Full Screen

Full Screen

Mockito.mock

Using AI Code Generation

copy

Full Screen

1package org.mockito.internal.invocation;2import org.mockito.internal.invocation.InvocationBuilder;3public class MockitoMock {4public static void main(String[] args) {5InvocationBuilder invocationBuilder = new InvocationBuilder();6invocationBuilder.mock(Object.class);7}8}9Exception in thread "main" java.lang.NoSuchMethodError: org.mockito.internal.invocation.InvocationBuilder.mock(Ljava/lang/Class;)Lorg/mockito/internal/invocation/InvocationBuilder;10at org.mockito.internal.invocation.MockitoMock.main(MockitoMock.java:7)11Exception in thread "main" java.lang.NoSuchMethodError: org.mockito.internal.invocation.InvocationBuilder.mock(Ljava/lang/Class;)Lorg/mockito/internal/invocation/InvocationBuilder;12at org.mockito.internal.invocation.MockitoMock.main(MockitoMock.java:7)

Full Screen

Full Screen

Mockito.mock

Using AI Code Generation

copy

Full Screen

1package org.mockito.internal.invocation;2import org.mockito.internal.invocation.InvocationBuilder;3import org.mockito.invocation.Invocation;4public class InvocationBuilderMockitoMock {5 public static void main(String[] args) {6 InvocationBuilder invocationBuilder = new InvocationBuilder();7 Invocation invocation = invocationBuilder.mock(Invocation.class);8 System.out.println(invocation.toString());9 }10}11package org.mockito.internal.invocation;12import org.mockito.internal.invocation.InvocationBuilder;13import org.mockito.invocation.Invocation;14public class InvocationBuilderMockitoWhen {15 public static void main(String[] args) {16 InvocationBuilder invocationBuilder = new InvocationBuilder();17 Invocation invocation = invocationBuilder.when("test").getMock();18 System.out.println(invocation.toString());19 }20}21package org.mockito.internal.invocation;22import org.mockito.internal.invocation.InvocationMatcher;23import org.mockito.invocation.Invocation;24public class InvocationMatcherMockitoMock {25 public static void main(String[] args) {26 InvocationMatcher invocationMatcher = new InvocationMatcher();27 Invocation invocation = invocationMatcher.mock(Invocation.class);28 System.out.println(invocation.toString());29 }30}31package org.mockito.internal.invocation;32import org.mockito.internal.invocation.InvocationMatcher;33import org.mockito.invocation.Invocation;34public class InvocationMatcherMockitoWhen {35 public static void main(String[] args) {36 InvocationMatcher invocationMatcher = new InvocationMatcher();37 Invocation invocation = invocationMatcher.when("test").getMock();38 System.out.println(invocation.toString());39 }40}41package org.mockito.internal.invocation;42import org.mockito.internal.invocation.InvocationMatcherBuilder;43import org.mockito.invocation.Invocation;44public class InvocationMatcherBuilderMockitoMock {45 public static void main(String[] args) {46 InvocationMatcherBuilder invocationMatcherBuilder = new InvocationMatcherBuilder();

Full Screen

Full Screen

Mockito.mock

Using AI Code Generation

copy

Full Screen

1package com.automationrhapsody.junit;2import static org.mockito.Mockito.mock;3import static org.mockito.Mockito.when;4import static org.mockito.Mockito.verify;5import org.junit.Test;6import org.junit.runner.RunWith;7import org.mockito.Mock;8import org.mockito.runners.MockitoJUnitRunner;9import com.automationrhapsody.junit.Calculator;10@RunWith(MockitoJUnitRunner.class)11public class CalculatorTest {12Calculator calculator;13public void testCalculator() {14Calculator mockCalculator = mock(Calculator.class);15when(mockCalculator.add(1, 2)).thenReturn(3);16int result = mockCalculator.add(1, 2);17verify(mockCalculator).add(1, 2);18}19}20Calculator mockCalculator = mock(Calculator.class);21The method mock(Class) is undefined for the type InvocationBuilder

Full Screen

Full Screen

Mockito.mock

Using AI Code Generation

copy

Full Screen

1package org.mockito.internal.invocation;2import org.mockito.internal.invocation.InvocationBuilder;3import org.mockito.internal.invocation.InvocationMatcher;4import org.mockito.internal.invocation.Invocation;5public class InvocationBuilderMockitoMock {6 public static void main(String[] args) {7 InvocationBuilder mock = Mockito.mock(InvocationBuilder.class);8 }9}10package org.mockito.internal.invocation;11import org.mockito.internal.invocation.InvocationBuilder;12import org.mockito.internal.invocation.InvocationMatcher;13import org.mockito.internal.invocation.Invocation;14public class InvocationBuilderMockitoMock {15 public static void main(String[] args) {16 InvocationBuilder mock = Mockito.mock(InvocationBuilder.class);17 }18}19package org.mockito.internal.invocation;20import org.mockito.internal.invocation.InvocationBuilder;21import org.mockito.internal.invocation.InvocationMatcher;22import org.mockito.internal.invocation.Invocation;23public class InvocationBuilderMockitoMock {24 public static void main(String[] args) {25 InvocationBuilder mock = Mockito.mock(InvocationBuilder.class);26 }27}28package org.mockito.internal.invocation;29import org.mockito.internal.invocation.InvocationBuilder;30import org.mockito.internal.invocation.InvocationMatcher;31import org.mockito.internal.invocation.Invocation;32public class InvocationBuilderMockitoMock {33 public static void main(String[] args) {34 InvocationBuilder mock = Mockito.mock(InvocationBuilder.class);35 }36}37package org.mockito.internal.invocation;38import org.mockito.internal.invocation.InvocationBuilder;39import org.mockito.internal.invocation.InvocationMatcher;40import org.mockito.internal.invocation.Invocation;41public class InvocationBuilderMockitoMock {

Full Screen

Full Screen

Mockito.mock

Using AI Code Generation

copy

Full Screen

1public class MockitoMockMethodExample {2 public static void main(String[] args) {3 List mockedList = org.mockito.Mockito.mock(List.class);4 mockedList.add("one");5 mockedList.clear();6 org.mockito.Mockito.verify(mockedList).add("one");7 org.mockito.Mockito.verify(mockedList).clear();8 }9}

Full Screen

Full Screen

Mockito.mock

Using AI Code Generation

copy

Full Screen

1package org.mockito.internal.invocation;2import org.mockito.invocation.Invocation;3import org.mockito.invocation.MockHandler;4public class InvocationBuilder {5 public InvocationBuilder(MockHandler handler) {6 }7 public InvocationBuilder method(String methodName) {8 return this;9 }10 public InvocationBuilder args(Object... args) {11 return this;12 }13 public InvocationBuilder toInvocation() {14 return this;15 }16}17package org.mockito.invocation;18public interface Invocation {19 Object callRealMethod();20 Invocation copy();21 Object[] getArguments();22 MockHandler getMockHandler();23 String getMethodName();24 Object getMock();25 Object getReturnValue();26 StackTraceElement getStackTraceElement();27 boolean isInvoked();28 boolean isVerified();29 boolean isVoid();30 void markInvoked();31 void markVerified();32 void setArguments(Object[] arguments);33 void setMock(Object mock);34 void setReturnValue(Object returnValue);35 void validateMethodInvocationOnMock(Object mock);36}37package org.mockito.invocation;38public interface MockHandler {39 Object handle(Invocation invocation);40}41package org.mockito.invocation;42public class InvocationMatcher implements Invocation {43 public InvocationMatcher(Invocation invocation) {44 }45 public InvocationMatcher(String methodName, Object[] arguments) {46 }47 public InvocationMatcher(String methodName, Object[] arguments, String mockName) {48 }49 public InvocationMatcher(String methodName, Object[] arguments, String mockName, int sequenceNumber) {50 }51 public InvocationMatcher(String methodName, Object[] arguments, String mockName, int sequenceNumber, String toString) {52 }53 public InvocationMatcher(String methodName, Object[] arguments, String mockName, int sequenceNumber, String toString, MockHandler mockHandler) {54 }55 public static InvocationMatcher create(String methodName, Object[] arguments, String mockName) {56 return null;57 }58 public static InvocationMatcher create(String methodName, Object[] arguments, String mockName, int sequenceNumber) {59 return null;60 }61 public static InvocationMatcher create(String methodName, Object[] arguments, String mockName, int sequenceNumber, String toString) {62 return null;63 }64 public static InvocationMatcher create(String methodName, Object[] arguments, String mockName, int sequenceNumber, String toString, MockHandler mockHandler) {65 return null;66 }67 public boolean equals(Object obj

Full Screen

Full Screen

Mockito.mock

Using AI Code Generation

copy

Full Screen

1package org.mockito.internal.invocation;2import org.mockito.invocation.Invocation;3import org.mockito.internal.invocation.InvocationBuilder;4public class MockitoMock {5 public static void main(String[] args) {6 InvocationBuilder builder = new InvocationBuilder();7 Invocation invocation = builder.mock(Invocation.class);8 }9}10package org.mockito.internal.invocation;11import org.mockito.invocation.Invocation;12import org.mockito.internal.invocation.InvocationBuilder;13public class MockitoMock {14 public static void main(String[] args) {15 InvocationBuilder builder = new InvocationBuilder();16 Invocation invocation = builder.mock(Invocation.class);17 }18}19package org.mockito.internal.invocation;20import org.mockito.invocation.Invocation;21import org.mockito.internal.invocation.InvocationBuilder;22public class MockitoMock {23 public static void main(String[] args) {24 InvocationBuilder builder = new InvocationBuilder();25 Invocation invocation = builder.mock(Invocation.class);26 }27}28package org.mockito.internal.invocation;29import org.mockito.invocation.Invocation;30import org.mockito.internal.invocation.InvocationBuilder;31public class MockitoMock {32 public static void main(String[] args) {33 InvocationBuilder builder = new InvocationBuilder();34 Invocation invocation = builder.mock(Invocation.class);35 }36}37package org.mockito.internal.invocation;38import org.mockito.invocation.Invocation;39import org.mockito.internal.invocation.InvocationBuilder;40public class MockitoMock {41 public static void main(String[] args) {42 InvocationBuilder builder = new InvocationBuilder();43 Invocation invocation = builder.mock(Invocation.class);44 }45}46package org.mockito.internal.invocation;47import org.mockito.invocation.Invocation;48import org.mockito.internal.invocation.InvocationBuilder;

Full Screen

Full Screen

Mockito.mock

Using AI Code Generation

copy

Full Screen

1import org.mockito.internal.invocation.InvocationBuilder;2import org.mockito.internal.invocation.InvocationBuilder.Method;3public class TestMockitoMock1 {4 public static void main(String[] args) {5 InvocationBuilder builder = new InvocationBuilder();6 Method method = builder.method("testMethod");7 Object mock = builder.mock(method);8 System.out.println(mock);9 }10}112. Mockito.mock(Class<T> classToMock)12import org.mockito.internal.invocation.InvocationBuilder;13import org.mockito.internal.invocation.InvocationBuilder.Method;14public class TestMockitoMock2 {15 public static void main(String[] args) {16 InvocationBuilder builder = new InvocationBuilder();17 Object mock = builder.mock(TestMockitoMock2.class);18 System.out.println(mock);19 }20}213. Mockito.mock(Class<T> classToMock, Answer defaultAnswer)22import org.mockito.internal.invocation.InvocationBuilder;23import org.mockito.internal.invocation.InvocationBuilder.Method;24import org.mockito.invocation.InvocationOnMock;25import org.mockito.stubbing.Answer;26public class TestMockitoMock3 {27 public static void main(String[] args) {28 InvocationBuilder builder = new InvocationBuilder();29 Object mock = builder.mock(TestMockitoMock3.class, new Answer() {

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.

Run Mockito automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful