22
33import static org .junit .jupiter .api .Assertions .assertEquals ;
44import static org .junit .jupiter .api .Assertions .assertFalse ;
5+ import static org .junit .jupiter .api .Assertions .assertThrows ;
56import static org .junit .jupiter .api .Assertions .assertTrue ;
67
78import java .io .ByteArrayOutputStream ;
89import java .io .PrintStream ;
10+ import org .junit .jupiter .api .AfterEach ;
911import org .junit .jupiter .api .BeforeEach ;
1012import org .junit .jupiter .api .Test ;
1113
@@ -14,28 +16,44 @@ class Formula1CarTest {
1416 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream ();
1517 Track track ;
1618 Formula1Car f1Car1 ;
19+ Formula1Car f1Car2 ;
1720
1821 @ BeforeEach
1922 void setUp () {
2023 track = new Track ();
24+
2125 String [] f1Car1Sponsors = {"Hp" , "Shell" , "UniCredit" , "IBM" , "Puma" , "VGW Play" };
2226 f1Car1 =
2327 new Formula1Car (Team .FERRARI , f1Car1Sponsors , "Lewis Hamilton" , 44 , 8 , TyreCompound .MEDIUM );
2428 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+
2535 System .setOut (new PrintStream (outContent ));
2636 }
2737
28- @ Test
29- void testIsDrsAvailibe () {
30- assertFalse (f1Car1 .isDrsAvailible ());
38+ @ AfterEach
39+ void restoreStreams () {
40+ System .setOut (System .out );
41+ }
3142
32- // act
43+ @ Test
44+ void testIsDrsAvailibe_returnsTrue () {
3345 track .setDrsZone (true );
3446
35- // assert
3647 assertTrue (f1Car1 .isDrsAvailible ());
3748 }
3849
50+ @ Test
51+ void testIsDrsAvailibe_returnsFalse () {
52+ track .setDrsZone (false );
53+
54+ assertFalse (f1Car1 .isDrsAvailible ());
55+ }
56+
3957 @ Test
4058 void testRainStrategy_returnsTrue () {
4159 // checks if tyres change depending on weather conditions
@@ -55,7 +73,7 @@ void testRainStrategy_returnsFalse() {
5573 }
5674
5775 @ Test
58- void testPrintSponsors () throws Exception {
76+ void testPrintSponsors_withSponsors () throws NoSponsorsException {
5977 if (f1Car1 .getSponsors ().length == 0 ) {
6078 throw new NoSponsorsException (f1Car1 .getTeam () + " has no Sponsors" );
6179 } else {
@@ -80,4 +98,12 @@ void testPrintSponsors() throws Exception {
8098 assertEquals (expected , outContent .toString ());
8199 }
82100 }
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+ }
83109}
0 commit comments