Skip to content

Commit 1614b14

Browse files
committed
feat: adds Formula1Car.java tests
1 parent 5852e49 commit 1614b14

File tree

2 files changed

+83
-2
lines changed
  • lesson_16/objects/objects_app/src

2 files changed

+83
-2
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,4 @@ public Track(
3737
this.drsZone = drsZone;
3838
this.weather = weather;
3939
}
40-
41-
public static void main(String[] args) {}
4240
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.codedifferently.lesson16.evanphilakhong;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import java.io.ByteArrayOutputStream;
8+
import java.io.PrintStream;
9+
import org.junit.jupiter.api.BeforeEach;
10+
import org.junit.jupiter.api.Test;
11+
12+
class Formula1CarTest {
13+
14+
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
15+
Track track;
16+
Formula1Car f1Car1;
17+
18+
@BeforeEach
19+
void setUp() {
20+
track = new Track();
21+
String[] f1Car1Sponsors = {"Hp", "Shell", "UniCredit", "IBM", "Puma", "VGW Play"};
22+
f1Car1 =
23+
new Formula1Car(Team.FERRARI, f1Car1Sponsors, "Lewis Hamilton", 44, 8, TyreCompound.MEDIUM);
24+
f1Car1.setTrack(track);
25+
System.setOut(new PrintStream(outContent));
26+
}
27+
28+
@Test
29+
void testIsDrsAvailibe() {
30+
assertFalse(f1Car1.isDrsAvailible());
31+
32+
// act
33+
track.setDrsZone(true);
34+
35+
// assert
36+
assertTrue(f1Car1.isDrsAvailible());
37+
}
38+
39+
@Test
40+
void testRainStrategy_returnsTrue() {
41+
// checks if tyres change depending on weather conditions
42+
track.setWeather(Weather.LIGHT_RAIN);
43+
assertTrue(f1Car1.rainStrategy());
44+
assertEquals(f1Car1.getCurrTyre(), TyreCompound.INTERMEDIATE);
45+
46+
track.setWeather(Weather.HEAVY_RAIN);
47+
assertTrue(f1Car1.rainStrategy());
48+
assertEquals(f1Car1.getCurrTyre(), TyreCompound.WET);
49+
}
50+
51+
@Test
52+
void testRainStrategy_returnsFalse() {
53+
// checks the default condition no rain
54+
assertFalse(f1Car1.rainStrategy());
55+
}
56+
57+
@Test
58+
void testPrintSponsors() throws Exception {
59+
if (f1Car1.getSponsors().length == 0) {
60+
throw new NoSponsorsException(f1Car1.getTeam() + " has no Sponsors");
61+
} else {
62+
String expected =
63+
"Here's a list of our Sponsors:"
64+
+ System.lineSeparator()
65+
+ "Hp"
66+
+ System.lineSeparator()
67+
+ "Shell"
68+
+ System.lineSeparator()
69+
+ "UniCredit"
70+
+ System.lineSeparator()
71+
+ "IBM"
72+
+ System.lineSeparator()
73+
+ "Puma"
74+
+ System.lineSeparator()
75+
+ "VGW Play"
76+
+ System.lineSeparator();
77+
78+
f1Car1.printSponsors();
79+
80+
assertEquals(expected, outContent.toString());
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)