понедельник, 24 марта 2014 г.

JMockit. Reflection-based utilities

Все утилиты этого инструмента из пакета  mockit.Deencapsulation  статические и без состояний. Все это ни что иное как обвертки более высокоуровневого API над Reflection API .
Итак давайте потестим вот что:
import static mockit.Deencapsulation.*;

public final class DeencapsulationTest
{
   final SubClass anInstance = new SubClass();

   static class BaseClass
   {
      protected int baseInt;
      protected String baseString;
      protected Set<boolean> baseSet;
   }

   static final class SubClass extends BaseClass
   {
      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 SubClass() { intField = -1; }
      private SubClass(int a, String b) { intField = a; stringField = b; }
      private SubClass(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) {}
      }
   }

   // example tests
}
А теперь сами вызовы:

Получение и установка полей по именам и по типам

   @Test
   public void getSetFieldByName()
   {
      // Get instance fields:
      Integer intValue = getField(anInstance, "intField");
      List<String> listValue = getField(anInstance, "listField");

      // Get static fields:
      StringBuilder b = getField(SubClass.class, "buffer");

      // Set instance fields:
      setField(anInstance, "intField2", 901);

      // Set static fields:
      setField(SubClass.class, "buffer", new StringBuilder());
   }

   @Test
   public void getSetFieldByType()
   {
      // Get instance fields:
      String stringValue = getField(anInstance, String.class);
      List<string> listValue = getField(anInstance, List.class);
      Set<boolean> setValue = getField(anInstance, HashSet.class);
      getField(anInstance, int.class); // ambiguous: will throw an IllegalArgumentException

      // Get static fields:
      StringBuilder b = getField(SubClass.class, StringBuilder.class);

      // Set instance fields:
      setField(anInstance, "Test"); // will set SubClass#stringField, not BaseClass#baseString
      setField(anInstance, 901); // ambiguous: will throw an IllegalArgumentException

      // Set static fields:
      setField(SubClass.class, new StringBuilder());
      setField(SubClass.class, 'A'); // ambiguous: will throw an IllegalArgumentException
   }

Вызов недоступных методов

   @Test
   public void invokeMethod()
   {
      // Instance methods:
      Long l = invoke(anInstance, "aMethod");
      String s = invoke(anInstance, "instanceMethod", (short) 7, "abc", true);

      // Static methods:
      Boolean b = invoke(SubClass.class, "anStaticMethod");
      Object result = invoke(SubClass.class, "staticMethod", (short) 7, "abc", true);
   }

Создание инстанций через недоступные конструктора

   @Test
   public void newInstance()
   {
      // Instances of top-level or non-inner nested classes:
      SubClass instance = newInstance(SubClass.class.getName());
      SubClass instance2 = newInstance("DeencapsulationTest$Subclass", 1, "XYZ");

      // Instances of inner classes:
      SubClass.InnerClass innerInstance = newInnerInstance("InnerClass", anInstance);
      SubClass.InnerClass innerInstance2 = newInnerInstance("InnerClass", anInstance, true, 5L, "");
   }

Комментариев нет:

Отправить комментарий