Parcourir la source

Extract common setup

WordProblemSolver no longer instantiates in each test
Souvatzis Vasilios il y a 9 ans
Parent
révision
d81c79f2ab
1 fichiers modifiés avec 24 ajouts et 16 suppressions
  1. 24
    16
      exercises/wordy/src/test/java/WordProblemSolverTest.java

+ 24
- 16
exercises/wordy/src/test/java/WordProblemSolverTest.java Voir le fichier

1
 import org.junit.Ignore;
1
 import org.junit.Ignore;
2
 import org.junit.Rule;
2
 import org.junit.Rule;
3
 import org.junit.Test;
3
 import org.junit.Test;
4
+import org.junit.Before;
4
 import org.junit.rules.ExpectedException;
5
 import org.junit.rules.ExpectedException;
5
 
6
 
6
 import static org.junit.Assert.assertEquals;
7
 import static org.junit.Assert.assertEquals;
14
     @Rule
15
     @Rule
15
     public ExpectedException expectedException = ExpectedException.none();
16
     public ExpectedException expectedException = ExpectedException.none();
16
 
17
 
18
+    WordProblemSolver solver;
19
+
20
+    @Before
21
+    public void setup() {
22
+        solver = new WordProblemSolver();
23
+    }
24
+
17
     @Test
25
     @Test
18
     public void testSingleAddition1() {
26
     public void testSingleAddition1() {
19
-        assertEquals(2, new WordProblemSolver().solve("What is 1 plus 1?"));
27
+        assertEquals(2, solver.solve("What is 1 plus 1?"));
20
     }
28
     }
21
 
29
 
22
     @Ignore
30
     @Ignore
23
     @Test
31
     @Test
24
     public void testSingleAddition2() {
32
     public void testSingleAddition2() {
25
-        assertEquals(55, new WordProblemSolver().solve("What is 53 plus 2?"));
33
+        assertEquals(55, solver.solve("What is 53 plus 2?"));
26
     }
34
     }
27
 
35
 
28
     @Ignore
36
     @Ignore
29
     @Test
37
     @Test
30
     public void testSingleAdditionWithNegativeNumbers() {
38
     public void testSingleAdditionWithNegativeNumbers() {
31
-        assertEquals(-11, new WordProblemSolver().solve("What is -1 plus -10?"));
39
+        assertEquals(-11, solver.solve("What is -1 plus -10?"));
32
     }
40
     }
33
 
41
 
34
     @Ignore
42
     @Ignore
35
     @Test
43
     @Test
36
     public void testSingleAdditionOfLargeNumbers() {
44
     public void testSingleAdditionOfLargeNumbers() {
37
-        assertEquals(45801, new WordProblemSolver().solve("What is 123 plus 45678?"));
45
+        assertEquals(45801, solver.solve("What is 123 plus 45678?"));
38
     }
46
     }
39
 
47
 
40
     @Ignore
48
     @Ignore
41
     @Test
49
     @Test
42
     public void testSingleSubtraction() {
50
     public void testSingleSubtraction() {
43
-        assertEquals(16, new WordProblemSolver().solve("What is 4 minus -12?"));
51
+        assertEquals(16, solver.solve("What is 4 minus -12?"));
44
     }
52
     }
45
 
53
 
46
     @Ignore
54
     @Ignore
47
     @Test
55
     @Test
48
     public void testSingleMultiplication() {
56
     public void testSingleMultiplication() {
49
-        assertEquals(-75, new WordProblemSolver().solve("What is -3 multiplied by 25?"));
57
+        assertEquals(-75, solver.solve("What is -3 multiplied by 25?"));
50
     }
58
     }
51
 
59
 
52
     @Ignore
60
     @Ignore
53
     @Test
61
     @Test
54
     public void testSingleDivision() {
62
     public void testSingleDivision() {
55
-        assertEquals(-11, new WordProblemSolver().solve("What is 33 divided by -3?"));
63
+        assertEquals(-11, solver.solve("What is 33 divided by -3?"));
56
     }
64
     }
57
 
65
 
58
     @Ignore
66
     @Ignore
