Created
October 24, 2012 12:33
-
-
Save wataru420/3945829 to your computer and use it in GitHub Desktop.
jMokitを使ってプライベートなフィールドにあくせすしたり
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
public class HogeService { | |
private static StringBuilder buffer; | |
private static char static1; | |
private static char static2; | |
private int intField; | |
private int intField2; | |
private String stringField; | |
private List<String> listField; | |
private HogeService() { intField = -1; } | |
private HogeService(int a, String b) { intField = a; stringField = b; } | |
private HogeService(String... args) { listField = Arrays.asList(args); } | |
private long aMethod() { return 567L; } | |
private static Boolean anStaticMethod() { return true; } | |
private void instanceMethod(short s, String str, Boolean b) {} | |
private static void staticMethod(short s, String str, Boolean b) {} | |
private final class InnerClass | |
{ | |
InnerClass() {} | |
InnerClass(boolean b, Long l, String s) {} | |
} | |
} |
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
import mockit.Deencapsulation; | |
public class HogeServiceTest { | |
HogeService target = new HogeService(); | |
/** | |
* 名前を元にフィールドに値をセットしたりゲットしたり | |
*/ | |
@Test | |
public void getSetFieldByName() | |
{ | |
// インスタンスフィールドの取得 | |
Integer intValue = Deencapsulation.getField(target, "intField"); | |
List<String> listValue = Deencapsulation.getField(target, "listField"); | |
// スタティックフィールドの取得 | |
StringBuilder b = getField(HogeService.class, "buffer"); | |
// インスタンスフィールドに値を入れる | |
Deencapsulation.setField(target, "intField2", 901); | |
// スタティックフィールドに値を入れる | |
Deencapsulation.setField(HogeService.class, "buffer", new StringBuilder()); | |
} | |
/** | |
* 型を元にフィールドに値をセットしたりゲットしたり | |
*/ | |
@Test | |
public void getSetFieldByType() | |
{ | |
// Get instance fields: | |
String stringValue = Deencapsulation.getField(target, String.class); | |
List<String> listValue = Deencapsulation.getField(target, List.class); | |
Set<Boolean> setValue = Deencapsulation.getField(target, HashSet.class); | |
Deencapsulation.getField(target, int.class); // ambiguous: will throw an IllegalArgumentException | |
// Get static fields: | |
StringBuilder b = Deencapsulation.getField(HogeService.class, StringBuilder.class); | |
// Set instance fields: | |
Deencapsulation.setField(target, "Test"); // will set SubClass#stringField, not BaseClass#baseString | |
Deencapsulation.setField(target, 901); // ambiguous: will throw an IllegalArgumentException | |
// Set static fields: | |
Deencapsulation.setField(HogeService.class, new StringBuilder()); | |
Deencapsulation.setField(HogeService.class, 'A'); // ambiguous: will throw an IllegalArgumentException | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://jmockit.googlecode.com/svn/trunk/www/tutorial/ReflectionUtilities.html#deencapsulation