forked from akrotov-education/practice-autumn-2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask03Test.java
67 lines (56 loc) · 2.03 KB
/
Task03Test.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
package lesson02.part02;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import static org.junit.Assert.*;
@RunWith(JUnit4.class)
public class Task03Test {
@Test
public void checkClassFields() {
Task03.Cat cat = new Task03.Cat();
Field[] fields = cat.getClass().getDeclaredFields();
Assert.assertTrue("Class Cat must contain only one field 'name'",
fields.length == 1 && fields[0].toString().contains("name")
);
}
@Test
public void checkFieldName() {
Task03.Cat cat = new Task03.Cat();
try {
Field field = cat.getClass().getDeclaredField("name");
Assert.assertTrue("Field 'name' must be private and have String type",
field.toString().contains("private") && field.toString().contains("String")
);
} catch (NoSuchFieldException e) {
Assert.fail("Class Cat doesn't have field name");
}
}
@Test
public void checkClassMethods() {
Method[] methods = Task03.Cat.class.getDeclaredMethods();
Assert.assertTrue("Class Cat must contain only one method setName",
methods.length == 2 && methods[1].getName().contentEquals("setName")
);
}
@Test
public void checkSetNameWork() {
Task03.Cat cat = new Task03.Cat();
cat.setName("Kek");
try {
Field f = cat.getClass().getDeclaredField("name"); //NoSuchFieldException
f.setAccessible(true);
String val = (String)f.get(cat);
Assert.assertEquals("Method setName should set name from transmitted parameter",
"Kek",
val
);
} catch (NoSuchFieldException e) {
Assert.fail("Class doesn't contain field name");
} catch (IllegalAccessException e) {
Assert.fail("Can't get access to private field");
}
}
}