How to use InvocationInfoTest class of org.mockito.internal.stubbing.answers package

Best Mockito code snippet using org.mockito.internal.stubbing.answers.InvocationInfoTest

Run Mockito automation tests on LambdaTest cloud grid

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

copy
1/*
2 * Copyright (c) 2007 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5package org.mockito.test.stubbing.answers;
6
7import java.lang.reflect.Method;
8import java.nio.charset.CharacterCodingException;
9import org.junit.Test;
10import org.mockito.internal.stubbing.answers.InvocationInfo;
11import org.mockito.test.invocation.InvocationBuilder;
12import org.mockito.invocation.Invocation;
13import org.mockito.test.mockitousage.IMethods;
14
15import static org.assertj.core.api.Assertions.assertThat;
16import static org.mockito.Mockito.mock;
17import static org.mockito.test.mockitoutil.TestBase.getLastInvocation;
18
19public class InvocationInfoTest {
20
21    @Test
22    public void should_know_valid_throwables() throws Exception {
23        //when
24        Invocation invocation = new InvocationBuilder().method("canThrowException").toInvocation();
25        InvocationInfo info = new InvocationInfo(invocation);
26
27        //then
28        assertThat(info.isValidException(new Exception())).isFalse();
29        assertThat(info.isValidException(new CharacterCodingException())).isTrue();
30    }
31
32    @Test
33    public void should_know_valid_return_types() throws Exception {
34        assertThat(new InvocationInfo(new InvocationBuilder().method("integerReturningMethod").toInvocation()).isValidReturnType(Integer.class)).isTrue();
35        assertThat(new InvocationInfo(new InvocationBuilder().method("integerReturningMethod").toInvocation()).isValidReturnType(int.class)).isTrue();
36        assertThat(new InvocationInfo(new InvocationBuilder().method("intReturningMethod").toInvocation()).isValidReturnType(Integer.class)).isTrue();
37        assertThat(new InvocationInfo(new InvocationBuilder().method("intReturningMethod").toInvocation()).isValidReturnType(int.class)).isTrue();
38        assertThat(new InvocationInfo(new InvocationBuilder().method("integerReturningMethod").toInvocation()).isValidReturnType(String.class)).isFalse();
39    }
40
41    @Test
42    public void should_know_when_invocation_returns_primitive() {
43        assertThat(new InvocationInfo(new InvocationBuilder().method("intReturningMethod").toInvocation()).returnsPrimitive()).isTrue();
44        assertThat(new InvocationInfo(new InvocationBuilder().method("integerReturningMethod").toInvocation()).returnsPrimitive()).isFalse();
45    }
46
47    @Test
48    public void should_know_when_invocation_returns_void() {
49        assertThat(new InvocationInfo(new InvocationBuilder().method("voidMethod").toInvocation()).isVoid()).isTrue();
50        assertThat(new InvocationInfo(new InvocationBuilder().method("integerReturningMethod").toInvocation()).isVoid()).isFalse();
51    }
52
53    @Test
54    public void should_read_the_method_name() {
55        assertThat(new InvocationInfo(new InvocationBuilder().method("voidMethod").toInvocation()).getMethodName()).isEqualTo("voidMethod");
56    }
57
58    @Test
59    public void should_read_the_method_return_name() {
60        assertThat(new InvocationInfo(new InvocationBuilder().method("voidMethod").toInvocation()).printMethodReturnType()).isEqualTo("void");
61        assertThat(new InvocationInfo(new InvocationBuilder().method("integerReturningMethod").toInvocation()).printMethodReturnType()).isEqualTo("Integer");
62        assertThat(new InvocationInfo(new InvocationBuilder().method("intReturningMethod").toInvocation()).printMethodReturnType()).isEqualTo("int");
63    }
64
65    @Test
66    public void should_know_abstract_method() throws Exception { // To be extended with Java 8
67        assertThat(new InvocationInfo(new InvocationBuilder().method(iAmAbstract()).toInvocation()).isAbstract()).isTrue();
68        assertThat(new InvocationInfo(new InvocationBuilder().method(iAmNotAbstract()).toInvocation()).isAbstract()).isFalse();
69    }
70
71    @Test
72    public void should_know_method_is_declared_on_interface() throws Exception {
73        assertThat(new InvocationInfo(new InvocationBuilder().method(iAmAbstract()).toInvocation()).isDeclaredOnInterface()).isFalse();
74        assertThat(new InvocationInfo(new InvocationBuilder().method("voidMethod").toInvocation()).isDeclaredOnInterface()).isTrue();
75    }
76
77    @Test
78    public void isVoid_invocationOnVoidMethod_returnTrue(){
79        mock(IMethods.class).voidMethod();
80
81        InvocationInfo voidMethod = new InvocationInfo(getLastInvocation());
82
83        assertThat(voidMethod.isVoid()).isTrue();
84    }
85
86    @Test
87    public void isVoid_invocationOnVoidReturningMethod_returnTrue(){
88        mock(IMethods.class).voidReturningMethod();
89
90        InvocationInfo voidRetuningMethod = new InvocationInfo(getLastInvocation());
91
92        assertThat(voidRetuningMethod.isVoid()).isTrue();
93    }
94
95    @Test
96    public void isVoid_invocationNonVoidMethod_returnFalse(){
97        mock(IMethods.class).simpleMethod();
98
99        InvocationInfo stringReturningMethod = new InvocationInfo(getLastInvocation());
100
101        assertThat(stringReturningMethod.isVoid()).isFalse();
102    }
103
104    private Method iAmAbstract() throws NoSuchMethodException {
105        abstract class TheAbstract {
106            abstract void iAmAbstract();
107        }
108        return TheAbstract.class.getDeclaredMethod("iAmAbstract");
109    }
110
111    private Method iAmNotAbstract() throws NoSuchMethodException {
112        abstract class TheNotAbstract {
113            void iAmNotAbstract() {}
114        }
115        return TheNotAbstract.class.getDeclaredMethod("iAmNotAbstract");
116    }
117}
118
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

...Most popular Stackoverflow questions on InvocationInfoTest

    No relevant questions found for this class 😞

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)