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