Best Easymock code snippet using org.easymock.internal.ReflectionUtils.ReflectionUtils
Source:ReflectionUtilsTest.java
...13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16package org.easymock.tests2;17import org.easymock.internal.ReflectionUtils;18import org.junit.Test;19import java.lang.reflect.Constructor;20import java.lang.reflect.Method;21import java.util.Collection;22import java.util.List;23import java.util.function.Function;24import static org.easymock.internal.ReflectionUtils.*;25import static org.junit.Assert.*;26/**27 * @author Henri Tremblay28 */29public class ReflectionUtilsTest {30 private static final Class<?>[] NO_PARAMS = new Class[0];31 public static class B {32 protected void foo(long l) { }33 public void parentMethod() {}34 }35 public static class A extends B {36 public A(boolean bool, byte b, int i, short s, char c, long l, float f, double d) { }37 public A(int i) { }38 protected A(long l) { }39 private A(byte b) { }40 A(char c) { }41 public A(CharSequence c) { }42 public A(StringBuilder s) { }43 public void foo(int i) {}44 public static void staticMethod() {}45 private void privateMethod() {}46 protected void protectedMethod() {}47 void packageMethod() {}48 }49 @Test50 public void testGetConstructor_public() throws NoSuchMethodException {51 Constructor<A> c = ReflectionUtils.getConstructor(A.class, 5);52 assertArrayEquals(new Class[] { int.class }, c.getParameterTypes());53 }54 @Test55 public void testGetConstructor_protected() throws NoSuchMethodException {56 Constructor<A> c = ReflectionUtils.getConstructor(A.class, 5L);57 assertArrayEquals(new Class[] { long.class }, c.getParameterTypes());58 }59 @Test60 public void testGetConstructor_default() throws NoSuchMethodException {61 Constructor<A> c = ReflectionUtils.getConstructor(A.class, 'c');62 assertArrayEquals(new Class[] { char.class }, c.getParameterTypes());63 }64 @Test65 public void testGetConstructor_private() {66 assertThrows(NoSuchMethodException.class, () -> ReflectionUtils.getConstructor(A.class, (byte) 5));67 }68 @Test69 public void testGetConstructor_twoMatching() {70 assertThrows(IllegalArgumentException.class, () -> ReflectionUtils.getConstructor(A.class, new StringBuilder(0)));71 }72 @Test73 public void testGetConstructor_notFound() {74 assertThrows(NoSuchMethodException.class, () -> ReflectionUtils.getConstructor(A.class, true));75 }76 @Test77 public void testGetConstructor_WrongParams() {78 assertThrows(NoSuchMethodException.class, () -> ReflectionUtils.getConstructor(A.class, "", ""));79 }80 @Test81 public void testGetConstructor_AllPrimitives() throws NoSuchMethodException {82 Constructor<A> c = ReflectionUtils.getConstructor(A.class, true, (byte) 1, 2, (short) 3, 'g',83 5L, 4.0f, 8.0);84 assertNotNull(c);85 }86 @Test87 public void testGetDeclareMethod_Found() throws Exception {88 Method expected = A.class.getDeclaredMethod("foo", int.class);89 Method actual = ReflectionUtils.getDeclaredMethod(A.class, "foo", Integer.TYPE);90 assertEquals(expected, actual);91 }92 @Test93 public void testGetDeclareMethod_NotFound() {94 RuntimeException t = assertThrows(RuntimeException.class, () -> ReflectionUtils.getDeclaredMethod(A.class, "foo"));95 assertEquals(NoSuchMethodException.class, t.getCause().getClass());96 }97 @Test98 public void testIsClassMockingPossible() {99 assertTrue(ReflectionUtils.isClassAvailable("org.easymock.EasyMock"));100 assertFalse(ReflectionUtils.isClassAvailable("org.easymock.NotThere"));101 }102 @Test103 public void testFindMethodWithParam_notFound() {104 assertNull(ReflectionUtils.findMethod(getClass(), "xxx", NOT_PRIVATE, int.class));105 }106 @Test107 public void testFindMethodWithParam_foundDirectlyOnClass() {108 Method method = ReflectionUtils.findMethod(A.class, "foo", NOT_PRIVATE, int.class);109 assertEquals("foo", method.getName());110 assertEquals(A.class, method.getDeclaringClass());111 }112 @Test113 public void testFindMethodWithParam_foundDirectlyOnClassButWithDifferentParams() {114 assertNull(ReflectionUtils.findMethod(getClass(), "foo", NOT_PRIVATE, double.class));115 assertNull(ReflectionUtils.findMethod(getClass(), "foo", NOT_PRIVATE, int.class, int.class));116 }117 @Test118 public void testFindMethodWithParam_privateMethodsIgnored() {119 assertNull(ReflectionUtils.findMethod(A.class, "privateMethod", NOT_PRIVATE, NO_PARAMS));120 }121 @Test122 public void testFindMethodWithParam_protectedMethodsFound() {123 Method method = ReflectionUtils.findMethod(A.class, "protectedMethod", NOT_PRIVATE, NO_PARAMS);124 assertEquals("protectedMethod", method.getName());125 assertEquals(A.class, method.getDeclaringClass());126 }127 @Test128 public void testFindMethodWithParam_packageMethodsFound() {129 Method method = ReflectionUtils.findMethod(A.class, "packageMethod", NOT_PRIVATE, NO_PARAMS);130 assertEquals("packageMethod", method.getName());131 assertEquals(A.class, method.getDeclaringClass());132 }133 @Test134 public void testFindMethodWithParam_parentMethodsFound() {135 Method method = ReflectionUtils.findMethod(A.class, "parentMethod", NOT_PRIVATE, NO_PARAMS);136 assertEquals("parentMethod", method.getName());137 assertEquals(B.class, method.getDeclaringClass());138 }139 @Test140 public void testGetDefaultMethods_onClass() {141 IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> ReflectionUtils.getDefaultMethods(getClass()));142 assertEquals("Only interfaces can have default methods. Not " + getClass(), e.getMessage());143 }144 @Test145 public void testGetDefaultMethods_noDefaultMethods() {146 assertTrue(ReflectionUtils.getDefaultMethods(Runnable.class).isEmpty());147 }148 @Test149 public void testGetDefaultMethods_withDefaultMethods() {150 assertEquals(2, ReflectionUtils.getDefaultMethods(Function.class).size());151 }152 @Test153 public void testGetDefaultMethods_withDefaultMethodsBaseClass() {154 Method stream = findMethod(Collection.class, "stream", m -> true);155 assertTrue(ReflectionUtils.getDefaultMethods(List.class).contains(stream));156 }157}...
Source:AbstractMenuObjectTest.java
...14import org.eclipse.wb.internal.core.model.menu.AbstractMenuObject;15import org.eclipse.wb.internal.core.model.menu.IMenuObjectInfo;16import org.eclipse.wb.internal.core.model.menu.IMenuObjectListener;17import org.eclipse.wb.internal.core.model.menu.IMenuPolicy;18import org.eclipse.wb.internal.core.utils.reflect.ReflectionUtils;19import org.eclipse.wb.tests.designer.tests.DesignerTestCase;2021import org.eclipse.swt.graphics.Image;2223import org.apache.commons.lang.NotImplementedException;24import org.easymock.EasyMock;25import org.easymock.IMocksControl;2627/**28 * Tests for {@link AbstractMenuObject}.29 * 30 * @author scheglov_ke31 */32public class AbstractMenuObjectTest extends DesignerTestCase {33 private IMocksControl m_mocksControl;34 private AbstractMenuObject m_menuObject;35 private IMenuObjectListener m_listener;3637 ////////////////////////////////////////////////////////////////////////////38 //39 // Life cycle40 //41 ////////////////////////////////////////////////////////////////////////////42 @Override43 protected void setUp() throws Exception {44 super.setUp();45 m_mocksControl = EasyMock.createStrictControl();46 m_menuObject = new AbstractMenuObject(null) {47 public Object getModel() {48 throw new NotImplementedException();49 }5051 public Image getImage() {52 throw new NotImplementedException();53 }5455 public Rectangle getBounds() {56 throw new NotImplementedException();57 }5859 public IMenuPolicy getPolicy() {60 throw new NotImplementedException();61 }62 };63 m_listener = m_mocksControl.createMock(IMenuObjectListener.class);64 }6566 @Override67 protected void tearDown() throws Exception {68 m_mocksControl.verify();69 super.tearDown();70 }7172 ////////////////////////////////////////////////////////////////////////////73 //74 // Tests75 //76 ////////////////////////////////////////////////////////////////////////////77 /**78 * Test that {@link IMenuObjectListener#refresh()} can be send.79 */80 public void test_refreshEvent() throws Exception {81 // prepare expectations82 m_listener.refresh();83 m_mocksControl.replay();84 // perform operations85 m_menuObject.addListener(m_listener);86 ReflectionUtils.invokeMethod2(m_menuObject, "fireRefreshListeners");87 }8889 /**90 * Test that {@link IMenuObjectListener#refresh()} can be send.91 */92 public void test_deleteEvent() throws Exception {93 Object object = new Object();94 // prepare expectations95 m_listener.deleting(object);96 m_mocksControl.replay();97 // perform operations98 m_menuObject.addListener(m_listener);99 ReflectionUtils.invokeMethod2(m_menuObject, "fireDeleteListeners", Object.class, object);100 }101102 /**103 * If {@link IMenuObjectInfo} is not added, it will not receive invocations.104 */105 public void test_noListener_noEvents() throws Exception {106 // prepare expectations107 m_mocksControl.replay();108 // perform operations109 ReflectionUtils.invokeMethod2(m_menuObject, "fireRefreshListeners");110 }111112 /**113 * If {@link IMenuObjectInfo} is not added, it will not receive invocations.114 */115 public void test_removeListener_noEvents() throws Exception {116 // prepare expectations117 m_mocksControl.replay();118 // perform operations119 m_menuObject.addListener(m_listener);120 m_menuObject.removeListener(m_listener);121 ReflectionUtils.invokeMethod2(m_menuObject, "fireRefreshListeners");122 }123}
...
ReflectionUtils
Using AI Code Generation
1import java.lang.reflect.Method;2import java.lang.reflect.InvocationTargetException;3import org.easymock.internal.ReflectionUtils;4public class 1 {5 public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {6 Method method = ReflectionUtils.class.getDeclaredMethod("getDeclaredMethod", Class.class, String.class, Class[].class);7 method.setAccessible(true);8 Method getDeclaredMethod = (Method) method.invoke(null, 1.class, "getDeclaredMethod", new Class[]{Class.class, String.class, Class[].class});9 System.out.println(getDeclaredMethod);10 }11}12public static java.lang.reflect.Method org.easymock.internal.ReflectionUtils.getDeclaredMethod(java.lang.Class,java.lang.String,java.lang.Class[])
ReflectionUtils
Using AI Code Generation
1package com.mkyong.core;2import java.lang.reflect.Method;3import org.easymock.internal.ReflectionUtils;4public class App {5 public static void main(String[] args) {6 Method[] methods = ReflectionUtils.getMethods(App.class);7 for (Method method : methods) {8 System.out.println(method.getName());9 }10 }11}
ReflectionUtils
Using AI Code Generation
1public class TestReflectionUtils {2 public static void main(String[] args) {3 Method[] methods = ReflectionUtils.getDeclaredMethods(TestReflectionUtils.class);4 for (int i = 0; i < methods.length; i++) {5 System.out.println(methods[i]);6 }7 }8}9public static void main(java.l
ReflectionUtils
Using AI Code Generation
1package org.easymock.internal;2import java.lang.reflect.Field;3import java.lang.reflect.Modifier;4import java.util.ArrayList;5import java.util.List;6public class ReflectionUtils {7 private ReflectionUtils() {8 }9 public static List<Field> getAllFields(Class<?> clazz) {10 List<Field> fields = new ArrayList<Field>();11 while (clazz != null) {12 for (Field field : clazz.getDeclaredFields()) {13 if (!Modifier.isStatic(field.getModifiers())) {14 fields.add(field);15 }16 }17 clazz = clazz.getSuperclass();18 }19 return fields;20 }21 public static Object getFieldValue(Object target, String fieldName) {22 if (target == null) {23 throw new IllegalArgumentException("target cannot be null");24 }25 if (fieldName == null) {26 throw new IllegalArgumentException("fieldName cannot be null");27 }28 Class<?> clazz = target.getClass();29 while (clazz != null) {30 try {31 Field field = clazz.getDeclaredField(fieldName);32 field.setAccessible(true);33 return field.get(target);34 } catch (NoSuchFieldException e) {35 clazz = clazz.getSuperclass();36 } catch (IllegalAccessException e) {37 throw new RuntimeException(e);38 }39 }40 throw new IllegalArgumentException("No such field: " + fieldName);41 }42}43package org.easymock.internal;44import java.lang.reflect.Field;45import java.lang.reflect.Modifier;46import java.util.ArrayList;47import java.util.List;48public class ReflectionUtils {49 private ReflectionUtils() {50 }51 public static List<Field> getAllFields(Class<?> clazz) {52 List<Field> fields = new ArrayList<Field>();53 while (clazz != null) {54 for (Field field : clazz.getDeclaredFields()) {55 if (!Modifier.isStatic(field.getModifiers())) {56 fields.add(field);57 }58 }59 clazz = clazz.getSuperclass();60 }61 return fields;62 }63 public static Object getFieldValue(Object target, String fieldName) {64 if (target == null) {65 throw new IllegalArgumentException("target cannot be null");66 }67 if (fieldName == null) {68 throw new IllegalArgumentException("fieldName cannot be null");69 }70 Class<?> clazz = target.getClass();
ReflectionUtils
Using AI Code Generation
1public class 1 {2 public static void main(String[] args) {3 try {4 Class c = Class.forName("com.test.Test");5 Object o = c.newInstance();6 Class[] cl = new Class[1];7 cl[0] = String.class;8 Method m = c.getDeclaredMethod("test", cl);9 Object[] args1 = new Object[1];10 args1[0] = "hello";11 Object result = ReflectionUtils.invoke(m, o, args1);12 System.out.println(result);13 } catch (Exception e) {14 e.printStackTrace();15 }16 }17}18public class Test {19 private String test(String s) {20 return s;21 }22}23Your name to display (optional):
ReflectionUtils
Using AI Code Generation
1import org.easymock.internal.MocksControl;2import org.easymock.internal.ReflectionUtils;3public class 1 {4 public static void main(String[] args) throws Exception {5 MocksControl mocksControl = new MocksControl();6 Object value = ReflectionUtils.getField(mocksControl, "defaultMockControl");7 System.out.println("value of defaultMockControl field of MocksControl class is " + value);8 }9}
ReflectionUtils
Using AI Code Generation
1import java.lang.reflect.Method;2import java.lang.reflect.InvocationTargetException;3import org.easymock.internal.ReflectionUtils;4public class 1 {5 public static void main(String[] args) {6 try {7 Class<?> c = Class.forName("org.easymock.internal.ReflectionUtils");8 Method m = c.getDeclaredMethod("getCallerMethodName");9 m.setAccessible(true);10 String methodName = (String) m.invoke(null);11 System.out.println(methodName);12 } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {13 e.printStackTrace();14 }15 }16}17import java.lang.reflect.Method;18import java.lang.reflect.InvocationTargetException;19import org.easymock.internal.ReflectionUtils;20public class 2 {21 public static void main(String[] args) {22 try {23 Class<?> c = Class.forName("org.easymock.internal.ReflectionUtils");24 Method m = c.getDeclaredMethod("getCallerClass");25 m.setAccessible(true);26 Class<?> callerClass = (Class<?>) m.invoke(null);27 System.out.println(callerClass.getName());28 } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {29 e.printStackTrace();30 }31 }32}33import java.lang.reflect.Method;34import java.lang.reflect.InvocationTargetException;35import org.easymock.internal.ReflectionUtils;36public class 3 {37 public static void main(String[] args) {38 try {39 Class<?> c = Class.forName("org.easymock.internal.ReflectionUtils");40 Method m = c.getDeclaredMethod("getCallerClass", int.class);41 m.setAccessible(true);42 Class<?> callerClass = (Class<?>) m.invoke(null, 2);
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!