2
2
3
3
import static org .junit .jupiter .api .Assertions .assertEquals ;
4
4
import static org .junit .jupiter .api .Assertions .assertFalse ;
5
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
5
6
import static org .junit .jupiter .api .Assertions .assertTrue ;
6
7
7
8
import java .io .ByteArrayOutputStream ;
8
9
import java .io .PrintStream ;
10
+ import org .junit .jupiter .api .AfterEach ;
9
11
import org .junit .jupiter .api .BeforeEach ;
10
12
import org .junit .jupiter .api .Test ;
11
13
@@ -14,28 +16,44 @@ class Formula1CarTest {
14
16
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream ();
15
17
Track track ;
16
18
Formula1Car f1Car1 ;
19
+ Formula1Car f1Car2 ;
17
20
18
21
@ BeforeEach
19
22
void setUp () {
20
23
track = new Track ();
24
+
21
25
String [] f1Car1Sponsors = {"Hp" , "Shell" , "UniCredit" , "IBM" , "Puma" , "VGW Play" };
22
26
f1Car1 =
23
27
new Formula1Car (Team .FERRARI , f1Car1Sponsors , "Lewis Hamilton" , 44 , 8 , TyreCompound .MEDIUM );
24
28
f1Car1 .setTrack (track );
29
+
30
+ String [] f1Car2Sponsors = {};
31
+ f1Car2 =
32
+ new Formula1Car (Team .MCLAREN , f1Car2Sponsors , "Lando Norris" , 4 , 2 , TyreCompound .MEDIUM );
33
+ f1Car2 .setTrack (track );
34
+
25
35
System .setOut (new PrintStream (outContent ));
26
36
}
27
37
28
- @ Test
29
- void testIsDrsAvailibe () {
30
- assertFalse (f1Car1 .isDrsAvailible ());
38
+ @ AfterEach
39
+ void restoreStreams () {
40
+ System .setOut (System .out );
41
+ }
31
42
32
- // act
43
+ @ Test
44
+ void testIsDrsAvailibe_returnsTrue () {
33
45
track .setDrsZone (true );
34
46
35
- // assert
36
47
assertTrue (f1Car1 .isDrsAvailible ());
37
48
}
38
49
50
+ @ Test
51
+ void testIsDrsAvailibe_returnsFalse () {
52
+ track .setDrsZone (false );
53
+
54
+ assertFalse (f1Car1 .isDrsAvailible ());
55
+ }
56
+
39
57
@ Test
40
58
void testRainStrategy_returnsTrue () {
41
59
// checks if tyres change depending on weather conditions
@@ -55,7 +73,7 @@ void testRainStrategy_returnsFalse() {
55
73
}
56
74
57
75
@ Test
58
- void testPrintSponsors () throws Exception {
76
+ void testPrintSponsors_withSponsors () throws NoSponsorsException {
59
77
if (f1Car1 .getSponsors ().length == 0 ) {
60
78
throw new NoSponsorsException (f1Car1 .getTeam () + " has no Sponsors" );
61
79
} else {
@@ -80,4 +98,12 @@ void testPrintSponsors() throws Exception {
80
98
assertEquals (expected , outContent .toString ());
81
99
}
82
100
}
101
+
102
+ @ Test
103
+ void testPrintSponsors_noSponsors () {
104
+ NoSponsorsException exception =
105
+ assertThrows (NoSponsorsException .class , () -> f1Car2 .printSponsors ());
106
+
107
+ assertEquals (f1Car2 .getTeam () + " has no Sponsors" , exception .getMessage ());
108
+ }
83
109
}
0 commit comments