public final class TestUtil extends Object
| Modifier and Type | Method and Description |
|---|---|
static StackTraceElement |
getExecutingMethod()
Gets the current executing method name.
|
static <T,E> E |
getPrivateField(String fieldName,
T obj)
Gets a private field using reflection.
|
static <T,E> E |
getPrivateField(String fieldName,
T obj,
Class<? super T> declaredFieldClass)
Gets a private field using reflection.
|
static <T> void |
setPrivateField(String fieldName,
Object fieldValue,
T obj)
Updates the value of a private field using reflection.
|
static <T> void |
setPrivateField(String fieldName,
Object fieldValue,
T obj,
Class<? super T> declaredFieldClass)
Updates the value of a private field using reflection.
|
public static <T> void setPrivateField(String fieldName, Object fieldValue, T obj) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException
The purpose of this method is to set mocks for dependency objects created internally by the class being tested or injected by frameworks like OSGi. Reflection should not be used to update fields which by design are private.
fieldName - field namefieldValue - new field's valueobj - instance to update the field onSecurityException - If a security manager, s, is present and any of the following
conditions is met: - invocation of s.checkMemberAccess(this, Member.DECLARED)
denies access to the declared field - the caller's class loader is not the same
as or an ancestor of the class loader for the current class and invocation of
s.checkPackageAccess() denies access to the package of this class.NoSuchFieldException - if a field with the specified name is not foundIllegalArgumentException - if fieldName is nullIllegalAccessException - if the underlying field is inaccessibleNullPointerException - if either the fieldName or obj is nullpublic static <T> void setPrivateField(String fieldName, Object fieldValue, T obj, Class<? super T> declaredFieldClass) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException
declaredFieldClass class and not in a predecessor or ancestor.
The purpose of this method is to set mocks for dependency objects created internally by the class being tested or injected by frameworks like OSGi. Reflection should not be used to update fields which by design are private.
fieldName - field namefieldValue - new field's valueobj - instance to update the field ondeclaredFieldClass - class where the field is locatedSecurityException - If a security manager, s, is present and any of the following
conditions is met: - invocation of s.checkMemberAccess(this, Member.DECLARED)
denies access to the declared field - the caller's class loader is not the same
as or an ancestor of the class loader for the current class and invocation of
s.checkPackageAccess() denies access to the package of this class.NoSuchFieldException - if a field with the specified name is not foundIllegalArgumentException - if either the fieldName or obj is
null, or if fieldName is emptyIllegalAccessException - if the underlying field is inaccessiblepublic static <T,E> E getPrivateField(String fieldName, T obj) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException
fieldName - field nameobj - instance to get the field fromSecurityException - If a security manager, s, is present and any of the following
conditions is met: - invocation of s.checkMemberAccess(this, Member.DECLARED)
denies access to the declared field - the caller's class loader is not the same
as or an ancestor of the class loader for the current class and invocation of
s.checkPackageAccess() denies access to the package of this class.NoSuchFieldException - if a field with the specified name is not foundIllegalArgumentException - if either the fieldName or obj is
null, or if fieldName is emptyIllegalAccessException - if the underlying field is inaccessiblepublic static <T,E> E getPrivateField(String fieldName, T obj, Class<? super T> declaredFieldClass) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException
declaredFieldClass class and not in a predecessor or ancestor.fieldName - field nameobj - instance to get the field fromdeclaredFieldClass - class where the field is locatedSecurityException - If a security manager, s, is present and any of the following
conditions is met: - invocation of s.checkMemberAccess(this, Member.DECLARED)
denies access to the declared field - the caller's class loader is not the same
as or an ancestor of the class loader for the current class and invocation of
s.checkPackageAccess() denies access to the package of this class.NoSuchFieldException - if a field with the specified name is not foundIllegalArgumentException - if either the fieldName or obj is
null, or if fieldName is nullIllegalAccessException - if the underlying field is inaccessiblepublic static StackTraceElement getExecutingMethod()
public class MyClassTest {
@Test
public void testMyMethod() {
StackTraceElement executingMethod = TestUtil.getExecutingMethod();
System.out.println(executingMethod.toString());
}
}
Copyright © 2016. All rights reserved.