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

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

Source:InvocationMatcherTest.java Github

copy

Full Screen

...23 private InvocationMatcher simpleMethod;24 @Mock private IMethods mock;25 @Before26 public void setup() {27 simpleMethod = new InvocationBuilder().mock(mock).simpleMethod().toInvocationMatcher();28 }29 @Test30 public void should_be_a_citizen_of_hashes() throws Exception {31 Invocation invocation = new InvocationBuilder().toInvocation();32 Invocation invocationTwo = new InvocationBuilder().args("blah").toInvocation();33 Map map = new HashMap();34 map.put(new InvocationMatcher(invocation), "one");35 map.put(new InvocationMatcher(invocationTwo), "two");36 assertEquals(2, map.size());37 }38 @Test39 public void should_not_equal_if_number_of_arguments_differ() throws Exception {40 InvocationMatcher withOneArg = new InvocationMatcher(new InvocationBuilder().args("test").toInvocation());41 InvocationMatcher withTwoArgs = new InvocationMatcher(new InvocationBuilder().args("test", 100).toInvocation());42 assertFalse(withOneArg.equals(null));43 assertFalse(withOneArg.equals(withTwoArgs));44 }45 @Test46 public void should_to_string_with_matchers() throws Exception {47 MockitoMatcher m = NotNull.NOT_NULL;48 InvocationMatcher notNull = new InvocationMatcher(new InvocationBuilder().toInvocation(), asList(m));49 MockitoMatcher mTwo = new Equals('x');50 InvocationMatcher equals = new InvocationMatcher(new InvocationBuilder().toInvocation(), asList(mTwo));51 assertContains("simpleMethod(notNull())", notNull.toString());52 assertContains("simpleMethod('x')", equals.toString());53 }54 @Test55 public void should_know_if_is_similar_to() throws Exception {56 Invocation same = new InvocationBuilder().mock(mock).simpleMethod().toInvocation();57 assertTrue(simpleMethod.hasSimilarMethod(same));58 Invocation different = new InvocationBuilder().mock(mock).differentMethod().toInvocation();59 assertFalse(simpleMethod.hasSimilarMethod(different));60 }61 @Test62 public void should_not_be_similar_to_verified_invocation() throws Exception {63 Invocation verified = new InvocationBuilder().simpleMethod().verified().toInvocation();64 assertFalse(simpleMethod.hasSimilarMethod(verified));65 }66 @Test67 public void should_not_be_similar_if_mocks_are_different() throws Exception {68 Invocation onDifferentMock = new InvocationBuilder().simpleMethod().mock("different mock").toInvocation();69 assertFalse(simpleMethod.hasSimilarMethod(onDifferentMock));70 }71 @Test72 public void should_not_be_similar_if_is_overloaded_but_used_with_the_same_arg() throws Exception {73 Method method = IMethods.class.getMethod("simpleMethod", String.class);74 Method overloadedMethod = IMethods.class.getMethod("simpleMethod", Object.class);75 String sameArg = "test";76 InvocationMatcher invocation = new InvocationBuilder().method(method).arg(sameArg).toInvocationMatcher();77 Invocation overloadedInvocation = new InvocationBuilder().method(overloadedMethod).arg(sameArg).toInvocation();78 assertFalse(invocation.hasSimilarMethod(overloadedInvocation));79 }80 @Test81 public void should_be_similar_if_is_overloaded_but_used_with_different_arg() throws Exception {82 Method method = IMethods.class.getMethod("simpleMethod", String.class);83 Method overloadedMethod = IMethods.class.getMethod("simpleMethod", Object.class);84 InvocationMatcher invocation = new InvocationBuilder().mock(mock).method(method).arg("foo").toInvocationMatcher();85 Invocation overloadedInvocation = new InvocationBuilder().mock(mock).method(overloadedMethod).arg("bar").toInvocation();86 assertTrue(invocation.hasSimilarMethod(overloadedInvocation));87 }88 @Test89 public void should_capture_arguments_from_invocation() throws Exception {90 //given91 Invocation invocation = new InvocationBuilder().args("1", 100).toInvocation();92 CapturingMatcher capturingMatcher = new CapturingMatcher();93 InvocationMatcher invocationMatcher = new InvocationMatcher(invocation, (List) asList(new Equals("1"), capturingMatcher));94 //when95 invocationMatcher.captureArgumentsFrom(invocation);96 //then97 assertEquals(1, capturingMatcher.getAllValues().size());98 assertEquals(100, capturingMatcher.getLastValue());99 }100 @Test101 public void should_match_varargs_using_any_varargs() throws Exception {102 //given103 mock.varargs("1", "2");104 Invocation invocation = getLastInvocation();105 InvocationMatcher invocationMatcher = new InvocationMatcher(invocation, (List) asList(AnyVararg.ANY_VARARG));106 //when107 boolean match = invocationMatcher.matches(invocation);108 //then109 assertTrue(match);110 }111 @Test112 public void should_capture_varargs_as_vararg() throws Exception {113 //given114 mock.mixedVarargs(1, "a", "b");115 Invocation invocation = getLastInvocation();116 CapturingMatcher m = new CapturingMatcher();117 InvocationMatcher invocationMatcher = new InvocationMatcher(invocation, (List) asList(new Equals(1), new LocalizedMatcher(m)));118 //when119 invocationMatcher.captureArgumentsFrom(invocation);120 //then121 Assertions.assertThat(m.getAllValues()).containsExactly("a", "b");122 }123 @Test // like using several time the captor in the vararg124 public void should_capture_arguments_when_args_count_does_NOT_match() throws Exception {125 //given126 mock.varargs();127 Invocation invocation = getLastInvocation();128 //when129 InvocationMatcher invocationMatcher = new InvocationMatcher(invocation, (List) asList(new LocalizedMatcher(AnyVararg.ANY_VARARG)));130 //then131 invocationMatcher.captureArgumentsFrom(invocation);132 }133 @Test134 public void should_create_from_invocations() throws Exception {135 //given136 Invocation i = new InvocationBuilder().toInvocation();137 //when138 List<InvocationMatcher> out = InvocationMatcher.createFrom(asList(i));139 //then140 assertEquals(1, out.size());141 assertEquals(i, out.get(0).getInvocation());142 }143}...

Full Screen

Full Screen

Source:InvocationTest.java Github

copy

Full Screen

...20public class InvocationTest extends TestBase {21 private Invocation invocation;22 @Before23 public void setup() throws Exception {24 invocation = new InvocationBuilder().args(" ").mock("mock").toInvocation();25 }26 @Test27 public void shouldKnowIfIsEqualTo() {28 Invocation equal = new InvocationBuilder().args(" ").mock("mock").toInvocation();29 Invocation nonEqual = new InvocationBuilder().args("X").mock("mock").toInvocation();30 Invocation withNewStringInstance = new InvocationBuilder().args(new String(" ")).mock("mock").toInvocation();31 assertFalse(invocation.equals(null));32 assertFalse(invocation.equals(""));33 assertTrue(invocation.equals(equal));34 assertFalse(invocation.equals(nonEqual));35 assertTrue(invocation.equals(withNewStringInstance));36 }37 @Test38 public void shouldEqualToNotConsiderSequenceNumber() {39 Invocation equal = new InvocationBuilder().args(" ").mock("mock").seq(2).toInvocation();40 assertTrue(invocation.equals(equal));41 assertTrue(invocation.getSequenceNumber() != equal.getSequenceNumber());42 }43 @Test44 public void shouldBeACitizenOfHashes() {45 Map map = new HashMap();46 map.put(invocation, "one");47 assertEquals("one", map.get(invocation));48 }49 @Test50 public void shouldPrintMethodName() {51 invocation = new InvocationBuilder().toInvocation();52 assertEquals("iMethods.simpleMethod();", invocation.toString());53 }54 @Test55 public void shouldPrintMethodArgs() {56 invocation = new InvocationBuilder().args("foo").toInvocation();57 assertThat(invocation.toString(), endsWith("simpleMethod(\"foo\");"));58 }59 @Test60 public void shouldPrintMethodIntegerArgAndString() {61 invocation = new InvocationBuilder().args("foo", 1).toInvocation();62 assertThat(invocation.toString(), endsWith("simpleMethod(\"foo\", 1);"));63 }64 @Test65 public void shouldPrintNull() {66 invocation = new InvocationBuilder().args((String) null).toInvocation();67 assertThat(invocation.toString(), endsWith("simpleMethod(null);"));68 }69 @Test70 public void shouldPrintArray() {71 invocation = new InvocationBuilder().method("oneArray").args(new int[]{1, 2, 3}).toInvocation();72 assertThat(invocation.toString(), endsWith("oneArray([1, 2, 3]);"));73 }74 @Test75 public void shouldPrintNullIfArrayIsNull() throws Exception {76 Method m = IMethods.class.getMethod("oneArray", Object[].class);77 invocation = new InvocationBuilder().method(m).args((Object) null).toInvocation();78 assertThat(invocation.toString(), endsWith("oneArray(null);"));79 }80 @Test81 public void shouldPrintArgumentsInMultilinesWhenGetsTooBig() {82 invocation = new InvocationBuilder().args("veeeeery long string that makes it ugly in one line", 1).toInvocation();83 assertThat(invocation.toString(), endsWith(84 "simpleMethod(" +85 "\n" +86 " \"veeeeery long string that makes it ugly in one line\"," +87 "\n" +88 " 1" +89 "\n" +90 ");"));91 }92 @Test93 public void shouldTransformArgumentsToMatchers() throws Exception {94 Invocation i = new InvocationBuilder().args("foo", new String[]{"bar"}).toInvocation();95 List matchers = i.argumentsToMatchers();96 assertEquals(2, matchers.size());97 assertEquals(Equals.class, matchers.get(0).getClass());98 assertEquals(ArrayEquals.class, matchers.get(1).getClass());99 }100 @Test101 public void shouldKnowIfIsToString() throws Exception {102 Invocation toString = new InvocationBuilder().method("toString").toInvocation();103 assertTrue(Invocation.isToString(toString));104 Invocation notToString = new InvocationBuilder().method("toString").arg("foo").toInvocation();105 assertFalse(Invocation.isToString(notToString));106 }107 @Test108 public void shouldKnowValidThrowables() throws Exception {109 Invocation invocation = new InvocationBuilder().method("canThrowException").toInvocation();110 assertFalse(invocation.isValidException(new Exception()));111 assertTrue(invocation.isValidException(new CharacterCodingException()));112 }113 class Foo {114 public String bark() {115 return "woof";116 }117 }118 @Test119 public void shouldBeAbleToCallRealMethod() throws Throwable {120 //when121 Invocation invocation = invocationOf(Foo.class, "bark", new RealMethod() {122 public Object invoke(Object target, Object[] arguments) throws Throwable {123 return new Foo().bark();124 }125 });126 //then127 assertEquals("woof", invocation.callRealMethod());128 }129 @Test130 public void shouldScreamWhenCallingRealMethodOnInterface() throws Throwable {131 //given132 Invocation invocationOnInterface = new InvocationBuilder().toInvocation();133 try {134 //when135 invocationOnInterface.callRealMethod();136 //then137 fail();138 } catch (MockitoException e) {139 }140 }141}...

Full Screen

Full Screen

Source:InvocationImplTest.java Github

copy

Full Screen

...20public class InvocationImplTest extends TestBase {21 private Invocation invocation;22 @Before23 public void setup() throws Exception {24 invocation = new InvocationBuilder().args(" ").mock("mock").toInvocation();25 }26 @Test27 public void shouldKnowIfIsEqualTo() {28 Invocation equal = new InvocationBuilder().args(" ").mock("mock").toInvocation();29 Invocation nonEqual = new InvocationBuilder().args("X").mock("mock").toInvocation();30 Invocation withNewStringInstance = new InvocationBuilder().args(new String(" ")).mock("mock").toInvocation();31 assertFalse(invocation.equals(null));32 assertFalse(invocation.equals(""));33 assertTrue(invocation.equals(equal));34 assertFalse(invocation.equals(nonEqual));35 assertTrue(invocation.equals(withNewStringInstance));36 }37 38 @Test39 public void shouldEqualToNotConsiderSequenceNumber() {40 Invocation equal = new InvocationBuilder().args(" ").mock("mock").seq(2).toInvocation();41 42 assertTrue(invocation.equals(equal));43 assertTrue(invocation.getSequenceNumber() != equal.getSequenceNumber());44 }45 46 @Test47 public void shouldBeACitizenOfHashes() {48 Map map = new HashMap();49 map.put(invocation, "one");50 assertEquals("one", map.get(invocation));51 }52 53 @Test54 public void shouldPrintMethodName() {55 invocation = new InvocationBuilder().toInvocation();56 assertEquals("iMethods.simpleMethod();", invocation.toString());57 }58 59 @Test60 public void shouldPrintMethodArgs() {61 invocation = new InvocationBuilder().args("foo").toInvocation();62 assertThat(invocation.toString(), endsWith("simpleMethod(\"foo\");"));63 }64 65 @Test66 public void shouldPrintMethodIntegerArgAndString() {67 invocation = new InvocationBuilder().args("foo", 1).toInvocation();68 assertThat(invocation.toString(), endsWith("simpleMethod(\"foo\", 1);"));69 }70 71 @Test72 public void shouldPrintNull() {73 invocation = new InvocationBuilder().args((String) null).toInvocation();74 assertThat(invocation.toString(), endsWith("simpleMethod(null);"));75 }76 77 @Test78 public void shouldPrintArray() {79 invocation = new InvocationBuilder().method("oneArray").args(new int[] { 1, 2, 3 }).toInvocation();80 assertThat(invocation.toString(), endsWith("oneArray([1, 2, 3]);"));81 }82 83 @Test84 public void shouldPrintNullIfArrayIsNull() throws Exception {85 Method m = IMethods.class.getMethod("oneArray", Object[].class);86 invocation = new InvocationBuilder().method(m).args((Object) null).toInvocation();87 assertThat(invocation.toString(), endsWith("oneArray(null);"));88 }89 90 @Test91 public void shouldPrintArgumentsInMultilinesWhenGetsTooBig() {92 invocation = new InvocationBuilder().args("veeeeery long string that makes it ugly in one line", 1).toInvocation();93 assertThat(invocation.toString(), endsWith(94 "simpleMethod(" +95 "\n" +96 " \"veeeeery long string that makes it ugly in one line\"," +97 "\n" +98 " 1" +99 "\n" +100 ");"));101 }102 103 @Test104 public void shouldTransformArgumentsToMatchers() throws Exception {105 Invocation i = new InvocationBuilder().args("foo", new String[]{"bar"}).toInvocation();106 List matchers = ArgumentsProcessor.argumentsToMatchers(i.getArguments());107 assertEquals(2, matchers.size());108 assertEquals(Equals.class, matchers.get(0).getClass());109 assertEquals(ArrayEquals.class, matchers.get(1).getClass());110 }111 112 class Foo {113 public String bark() {114 return "woof";115 }116 }117 118 @Test119 public void shouldBeAbleToCallRealMethod() throws Throwable {120 //when121 Invocation invocation = invocationOf(Foo.class, "bark", new RealMethod() {122 public Object invoke(Object target, Object[] arguments) throws Throwable {123 return new Foo().bark();124 }});125 //then126 assertEquals("woof", invocation.callRealMethod());127 }128 129 @Test130 public void shouldScreamWhenCallingRealMethodOnInterface() throws Throwable {131 //given132 Invocation invocationOnInterface = new InvocationBuilder().toInvocation();133 try {134 //when135 invocationOnInterface.callRealMethod();136 //then137 fail();138 } catch(MockitoException e) {}139 }140 141 @Test142 public void shouldReturnCastedArgumentAt(){143 //given144 int argument = 42;145 Invocation invocationOnInterface = new InvocationBuilder().method("twoArgumentMethod").146 argTypes(int.class, int.class).args(1, argument).toInvocation();147 //when148 int secondArgument = invocationOnInterface.getArgumentAt(1, int.class);149 //then150 assertTrue(secondArgument == argument);151 }152}...

Full Screen

Full Screen

Source:NumberOfInvocationsCheckerTest.java Github

copy

Full Screen

...25 public void setup() {26 reporterStub = new ReporterStub();27 finderStub = new InvocationsFinderStub();28 checker = new NumberOfInvocationsChecker(reporterStub, finderStub);29 wanted = new InvocationBuilder().toInvocationMatcher();30 invocations = new LinkedList<Invocation>(asList(new InvocationBuilder().toInvocation()));31 }32 @Test33 public void shouldReportTooLittleActual() throws Exception {34 finderStub.actualToReturn.add(new InvocationBuilder().toInvocation());35 checker.check(invocations, wanted, 100);36 assertEquals(1, reporterStub.actualCount);37 assertEquals(100, reporterStub.wantedCount);38 assertEquals(wanted, reporterStub.wanted);39 }40 @Test41 public void shouldReportWithLastInvocationStackTrace() throws Exception {42 Invocation first = new InvocationBuilder().toInvocation();43 Invocation second = new InvocationBuilder().toInvocation();44 finderStub.actualToReturn.addAll(asList(first, second));45 checker.check(invocations, wanted, 100);46 assertSame(second.getLocation(), reporterStub.location);47 }48 @Test49 public void shouldNotReportWithLastInvocationStackTraceIfNoInvocationsFound() throws Exception {50 assertTrue(finderStub.actualToReturn.isEmpty());51 checker.check(invocations, wanted, 100);52 assertNull(reporterStub.location);53 }54 @Test55 public void shouldReportWithFirstUndesiredInvocationStackTrace() throws Exception {56 Invocation first = new InvocationBuilder().toInvocation();57 Invocation second = new InvocationBuilder().toInvocation();58 Invocation third = new InvocationBuilder().toInvocation();59 finderStub.actualToReturn.addAll(asList(first, second, third));60 checker.check(invocations, wanted, 2);61 assertSame(third.getLocation(), reporterStub.location);62 }63 @Test64 public void shouldReportTooManyActual() throws Exception {65 finderStub.actualToReturn.add(new InvocationBuilder().toInvocation());66 finderStub.actualToReturn.add(new InvocationBuilder().toInvocation());67 checker.check(invocations, wanted, 1);68 assertEquals(2, reporterStub.actualCount);69 assertEquals(1, reporterStub.wantedCount);70 assertEquals(wanted, reporterStub.wanted);71 }72 @Test73 public void shouldReportNeverWantedButInvoked() throws Exception {74 Invocation invocation = new InvocationBuilder().toInvocation();75 finderStub.actualToReturn.add(invocation);76 checker.check(invocations, wanted, 0);77 assertEquals(wanted, reporterStub.wanted);78 assertEquals(invocation.getLocation(), reporterStub.location);79 }80 @Test81 public void shouldMarkInvocationsAsVerified() throws Exception {82 Invocation invocation = new InvocationBuilder().toInvocation();83 finderStub.actualToReturn.add(invocation);84 assertFalse(invocation.isVerified());85 checker.check(invocations, wanted, 1);86 assertTrue(invocation.isVerified());87 }88 class ReporterStub extends Reporter {89 private int wantedCount;90 private int actualCount;91 private PrintableInvocation wanted;92 private Location location;93 @Override94 public void tooLittleActualInvocations(Discrepancy discrepancy, PrintableInvocation wanted, Location lastActualLocation) {95 this.wantedCount = discrepancy.getWantedCount();96 this.actualCount = discrepancy.getActualCount();...

Full Screen

Full Screen

Source:AnswersValidatorTest.java Github

copy

Full Screen

...14import static org.mockito.Mockito.*;15@SuppressWarnings("unchecked")16public class AnswersValidatorTest extends TestBase {17 private AnswersValidator validator = new AnswersValidator();18 private Invocation invocation = new InvocationBuilder().method("canThrowException").toInvocation();19 @Test20 public void shouldValidateNullThrowable() throws Throwable {21 try {22 validator.validate(new ThrowsException(null), null);23 fail();24 } catch (MockitoException e) {25 }26 }27 @Test28 public void shouldPassProperCheckedException() throws Throwable {29 validator.validate(new ThrowsException(new CharacterCodingException()), invocation);30 }31 @Test(expected = MockitoException.class)32 public void shouldFailInvalidCheckedException() throws Throwable {33 validator.validate(new ThrowsException(new IOException()), invocation);34 }35 @Test36 public void shouldPassRuntimeExceptions() throws Throwable {37 validator.validate(new ThrowsException(new Error()), invocation);38 validator.validate(new ThrowsException(new RuntimeException()), invocation);39 }40 @Test(expected = MockitoException.class)41 public void shouldFailWhenReturnValueIsSetForVoidMethod() throws Throwable {42 validator.validate(new Returns("one"), new InvocationBuilder().method("voidMethod").toInvocation());43 }44 @Test(expected = MockitoException.class)45 public void shouldFailWhenNonVoidMethodDoesNothing() throws Throwable {46 validator.validate(new DoesNothing(), new InvocationBuilder().simpleMethod().toInvocation());47 }48 @Test49 public void shouldAllowVoidReturnForVoidMethod() throws Throwable {50 validator.validate(new DoesNothing(), new InvocationBuilder().method("voidMethod").toInvocation());51 }52 @Test53 public void shouldAllowCorrectTypeOfReturnValue() throws Throwable {54 validator.validate(new Returns("one"), new InvocationBuilder().simpleMethod().toInvocation());55 validator.validate(new Returns(false), new InvocationBuilder().method("booleanReturningMethod").toInvocation());56 validator.validate(new Returns(new Boolean(true)), new InvocationBuilder().method("booleanObjectReturningMethod").toInvocation());57 validator.validate(new Returns(1), new InvocationBuilder().method("integerReturningMethod").toInvocation());58 validator.validate(new Returns(1L), new InvocationBuilder().method("longReturningMethod").toInvocation());59 validator.validate(new Returns(1L), new InvocationBuilder().method("longObjectReturningMethod").toInvocation());60 validator.validate(new Returns(null), new InvocationBuilder().method("objectReturningMethodNoArgs").toInvocation());61 validator.validate(new Returns(1), new InvocationBuilder().method("objectReturningMethodNoArgs").toInvocation());62 }63 @Test(expected = MockitoException.class)64 public void shouldFailOnReturnTypeMismatch() throws Throwable {65 validator.validate(new Returns("String"), new InvocationBuilder().method("booleanReturningMethod").toInvocation());66 }67 @Test(expected = MockitoException.class)68 public void shouldFailOnWrongPrimitive() throws Throwable {69 validator.validate(new Returns(1), new InvocationBuilder().method("doubleReturningMethod").toInvocation());70 }71 @Test(expected = MockitoException.class)72 public void shouldFailOnNullWithPrimitive() throws Throwable {73 validator.validate(new Returns(null), new InvocationBuilder().method("booleanReturningMethod").toInvocation());74 }75 @Test76 public void shouldFailWhenCallingRealMethodOnIterface() throws Throwable {77 //given78 Invocation inovcationOnIterface = new InvocationBuilder().method("simpleMethod").toInvocation();79 try {80 //when81 validator.validate(new CallsRealMethods(), inovcationOnIterface);82 //then83 fail();84 } catch (MockitoException e) {85 }86 }87 @Test88 public void shouldBeOKWhenCallingRealMethodOnConcreteClass() throws Throwable {89 //given90 ArrayList mock = mock(ArrayList.class);91 mock.clear();92 Invocation invocationOnClass = getLastInvocation();...

Full Screen

Full Screen

Source:ReturnsTest.java Github

copy

Full Screen

...16 .answer(17 new InvocationBuilder()18 .method("oneArg")19 .arg("A")20 .toInvocation()))21 .isEqualTo("value");22 }23 @Test(expected = MockitoException.class)24 public void should_fail_when_return_Value_is_set_for_void_method() throws Throwable {25 new Returns("one").validateFor(new InvocationBuilder().method("voidMethod").toInvocation());26 }27 @Test28 public void should_allow_correct_type_of_return_value() throws Throwable {29 new Returns("one").validateFor(new InvocationBuilder().simpleMethod().toInvocation());30 new Returns(false)31 .validateFor(32 new InvocationBuilder().method("booleanReturningMethod").toInvocation());33 new Returns(TRUE)34 .validateFor(35 new InvocationBuilder()36 .method("booleanObjectReturningMethod")37 .toInvocation());38 new Returns(1)39 .validateFor(40 new InvocationBuilder().method("integerReturningMethod").toInvocation());41 new Returns(1L)42 .validateFor(new InvocationBuilder().method("longReturningMethod").toInvocation());43 new Returns(1L)44 .validateFor(45 new InvocationBuilder().method("longObjectReturningMethod").toInvocation());46 new Returns(null)47 .validateFor(48 new InvocationBuilder()49 .method("objectReturningMethodNoArgs")50 .toInvocation());51 new Returns(1)52 .validateFor(53 new InvocationBuilder()54 .method("objectReturningMethodNoArgs")55 .toInvocation());56 }57 @Test(expected = MockitoException.class)58 public void should_fail_on_return_type_mismatch() throws Throwable {59 new Returns("String")60 .validateFor(61 new InvocationBuilder().method("booleanReturningMethod").toInvocation());62 }63 @Test(expected = MockitoException.class)64 public void should_fail_on_wrong_primitive() throws Throwable {65 new Returns(1)66 .validateFor(67 new InvocationBuilder().method("doubleReturningMethod").toInvocation());68 }69 @Test(expected = MockitoException.class)70 public void should_fail_on_null_with_primitive() throws Throwable {71 new Returns(null)72 .validateFor(73 new InvocationBuilder().method("booleanReturningMethod").toInvocation());74 }75}...

Full Screen

Full Screen

Source:AtLeastXNumberOfInvocationsCheckerTest.java Github

copy

Full Screen

...22 @Test23 public void shouldMarkActualInvocationsAsVerifiedInOrder() {24 InOrderContext context = new InOrderContextImpl();25 //given26 Invocation invocation = new InvocationBuilder().simpleMethod().toInvocation();27 Invocation invocationTwo = new InvocationBuilder().differentMethod().toInvocation();28 //when29 checkAtLeastNumberOfInvocations(asList(invocation, invocationTwo), new InvocationMatcher(invocation), 1, context);30 //then31 assertThat(invocation.isVerified()).isTrue();32 }33 @Test34 public void shouldReportTooFewInvocationsInOrder() {35 InOrderContext context = new InOrderContextImpl();36 //given37 Invocation invocation = new InvocationBuilder().simpleMethod().toInvocation();38 Invocation invocationTwo = new InvocationBuilder().differentMethod().toInvocation();39 exception.expect(VerificationInOrderFailure.class);40 exception.expectMessage("iMethods.simpleMethod()");41 exception.expectMessage("Wanted *at least* 2 times");42 exception.expectMessage("But was 1 time");43 //when44 checkAtLeastNumberOfInvocations(asList(invocation, invocationTwo), new InvocationMatcher(invocation), 2, context);45 }46 @Test47 public void shouldMarkActualInvocationsAsVerified() {48 //given49 Invocation invocation = new InvocationBuilder().simpleMethod().toInvocation();50 Invocation invocationTwo = new InvocationBuilder().differentMethod().toInvocation();51 //when52 checkAtLeastNumberOfInvocations(asList(invocation, invocationTwo), new InvocationMatcher(invocation), 1);53 //then54 assertThat(invocation.isVerified()).isTrue();55 }56 @Test57 public void shouldReportTooFewInvocations() {58 //given59 Invocation invocation = new InvocationBuilder().simpleMethod().toInvocation();60 Invocation invocationTwo = new InvocationBuilder().differentMethod().toInvocation();61 exception.expect(TooFewActualInvocations.class);62 exception.expectMessage("iMethods.simpleMethod()");63 exception.expectMessage("Wanted *at least* 2 times");64 exception.expectMessage("But was 1 time");65 //when66 checkAtLeastNumberOfInvocations(asList(invocation, invocationTwo), new InvocationMatcher(invocation), 2);67 }68}...

Full Screen

Full Screen

Source:AnswersWithDelayTest.java Github

copy

Full Screen

...11import org.mockito.internal.invocation.InvocationBuilder;12public class AnswersWithDelayTest {13 @Test14 public void should_return_value() throws Throwable {15 assertThat(new AnswersWithDelay(1, new Returns("value")).answer(new InvocationBuilder().method("oneArg").arg("A").toInvocation())).isEqualTo("value");16 }17 @Test(expected = MockitoException.class)18 public void should_fail_when_contained_answer_should_fail() throws Throwable {19 new AnswersWithDelay(1, new Returns("one")).validateFor(new InvocationBuilder().method("voidMethod").toInvocation());20 }21 @Test22 public void should_succeed_when_contained_answer_should_succeed() throws Throwable {23 new AnswersWithDelay(1, new Returns("one")).validateFor(new InvocationBuilder().simpleMethod().toInvocation());24 }25 @Test26 public void should_delay() throws Throwable {27 final long sleepyTime = 500L;28 final AnswersWithDelay testSubject = new AnswersWithDelay(sleepyTime, new Returns("value"));29 final Date before = new Date();30 testSubject.answer(new InvocationBuilder().method("oneArg").arg("A").toInvocation());31 final Date after = new Date();32 final long timePassed = after.getTime() - before.getTime();33 assertThat(timePassed).isCloseTo(sleepyTime, within(15L));34 }35}...

Full Screen

Full Screen

toInvocation

Using AI Code Generation

copy

Full Screen

1package org.mockito.internal.invocation;2import org.mockito.invocation.Invocation;3import org.mockito.invocation.Location;4import org.mockito.invocation.MatchableInvocation;5import org.mockito.invocation.MockHandler;6import org.mockito.invocation.StubInfo;7import org.mockito.invocation.Stubbing;8import org.mockito.mock.MockCreationSettings;9import org.mockito.mock.MockName;10import org.mockito.mock.SerializableMode;11import org.mockito.plugins.MockMaker;12import org.mockito.stubbing.Answer;13import org.mockito.stubbing.OngoingStubbing;14import org.mockito.stubbing.StubbedInvocationMatcher;15import org.mockito.verification.VerificationMode;16import org.mockito.verification.VerificationStrategy;17import org.mockito.verification.VerificationWithTimeout;18import org.mockito.internal.util.MockUtil;19import org.mockito.internal.util.reflection.LenientCopyTool;20import java.io.Serializable;21import java.lang.reflect.Method;22import java.util.List;23import java.util.Map;24import java.util.Set;25import static org.mockito.internal.invocation.InvocationBuilder.toInvocation;26import static org.mockito.internal.util.StringJoiner.join;27public class InvocationBuilder {28 private final MockMaker mockMaker;29 private final MockCreationSettings settings;30 private final MockHandler handler;31 private final MockName mockName;32 private final SerializableMode serializableMode;33 private final MockUtil mockUtil;34 private final LenientCopyTool lenientCopyTool;35 private final Set<MockName> spies;36 private final Map<MockName, MockHandler> handlers;37 private final Map<MockName, MockCreationSettings> mockSettings;38 private final List<StubbedInvocationMatcher> stubbed;39 private final VerificationStrategy verificationStrategy;40 private final VerificationMode defaultVerification;41 public InvocationBuilder() {42 mockMaker = null;43 settings = null;44 handler = null;45 mockName = null;46 serializableMode = null;47 mockUtil = null;48 lenientCopyTool = null;49 spies = null;50 handlers = null;51 mockSettings = null;52 stubbed = null;53 verificationStrategy = null;54 defaultVerification = null;55 }56 public InvocationBuilder(MockMaker mockMaker, MockCreationSettings settings, MockHandler handler, MockName mockName, SerializableMode serializableMode, MockUtil mockUtil, LenientCopyTool lenientCopyTool, Set<MockName> spies, Map<MockName, MockHandler> handlers, Map<MockName, MockCreationSettings

Full Screen

Full Screen

toInvocation

Using AI Code Generation

copy

Full Screen

1import org.mockito.internal.invocation.InvocationBuilder;2import org.mockito.invocation.Invocation;3import org.mockito.invocation.Location;4import org.mockito.invocation.MatchableInvocation;5import org.mockito.invocation.MockitoInvocationHandler;6import org.mockito.internal.invocation.MockitoMethod;7import org.mockito.internal.invocation.MockitoInvocationHandler;8import org.mockito.internal.invocation.MockitoMethod;9import org.mockito.inter

Full Screen

Full Screen

toInvocation

Using AI Code Generation

copy

Full Screen

1package org.mockito.internal.invocation;2import org.mockito.invocation.Invocation;3import org.mockito.invocation.Location;4import org.mockito.stubbing.Answer;5import java.lang.reflect.Method;6public class InvocationBuilder {7 public static Invocation toInvocation(Object mock, Method method, Object[] args, Answer answer) {8 return new InvocationBuilder().mock(mock).method(method).args(args).answer(answer).toInvocation();9 }10 public InvocationBuilder mock(Object mock) {11 this.mock = mock;12 return this;13 }14 public InvocationBuilder method(Method method) {15 this.method = method;16 return this;17 }18 public InvocationBuilder args(Object[] args) {19 this.args = args;20 return this;21 }22 public InvocationBuilder answer(Answer answer) {23 this.answer = answer;24 return this;25 }26 public Invocation toInvocation() {27 return new Invocation() {28 public Object getMock() {29 return mock;30 }31 public Method getMethod() {32 return method;33 }34 public Object[] getArguments() {35 return args;36 }37 public Location getLocation() {38 return null;39 }40 public Answer getAnswer() {41 return answer;42 }43 public Object callRealMethod() {44 return null;45 }46 public void validateFor(Object mock) {47 }48 public Invocation copy() {49 return null;50 }51 };52 }53 private Object mock;54 private Method method;55 private Object[] args;56 private Answer answer;57}58package org.mockito.internal.invocation;59import org.mockito.invocation.Invocation;60import org.mockito.invocation.Location;61import org.mockito.stubbing.Answer;62import java.lang.reflect.Method;63public class InvocationBuilder {64 public static Invocation toInvocation(Object mock, Method method, Object[] args, Answer answer) {65 return new InvocationBuilder().mock(mock).method(method).args(args).answer(answer).toInvocation();66 }67 public InvocationBuilder mock(Object mock) {68 this.mock = mock;69 return this;70 }71 public InvocationBuilder method(Method method) {72 this.method = method;73 return this;74 }75 public InvocationBuilder args(Object[] args) {76 this.args = args;77 return this;78 }

Full Screen

Full Screen

toInvocation

Using AI Code Generation

copy

Full Screen

1package com.example;2import java.lang.reflect.Method;3import org.mockito.internal.invocation.InvocationBuilder;4import org.mockito.invocation.Invocation;5public class Example {6 public static void main(String[] args) throws Exception {7 Method method = Object.class.getDeclaredMethod("toString");8 Invocation invocation = new InvocationBuilder().toInvocation(method);9 System.out.println(invocation);10 }11}12package com.example;13import java.lang.reflect.Method;14import org.mockito.internal.invocation.InvocationBuilder;15import org.mockito.invocation.Invocation;16public class Example {17 public static void main(String[] args) throws Exception {18 Method method = Object.class.getDeclaredMethod("toString");19 Invocation invocation = new InvocationBuilder().toInvocation(method);20 System.out.println(invocation);21 }22}23package com.example;24import java.lang.reflect.Method;25import org.mockito.internal.invocation.InvocationBuilder;26import org.mockito.invocation.Invocation;27public class Example {28 public static void main(String[] args) throws Exception {29 Method method = Object.class.getDeclaredMethod("toString");30 Invocation invocation = new InvocationBuilder().toInvocation(method);31 System.out.println(invocation);32 }33}34package com.example;35import java.lang.reflect.Method;36import org.mockito.internal.invocation.InvocationBuilder;37import org.mockito.invocation.Invocation;38public class Example {39 public static void main(String[] args) throws Exception {40 Method method = Object.class.getDeclaredMethod("toString");41 Invocation invocation = new InvocationBuilder().toInvocation(method);42 System.out.println(invocation);43 }44}45package com.example;46import java.lang.reflect.Method;47import org.mockito.internal.invocation.InvocationBuilder;48import org.mockito.invocation.Invocation;49public class Example {50 public static void main(String[] args) throws Exception {51 Method method = Object.class.getDeclaredMethod("toString");

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