Home | History | Annotate | Download | only in reflection
      1 /*
      2  * Copyright (c) 2007 Mockito contributors
      3  * This program is made available under the terms of the MIT License.
      4  */
      5 package org.mockito.internal.util.reflection;
      6 
      7 import org.junit.Test;
      8 
      9 import java.lang.reflect.Field;
     10 
     11 import static org.assertj.core.api.Assertions.assertThat;
     12 import static org.mockito.internal.util.reflection.Fields.syntheticField;
     13 
     14 public class FieldsTest {
     15 
     16     @Test
     17     public void fields_should_return_all_declared_fields_in_hierarchy() throws Exception {
     18         assertThat(Fields.allDeclaredFieldsOf(new HierarchyOfClasses()).filter(syntheticField()).names())
     19                 .containsOnly("a", "b", "static_a", "static_b");
     20     }
     21 
     22     @Test
     23     public void fields_should_return_declared_fields() throws Exception {
     24         assertThat(Fields.declaredFieldsOf(new HierarchyOfClasses()).filter(syntheticField()).names())
     25                 .containsOnly("b", "static_b");
     26     }
     27 
     28     @Test
     29     public void can_filter_not_null_fields() throws Exception {
     30         assertThat(Fields.declaredFieldsOf(new NullOrNotNullFields()).notNull().filter(syntheticField()).names())
     31                 .containsOnly("c");
     32     }
     33 
     34     @Test
     35     public void can_get_values_of_instance_fields() throws Exception {
     36         assertThat(Fields.declaredFieldsOf(new ValuedFields()).filter(syntheticField()).assignedValues())
     37                 .containsOnly("a", "b");
     38     }
     39 
     40 
     41     @Test
     42     public void can_get_list_of_InstanceField() throws Exception {
     43         ValuedFields instance = new ValuedFields();
     44 
     45         assertThat(Fields.declaredFieldsOf(instance).filter(syntheticField()).instanceFields())
     46                 .containsOnly(new InstanceField(field("a", instance), instance),
     47                               new InstanceField(field("b", instance), instance)
     48                 );
     49     }
     50 
     51     private Field field(String name, Object instance) throws NoSuchFieldException {
     52         return instance.getClass().getDeclaredField(name);
     53     }
     54 
     55 
     56     interface AnInterface {
     57         int someStaticInInterface = 0;
     58 
     59     }
     60     public static class ParentClass implements AnInterface {
     61         static int static_a;
     62         int a;
     63 
     64     }
     65     public static class HierarchyOfClasses extends ParentClass {
     66         static int static_b;
     67         int b = 1;
     68 
     69     }
     70     public static class NullOrNotNullFields {
     71         static Object static_b;
     72         Object b;
     73         Object c = new Object();
     74     }
     75 
     76     public static class ValuedFields {
     77         String a = "a";
     78         String b = "b";
     79     }
     80 }
     81