59
     @Test
67
     @Test
60
     public void testMultipleAdditions() {
68
     public void testMultipleAdditions() {
61
-        assertEquals(3, new WordProblemSolver().solve("What is 1 plus 1 plus 1?"));
69
+        assertEquals(3, solver.solve("What is 1 plus 1 plus 1?"));
62
     }
70
     }
63
 
71
 
64
     @Ignore
72
     @Ignore
65
     @Test
73
     @Test
66
     public void testAdditionThenSubtraction() {
74
     public void testAdditionThenSubtraction() {
67
-        assertEquals(8, new WordProblemSolver().solve("What is 1 plus 5 minus -2?"));
75
+        assertEquals(8, solver.solve("What is 1 plus 5 minus -2?"));
68
     }
76
     }
69
 
77
 
70
     @Ignore
78
     @Ignore
71
     @Test
79
     @Test
72
     public void testMultipleSubtractions() {
80
     public void testMultipleSubtractions() {
73
-        assertEquals(3, new WordProblemSolver().solve("What is 20 minus 4 minus 13?"));
81
+        assertEquals(3, solver.solve("What is 20 minus 4 minus 13?"));
74
     }
82
     }
75
 
83
 
76
     @Ignore
84
     @Ignore
77
     @Test
85
     @Test
78
     public void testSubtractionThenAddition() {
86
     public void testSubtractionThenAddition() {
79
-        assertEquals(14, new WordProblemSolver().solve("What is 17 minus 6 plus 3?"));
87
+        assertEquals(14, solver.solve("What is 17 minus 6 plus 3?"));
80
     }
88
     }
81
 
89
 
82
     @Ignore
90
     @Ignore
83
     @Test
91
     @Test
84
     public void testMultipleMultiplications() {
92
     public void testMultipleMultiplications() {
85
-        assertEquals(-12, new WordProblemSolver().solve("What is 2 multiplied by -2 multiplied by 3?"));
93
+        assertEquals(-12, solver.solve("What is 2 multiplied by -2 multiplied by 3?"));
86
     }
94
     }
87
 
95
 
88
     @Ignore
96
     @Ignore
89
     @Test
97
     @Test
90
     public void testAdditionThenMultiplication() {
98
     public void testAdditionThenMultiplication() {
91
-        assertEquals(-8, new WordProblemSolver().solve("What is -3 plus 7 multiplied by -2?"));
99
+        assertEquals(-8, solver.solve("What is -3 plus 7 multiplied by -2?"));
92
     }
100
     }
93
 
101
 
94
     @Ignore
102
     @Ignore
95
     @Test
103
     @Test
96
     public void testMultipleDivisions() {
104
     public void testMultipleDivisions() {
97
-        assertEquals(2, new WordProblemSolver().solve("What is -12 divided by 2 divided by -3?"));
105
+        assertEquals(2, solver.solve("What is -12 divided by 2 divided by -3?"));
98
     }
106
     }
99
 
107
 
100
     @Ignore
108
     @Ignore
103
         expectedException.expect(IllegalArgumentException.class);
111
         expectedException.expect(IllegalArgumentException.class);
104
         expectedException.expectMessage("I'm sorry, I don't understand the question!");
112
         expectedException.expectMessage("I'm sorry, I don't understand the question!");
105
 
113
 
106
-        new WordProblemSolver().solve("What is 52 cubed?");
114
+        solver.solve("What is 52 cubed?");
107
     }
115
     }
108
 
116
 
109
     @Ignore
117
     @Ignore
113
         expectedException.expectMessage("I'm sorry, I don't understand the question!");
121
         expectedException.expectMessage("I'm sorry, I don't understand the question!");
114
 
122
 
115
         // See https://en.wikipedia.org/wiki/President_of_the_United_States if you really need to know!
123
         // See https://en.wikipedia.org/wiki/President_of_the_United_States if you really need to know!
116
-        new WordProblemSolver().solve("Who is the President of the United States?");
124
+        solver.solve("Who is the President of the United States?");
117
     }
125
     }
118
 
126
 
119
 }
127
 }