-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestStudent.java
131 lines (101 loc) · 2.99 KB
/
TestStudent.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import static org.junit.Assert.*;
import java.util.ArrayList;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
public class TestStudent {
//test fixture
static Student student = null;
static ArrayList<Integer> grades = null;
static int initialAge = 19;
static String initialName = "John Doe";
@BeforeClass
public static void setUpBeforeClass() throws Exception {
grades = new ArrayList<>();
grades.add(5);
grades.add(9);
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
grades.clear();
grades = null;
}
@Before
public void setUp() throws Exception {
student = new Student(initialName, initialAge, grades);
}
@After
public void tearDown() throws Exception {
student = null;
}
@Ignore
@Test
public void test() {
fail("Not yet implemented");
}
@Test
public void testSetNameRightValue() throws WrongNameException {
String newName = "Alice";
student.setName(newName);
assertEquals("Testing with a right name", newName, student.getName());
}
@Test
public void testSetAgeRightValue() {
int newAge = initialAge + 1;
try {
student.setAge(newAge);
assertEquals("Test for right value", newAge, student.getAge());
} catch (WrongAgeException e) {
fail("We got an exception for a right value");
}
}
@Test
public void testSetAgeErrorCondition() {
int newAge = initialAge * -1;
try {
student.setAge(newAge);
fail("We didn't got an exception for a negative age");
} catch (WrongAgeException e) {
assertTrue(true);
}
}
@Test(expected = WrongAgeException.class)
public void testSetAgeErrorConditionGreaterThanMax() throws WrongAgeException {
int newAge = Student.MAX_AGE + 1000;
student.setAge(newAge);
}
@Test
public void testGetGradesAverageAscendingOrder() throws WrongGradesException {
ArrayList<Integer> sortedGrades = new ArrayList<>();
sortedGrades.add(7);
sortedGrades.add(8);
sortedGrades.add(9);
sortedGrades.add(10);
student.setGrades(sortedGrades);
float expectedAverage = 8.5f;
float computedAverage = student.getGradesAverage();
assertEquals(
"Testing with an ascending sorted set of grades", expectedAverage, computedAverage,0);
}
@Test
public void testGetGradesAverageCardinalityZero() throws WrongGradesException {
ArrayList<Integer> grades = new ArrayList<>();
student.setGrades(grades);
float expectedAverage = 0;
float computedAverage = student.getGradesAverage();
assertEquals("Testing with an empty set of grades", expectedAverage, computedAverage, 0);
}
@Test
public void testGetGradesAverageCardinalityOne() throws WrongGradesException {
ArrayList<Integer> grades = new ArrayList<>();
int theGrade = 10;
grades.add(theGrade);
student.setGrades(grades);
float expectedAverage = theGrade;
float computedAverage = student.getGradesAverage();
assertEquals("Testing with one grade", expectedAverage, computedAverage, 0);
}
}