Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
upgrade mockito to the latest available version
- Loading branch information
TheSnoozer
committed
Aug 10, 2017
1 parent
02d4905
commit 3c2e142
Showing
2 changed files
with
41 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/test/java/org/mockito/internal/util/reflection/Whitebox.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.mockito.internal.util.reflection; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
@Deprecated | ||
public class Whitebox { | ||
public static void setInternalState(Object target, String field, Object value) { | ||
Class<?> c = target.getClass(); | ||
try { | ||
Field f = getFieldFromHierarchy(c, field); | ||
f.setAccessible(true); | ||
f.set(target, value); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Unable to set internal state on a private field. Please report to mockito mailing list.", e); | ||
} | ||
} | ||
|
||
private static Field getFieldFromHierarchy(Class<?> clazz, String field) { | ||
Field f = getField(clazz, field); | ||
while (f == null && clazz != Object.class) { | ||
clazz = clazz.getSuperclass(); | ||
f = getField(clazz, field); | ||
} | ||
if (f == null) { | ||
throw new RuntimeException( | ||
"You want me to get this field: '" + field + | ||
"' on this class: '" + clazz.getSimpleName() + | ||
"' but this field is not declared withing hierarchy of this class!"); | ||
} | ||
return f; | ||
} | ||
|
||
private static Field getField(Class<?> clazz, String field) { | ||
try { | ||
return clazz.getDeclaredField(field); | ||
} catch (NoSuchFieldException e) { | ||
return null; | ||
} | ||
} | ||
} |