Skip to content

Commit c0e39af

Browse files
Evan PhilakhongEvan Philakhong
authored andcommitted
feat: adds printSponsors() tests
1 parent 1614b14 commit c0e39af

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/evanphilakhong/Formula1Car.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,4 @@ public boolean rainStrategy() {
7171
}
7272
return false;
7373
}
74-
75-
public static void main(String[] args) {
76-
Formula1Car formula1Car = new Formula1Car();
77-
Team team = Team.MCLAREN;
78-
formula1Car.setTeam(team);
79-
}
8074
}

lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/evanphilakhong/Formula1CarTest.java

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
56
import static org.junit.jupiter.api.Assertions.assertTrue;
67

78
import java.io.ByteArrayOutputStream;
89
import java.io.PrintStream;
10+
import org.junit.jupiter.api.AfterEach;
911
import org.junit.jupiter.api.BeforeEach;
1012
import 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

Comments
 (0)