Skip to content

Commit 41c92de

Browse files
authored
Update need for speed to use test runner v3 features (#2500)
1 parent 206bc9a commit 41c92de

File tree

2 files changed

+53
-13
lines changed

2 files changed

+53
-13
lines changed

exercises/concept/need-for-speed/build.gradle

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
apply plugin: "java"
2-
apply plugin: "eclipse"
3-
apply plugin: "idea"
4-
5-
// set default encoding to UTF-8
6-
compileJava.options.encoding = "UTF-8"
7-
compileTestJava.options.encoding = "UTF-8"
1+
plugins {
2+
id "java"
3+
}
84

95
repositories {
106
mavenCentral()
117
}
128

139
dependencies {
14-
testImplementation "junit:junit:4.13"
10+
testImplementation platform("org.junit:junit-bom:5.10.0")
11+
testImplementation "org.junit.jupiter:junit-jupiter"
1512
testImplementation "org.assertj:assertj-core:3.15.0"
1613
}
1714

1815
test {
16+
useJUnitPlatform()
17+
1918
testLogging {
20-
exceptionFormat = 'full'
19+
exceptionFormat = "full"
2120
showStandardStreams = true
2221
events = ["passed", "failed", "skipped"]
2322
}

exercises/concept/need-for-speed/src/test/java/NeedForSpeedTest.java

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
import static org.assertj.core.api.Assertions.assertThat;
1+
import org.junit.jupiter.api.DisplayName;
2+
import org.junit.jupiter.api.Tag;
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.assertj.core.api.Assertions.*;
26

3-
import org.junit.Test;
47

58
public class NeedForSpeedTest {
9+
610
@Test
11+
@Tag("task:3")
12+
@DisplayName("The distanceDriven method returns 0 on a new car")
713
public void new_remote_control_car_has_not_driven_any_distance() {
814
int speed = 10;
915
int batteryDrain = 2;
@@ -13,6 +19,8 @@ public void new_remote_control_car_has_not_driven_any_distance() {
1319
}
1420

1521
@Test
22+
@Tag("task:3")
23+
@DisplayName("The distanceDriven method returns 5 after driving once")
1624
public void drive_increases_distance_driven_with_speed() {
1725
int speed = 5;
1826
int batteryDrain = 1;
@@ -24,6 +32,8 @@ public void drive_increases_distance_driven_with_speed() {
2432
}
2533

2634
@Test
35+
@Tag("task:3")
36+
@DisplayName("The distanceDriven method returns the correct distance after driving multiple times")
2737
public void drive_does_not_increase_distance_driven_when_battery_drained() {
2838
int speed = 9;
2939
int batteryDrain = 50;
@@ -40,6 +50,8 @@ public void drive_does_not_increase_distance_driven_when_battery_drained() {
4050
}
4151

4252
@Test
53+
@Tag("task:4")
54+
@DisplayName("The batteryDrained method returns false when car never drove")
4355
public void new_remote_control_car_battery_is_not_drained() {
4456
int speed = 15;
4557
int batteryDrain = 3;
@@ -49,6 +61,8 @@ public void new_remote_control_car_battery_is_not_drained() {
4961
}
5062

5163
@Test
64+
@Tag("task:4")
65+
@DisplayName("The batteryDrained method returns false when car battery did not completely drain after driving")
5266
public void drive_to_almost_drain_battery() {
5367
int speed = 2;
5468
int batteryDrain = 1;
@@ -63,6 +77,8 @@ public void drive_to_almost_drain_battery() {
6377
}
6478

6579
@Test
80+
@Tag("task:4")
81+
@DisplayName("The batteryDrained method returns true when battery completely drained after driving")
6682
public void drive_until_battery_is_drained() {
6783
int speed = 2;
6884
int batteryDrain = 1;
@@ -77,25 +93,33 @@ public void drive_until_battery_is_drained() {
7793
}
7894

7995
@Test
96+
@Tag("task:5")
97+
@DisplayName("The distanceDriven method returns 0 on a new nitro car")
8098
public void nitro_car_has_not_driven_any_distance() {
8199
var car = NeedForSpeed.nitro();
82100
assertThat(car.distanceDriven()).isEqualTo(0);
83101
}
84102

85103
@Test
104+
@Tag("task:5")
105+
@DisplayName("The batteryDrained method returns false when nitro car never drove")
86106
public void nitro_car_has_battery_not_drained() {
87107
var car = NeedForSpeed.nitro();
88108
assertThat(car.batteryDrained()).isFalse();
89109
}
90110

91111
@Test
112+
@Tag("task:5")
113+
@DisplayName("The distanceDriven method returns the correct distance after driving a nitro car")
92114
public void nitro_car_has_correct_speed() {
93115
var car = NeedForSpeed.nitro();
94116
car.drive();
95117
assertThat(car.distanceDriven()).isEqualTo(50);
96118
}
97119

98120
@Test
121+
@Tag("task:5")
122+
@DisplayName("The batteryDrained method returns false when nitro battery did not completely drain after driving")
99123
public void nitro_has_correct_battery_drain() {
100124
var car = NeedForSpeed.nitro();
101125

@@ -105,14 +129,25 @@ public void nitro_has_correct_battery_drain() {
105129
}
106130

107131
assertThat(car.batteryDrained()).isFalse();
132+
}
108133

109-
// Drain the battery
110-
car.drive();
134+
@Test
135+
@Tag("task:5")
136+
@DisplayName("The batteryDrained method returns true when nitro battery completely drained after driving")
137+
public void nitro_battery_completely_drains() {
138+
var car = NeedForSpeed.nitro();
139+
140+
// The battery is almost drained
141+
for (var i = 0; i < 25; i++) {
142+
car.drive();
143+
}
111144

112145
assertThat(car.batteryDrained()).isTrue();
113146
}
114147

115148
@Test
149+
@Tag("task:6")
150+
@DisplayName("The tryFinishCar method returns true when car can finish a race")
116151
public void car_can_finish_with_car_that_can_easily_finish() {
117152
int speed = 10;
118153
int batteryDrain = 2;
@@ -125,6 +160,8 @@ public void car_can_finish_with_car_that_can_easily_finish() {
125160
}
126161

127162
@Test
163+
@Tag("task:6")
164+
@DisplayName("The tryFinishCar method returns true when car can just finish a race")
128165
public void car_can_finish_with_car_that_can_just_finish() {
129166
int speed = 2;
130167
int batteryDrain = 10;
@@ -137,6 +174,8 @@ public void car_can_finish_with_car_that_can_just_finish() {
137174
}
138175

139176
@Test
177+
@Tag("task:6")
178+
@DisplayName("The tryFinishCar method returns false when car just cannot finish a race")
140179
public void car_can_finish_with_car_that_just_cannot_finish() {
141180
int speed = 3;
142181
int batteryDrain = 20;
@@ -149,6 +188,8 @@ public void car_can_finish_with_car_that_just_cannot_finish() {
149188
}
150189

151190
@Test
191+
@Tag("task:6")
192+
@DisplayName("The tryFinishCar method returns false when car cannot finish a race")
152193
public void car_can_finish_with_car_that_cannot_finish() {
153194
int speed = 1;
154195
int batteryDrain = 20;

0 commit comments

Comments
 (0)