How to use method of org.easymock.tests2.ReflectionUtilsTest class

Best Easymock code snippet using org.easymock.tests2.ReflectionUtilsTest.

Run Easymock automation tests on LambdaTest cloud grid

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

copy
1/*
2 * Copyright 2001-2021 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.easymock.tests2;
17
18import org.easymock.internal.ReflectionUtils;
19import org.junit.Test;
20
21import java.lang.reflect.Constructor;
22import java.lang.reflect.Method;
23import java.util.Collection;
24import java.util.List;
25import java.util.function.Function;
26
27import static org.easymock.internal.ReflectionUtils.*;
28import static org.junit.Assert.*;
29
30/**
31 * @author Henri Tremblay
32 */
33public class ReflectionUtilsTest {
34
35    private static final Class<?>[] NO_PARAMS = new Class[0];
36
37    public static class B {
38        protected void foo(long l) { }
39
40        public void parentMethod() {}
41    }
42
43    public static class A extends B {
44
45        public A(boolean bool, byte b, int i, short s, char c, long l, float f, double d) { }
46
47        public A(int i) { }
48
49        protected A(long l) { }
50
51        private A(byte b) { }
52
53        A(char c) { }
54
55        public A(CharSequence c) { }
56
57        public A(StringBuilder s) { }
58
59        public void foo(int i) {}
60
61        public static void staticMethod() {}
62
63        private void privateMethod() {}
64
65        protected void protectedMethod() {}
66
67        void packageMethod() {}
68    }
69
70    @Test
71    public void testGetConstructor_public() throws NoSuchMethodException {
72        Constructor<A> c = ReflectionUtils.getConstructor(A.class, 5);
73        assertArrayEquals(new Class[] { int.class }, c.getParameterTypes());
74    }
75
76    @Test
77    public void testGetConstructor_protected() throws NoSuchMethodException {
78        Constructor<A> c = ReflectionUtils.getConstructor(A.class, 5L);
79        assertArrayEquals(new Class[] { long.class }, c.getParameterTypes());
80    }
81
82    @Test
83    public void testGetConstructor_default() throws NoSuchMethodException {
84        Constructor<A> c = ReflectionUtils.getConstructor(A.class, 'c');
85        assertArrayEquals(new Class[] { char.class }, c.getParameterTypes());
86    }
87
88    @Test
89    public void testGetConstructor_private() {
90        assertThrows(NoSuchMethodException.class, () -> ReflectionUtils.getConstructor(A.class, (byte) 5));
91    }
92
93    @Test
94    public void testGetConstructor_twoMatching() {
95        assertThrows(IllegalArgumentException.class, () -> ReflectionUtils.getConstructor(A.class, new StringBuilder(0)));
96    }
97
98    @Test
99    public void testGetConstructor_notFound() {
100        assertThrows(NoSuchMethodException.class, () -> ReflectionUtils.getConstructor(A.class, true));
101    }
102
103    @Test
104    public void testGetConstructor_WrongParams() {
105        assertThrows(NoSuchMethodException.class, () -> ReflectionUtils.getConstructor(A.class, "", ""));
106    }
107
108    @Test
109    public void testGetConstructor_AllPrimitives() throws NoSuchMethodException {
110        Constructor<A> c = ReflectionUtils.getConstructor(A.class, true, (byte) 1, 2, (short) 3, 'g',
111            5L, 4.0f, 8.0);
112        assertNotNull(c);
113    }
114
115    @Test
116    public void testGetDeclareMethod_Found() throws Exception {
117        Method expected = A.class.getDeclaredMethod("foo", int.class);
118        Method actual = ReflectionUtils.getDeclaredMethod(A.class, "foo", Integer.TYPE);
119        assertEquals(expected, actual);
120    }
121
122    @Test
123    public void testGetDeclareMethod_NotFound() {
124        RuntimeException t = assertThrows(RuntimeException.class, () -> ReflectionUtils.getDeclaredMethod(A.class, "foo"));
125        assertEquals(NoSuchMethodException.class, t.getCause().getClass());
126    }
127
128    @Test
129    public void testIsClassMockingPossible() {
130        assertTrue(ReflectionUtils.isClassAvailable("org.easymock.EasyMock"));
131        assertFalse(ReflectionUtils.isClassAvailable("org.easymock.NotThere"));
132    }
133
134    @Test
135    public void testFindMethodWithParam_notFound() {
136        assertNull(ReflectionUtils.findMethod(getClass(), "xxx", NOT_PRIVATE, int.class));
137    }
138
139    @Test
140    public void testFindMethodWithParam_foundDirectlyOnClass() {
141        Method method = ReflectionUtils.findMethod(A.class, "foo", NOT_PRIVATE, int.class);
142        assertEquals("foo", method.getName());
143        assertEquals(A.class, method.getDeclaringClass());
144    }
145
146    @Test
147    public void testFindMethodWithParam_foundDirectlyOnClassButWithDifferentParams() {
148        assertNull(ReflectionUtils.findMethod(getClass(), "foo", NOT_PRIVATE, double.class));
149        assertNull(ReflectionUtils.findMethod(getClass(), "foo", NOT_PRIVATE, int.class, int.class));
150    }
151
152    @Test
153    public void testFindMethodWithParam_privateMethodsIgnored() {
154        assertNull(ReflectionUtils.findMethod(A.class, "privateMethod", NOT_PRIVATE, NO_PARAMS));
155    }
156
157    @Test
158    public void testFindMethodWithParam_protectedMethodsFound() {
159        Method method = ReflectionUtils.findMethod(A.class, "protectedMethod", NOT_PRIVATE, NO_PARAMS);
160        assertEquals("protectedMethod", method.getName());
161        assertEquals(A.class, method.getDeclaringClass());
162    }
163
164    @Test
165    public void testFindMethodWithParam_packageMethodsFound() {
166        Method method = ReflectionUtils.findMethod(A.class, "packageMethod", NOT_PRIVATE, NO_PARAMS);
167        assertEquals("packageMethod", method.getName());
168        assertEquals(A.class, method.getDeclaringClass());
169    }
170
171    @Test
172    public void testFindMethodWithParam_parentMethodsFound() {
173        Method method = ReflectionUtils.findMethod(A.class, "parentMethod", NOT_PRIVATE, NO_PARAMS);
174        assertEquals("parentMethod", method.getName());
175        assertEquals(B.class, method.getDeclaringClass());
176    }
177
178    @Test
179    public void testGetDefaultMethods_onClass() {
180        IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> ReflectionUtils.getDefaultMethods(getClass()));
181        assertEquals("Only interfaces can have default methods. Not " + getClass(), e.getMessage());
182    }
183
184    @Test
185    public void testGetDefaultMethods_noDefaultMethods() {
186        assertTrue(ReflectionUtils.getDefaultMethods(Runnable.class).isEmpty());
187    }
188
189    @Test
190    public void testGetDefaultMethods_withDefaultMethods() {
191        assertEquals(2, ReflectionUtils.getDefaultMethods(Function.class).size());
192    }
193
194    @Test
195    public void testGetDefaultMethods_withDefaultMethodsBaseClass() {
196        Method stream = findMethod(Collection.class, "stream", m -> true);
197        assertTrue(ReflectionUtils.getDefaultMethods(List.class).contains(stream));
198    }
199}
200
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
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)