Просмотр исходного кода

Merge pull request #293 from michaelspets/junit_rules

Migrate to JUnit @Rule in triangle exercise [Fix #268]
FridaTveit 9 лет назад
Родитель
Сommit
4e98d87290
1 измененных файлов: 29 добавлений и 20 удалений
  1. 29
    20
      exercises/triangle/src/test/java/TriangleTest.java

+ 29
- 20
exercises/triangle/src/test/java/TriangleTest.java Просмотреть файл

1
 import org.junit.Test;
1
 import org.junit.Test;
2
 import org.junit.Ignore;
2
 import org.junit.Ignore;
3
+import org.junit.Rule;
4
+import org.junit.rules.ExpectedException;
3
 
5
 
4
 import static org.junit.Assert.assertEquals;
6
 import static org.junit.Assert.assertEquals;
5
 
7
 
6
 public class TriangleTest {
8
 public class TriangleTest {
7
 
9
 
10
+    @Rule
11
+    public final ExpectedException thrown = ExpectedException.none();
8
 
12
 
9
     @Test
13
     @Test
10
-    public void equilateralTriangleHaveEqualSides() throws Exception {
14
+    public void equilateralTriangleHaveEqualSides() throws TriangleException {
11
         Triangle triangle = new Triangle(2, 2, 2);
15
         Triangle triangle = new Triangle(2, 2, 2);
12
 
16
 
13
         assertEquals(TriangleKind.EQUILATERAL, triangle.getKind());
17
         assertEquals(TriangleKind.EQUILATERAL, triangle.getKind());
15
 
19
 
16
     @Ignore
20
     @Ignore
17
     @Test
21
     @Test
18
-    public void largerEquilateralTrianglesAlsoHaveEqualSides() throws Exception {
22
+    public void largerEquilateralTrianglesAlsoHaveEqualSides() throws TriangleException {
19
         Triangle triangle = new Triangle(10, 10, 10);
23
         Triangle triangle = new Triangle(10, 10, 10);
20
 
24
 
21
         assertEquals(TriangleKind.EQUILATERAL, triangle.getKind());
25
         assertEquals(TriangleKind.EQUILATERAL, triangle.getKind());
23
 
27
 
24
     @Ignore
28
     @Ignore
25
     @Test
29
     @Test
26
-    public void isoscelesTrianglesHaveLastTwoSidesEqual() throws Exception {
30
+    public void isoscelesTrianglesHaveLastTwoSidesEqual() throws TriangleException {
27
         Triangle triangle = new Triangle(3, 4, 4);
31
         Triangle triangle = new Triangle(3, 4, 4);
28
 
32
 
29
         assertEquals(TriangleKind.ISOSCELES, triangle.getKind());
33
         assertEquals(TriangleKind.ISOSCELES, triangle.getKind());
31
 
35
 
32
     @Ignore
36
     @Ignore
33
     @Test
37
     @Test
34
-    public void isoscelesTrianglesHaveFirstAndLastSidesEqual() throws Exception {
38
+    public void isoscelesTrianglesHaveFirstAndLastSidesEqual() throws TriangleException {
35
         Triangle triangle = new Triangle(4, 3, 4);
39
         Triangle triangle = new Triangle(4, 3, 4);
36
 
40
 
37
         assertEquals(TriangleKind.ISOSCELES, triangle.getKind());
41
         assertEquals(TriangleKind.ISOSCELES, triangle.getKind());
39
 
43
 
40
     @Ignore
44
     @Ignore
41
     @Test
45
     @Test
42
-    public void isoscelesTrianglesHaveTwoFirstSidesEqual() throws Exception {
46
+    public void isoscelesTrianglesHaveTwoFirstSidesEqual() throws TriangleException {
43
         Triangle triangle = new Triangle(4, 4, 3);
47
         Triangle triangle = new Triangle(4, 4, 3);
44
 
48
 
45
         assertEquals(TriangleKind.ISOSCELES, triangle.getKind());
49
         assertEquals(TriangleKind.ISOSCELES, triangle.getKind());
47
 
51
 
48
     @Ignore
52
     @Ignore
49
     @Test
53
     @Test
50
-    public void isoscelesTrianglesHaveInFactExactlyTwoSidesEqual() throws Exception {
54
+    public void isoscelesTrianglesHaveInFactExactlyTwoSidesEqual() throws TriangleException {
51
         Triangle triangle = new Triangle(10, 10, 2);
55
         Triangle triangle = new Triangle(10, 10, 2);
52
 
56
 
53
         assertEquals(TriangleKind.ISOSCELES, triangle.getKind());
57
         assertEquals(TriangleKind.ISOSCELES, triangle.getKind());
55
 
59
 
56
     @Ignore
60
     @Ignore
57
     @Test
61
     @Test
58
-    public void scaleneTrianglesHaveNoEqualSides() throws Exception {
62
+    public void scaleneTrianglesHaveNoEqualSides() throws TriangleException {
59
         Triangle triangle = new Triangle(3, 4, 5);
63
         Triangle triangle = new Triangle(3, 4, 5);
60
 
64
 
61
         assertEquals(TriangleKind.SCALENE, triangle.getKind());
65
         assertEquals(TriangleKind.SCALENE, triangle.getKind());
63
 
67
 
64
     @Ignore
68
     @Ignore
65
     @Test
69
     @Test
66
-    public void scaleneTrianglesHaveNoEqualSidesAtLargerScaleEither() throws Exception {
70
+    public void scaleneTrianglesHaveNoEqualSidesAtLargerScaleEither() throws TriangleException {
67
         Triangle triangle = new Triangle(10, 11, 12);
71
         Triangle triangle = new Triangle(10, 11, 12);
68
 
72
 
69
         assertEquals(TriangleKind.SCALENE, triangle.getKind());
73
         assertEquals(TriangleKind.SCALENE, triangle.getKind());
71
 
75
 
72
     @Ignore
76
     @Ignore
73
     @Test
77
     @Test
74
-    public void scaleneTrianglesHaveNoEqualSidesInDescendingOrderEither() throws Exception {
78
+    public void scaleneTrianglesHaveNoEqualSidesInDescendingOrderEither() throws TriangleException {
75
         Triangle triangle = new Triangle(5, 4, 2);
79
         Triangle triangle = new Triangle(5, 4, 2);
76
 
80
 
77
         assertEquals(TriangleKind.SCALENE, triangle.getKind());
81
         assertEquals(TriangleKind.SCALENE, triangle.getKind());
79
 
83
 
80
     @Ignore
84
     @Ignore
81
     @Test
85
     @Test
82
-    public void verySmallTrianglesAreLegal() throws Exception {
86
+    public void verySmallTrianglesAreLegal() throws TriangleException {
83
         Triangle triangle = new Triangle(0.4, 0.6, 0.3);
87
         Triangle triangle = new Triangle(0.4, 0.6, 0.3);
84
 
88
 
85
         assertEquals(TriangleKind.SCALENE, triangle.getKind());
89
         assertEquals(TriangleKind.SCALENE, triangle.getKind());
86
     }
90
     }
87
 
91
 
88
     @Ignore
92
     @Ignore
89
-    @Test(expected = TriangleException.class)
90
-    public void trianglesWithNoSizeAreIllegal() throws Exception {
93
+    @Test
94
+    public void trianglesWithNoSizeAreIllegal() throws TriangleException {
95
+        thrown.expect(TriangleException.class);
91
         new Triangle(0, 0, 0);
96
         new Triangle(0, 0, 0);
92
     }
97
     }
93
 
98
 
94
     @Ignore
99
     @Ignore
95
-    @Test(expected = TriangleException.class)
96
-    public void trianglesWithNegativeSidesAreIllegal() throws Exception {
100
+    @Test
101
+    public void trianglesWithNegativeSidesAreIllegal() throws TriangleException {
102
+        thrown.expect(TriangleException.class);
97
         new Triangle(3, 4, -5);
103
         new Triangle(3, 4, -5);
98
     }
104
     }
99
 
105
 
100
     @Ignore
106
     @Ignore
101
-    @Test(expected = TriangleException.class)
102
-    public void trianglesViolatingTriangleInequalityAreIllegal() throws Exception {
107
+    @Test
108
+    public void trianglesViolatingTriangleInequalityAreIllegal() throws TriangleException {
109
+        thrown.expect(TriangleException.class);
103
         new Triangle(1, 1, 3);
110
         new Triangle(1, 1, 3);
104
     }
111
     }
105
 
112
 
106
     @Ignore
113
     @Ignore
107
-    @Test(expected = TriangleException.class)
108
-    public void trianglesViolatingTriangleInequalityAreIllegal2() throws Exception {
114
+    @Test
115
+    public void trianglesViolatingTriangleInequalityAreIllegal2() throws TriangleException {
116
+        thrown.expect(TriangleException.class);
109
         new Triangle(2, 4, 2);
117
         new Triangle(2, 4, 2);
110
     }
118
     }
111
 
119
 
112
     @Ignore
120
     @Ignore
113
-    @Test(expected = TriangleException.class)
114
-    public void trianglesViolatingTriangleInequalityAreIllegal3() throws Exception {
121
+    @Test
122
+    public void trianglesViolatingTriangleInequalityAreIllegal3() throws TriangleException {
123
+        thrown.expect(TriangleException.class);
115
         new Triangle(7, 3, 2);
124
         new Triangle(7, 3, 2);
116
     }
125
     }
117
 }
126
 }