Skip to content

Commit 8b0cf69

Browse files
authored
feat: Add test coverage for single-table-inheritance pattern (#3468)
* Add STI tests * test: add delta for floating-point comparison in VehicleServiceTest * fix: guard teardown against nulls and use assertNotNull in VehicleServiceTest Add null checks in @AfterEach tearDown() to prevent NullPointerException if setUp() partially fails. Replace assertTrue(x != null) with assertNotNull(x). * style: format code in SingleTableInheritanceTest and VehicleServiceTest for consistency
1 parent 5ff4168 commit 8b0cf69

2 files changed

Lines changed: 199 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package com.iluwatar;
26+
27+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
28+
29+
import org.junit.jupiter.api.Test;
30+
31+
/** Tests that SingleTableInheritance example application runs without errors. */
32+
class SingleTableInheritanceTest {
33+
34+
@Test
35+
void shouldExecuteWithoutException() {
36+
assertDoesNotThrow(() -> SingleTableInheritance.main(new String[] {}));
37+
}
38+
}
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package com.iluwatar.service;
26+
27+
import static org.junit.jupiter.api.Assertions.assertEquals;
28+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
29+
import static org.junit.jupiter.api.Assertions.assertNotNull;
30+
import static org.junit.jupiter.api.Assertions.assertNull;
31+
import static org.junit.jupiter.api.Assertions.assertTrue;
32+
33+
import com.iluwatar.entity.Car;
34+
import com.iluwatar.entity.Freighter;
35+
import com.iluwatar.entity.Train;
36+
import com.iluwatar.entity.Truck;
37+
import com.iluwatar.entity.Vehicle;
38+
import java.util.List;
39+
import org.junit.jupiter.api.AfterEach;
40+
import org.junit.jupiter.api.BeforeEach;
41+
import org.junit.jupiter.api.Test;
42+
import org.springframework.beans.factory.annotation.Autowired;
43+
import org.springframework.boot.test.context.SpringBootTest;
44+
45+
/**
46+
* Tests for {@link VehicleService} exercising CRUD operations and verifying that all concrete
47+
* Vehicle subtypes are persisted and retrieved correctly via the single-table inheritance strategy.
48+
*/
49+
@SpringBootTest
50+
class VehicleServiceTest {
51+
52+
private static final double DELTA = 0.001;
53+
54+
@Autowired private VehicleService vehicleService;
55+
56+
private Vehicle savedCar;
57+
private Vehicle savedTruck;
58+
private Vehicle savedTrain;
59+
private Vehicle savedFreighter;
60+
61+
@BeforeEach
62+
void setUp() {
63+
savedCar = vehicleService.saveVehicle(new Car("Tesla", "Model S", 4, 825));
64+
savedTruck = vehicleService.saveVehicle(new Truck("Ford", "F-150", 3325, 14000));
65+
savedTrain = vehicleService.saveVehicle(new Train("Siemens", "Velaro", 600, 8));
66+
savedFreighter = vehicleService.saveVehicle(new Freighter("Boeing", "747F", 100000, 8130));
67+
}
68+
69+
@AfterEach
70+
void tearDown() {
71+
if (savedCar != null) vehicleService.deleteVehicle(savedCar);
72+
if (savedTruck != null) vehicleService.deleteVehicle(savedTruck);
73+
if (savedTrain != null) vehicleService.deleteVehicle(savedTrain);
74+
if (savedFreighter != null) vehicleService.deleteVehicle(savedFreighter);
75+
}
76+
77+
@Test
78+
void saveVehicle_shouldPersistCarWithCorrectType() {
79+
assertTrue(savedCar.getVehicleId() > 0);
80+
assertInstanceOf(Car.class, savedCar);
81+
assertEquals("Tesla", savedCar.getManufacturer());
82+
assertEquals("Model S", savedCar.getModel());
83+
assertEquals(825, ((Car) savedCar).getEngineCapacity());
84+
}
85+
86+
@Test
87+
void saveVehicle_shouldPersistTruckWithCorrectType() {
88+
assertTrue(savedTruck.getVehicleId() > 0);
89+
assertInstanceOf(Truck.class, savedTruck);
90+
assertEquals(14000, ((Truck) savedTruck).getTowingCapacity());
91+
}
92+
93+
@Test
94+
void saveVehicle_shouldPersistTrainWithCorrectType() {
95+
assertTrue(savedTrain.getVehicleId() > 0);
96+
assertInstanceOf(Train.class, savedTrain);
97+
assertEquals(8, ((Train) savedTrain).getNoOfCarriages());
98+
}
99+
100+
@Test
101+
void saveVehicle_shouldPersistFreighterWithCorrectType() {
102+
assertTrue(savedFreighter.getVehicleId() > 0);
103+
assertInstanceOf(Freighter.class, savedFreighter);
104+
assertEquals(8130.0, ((Freighter) savedFreighter).getFlightLength(), DELTA);
105+
}
106+
107+
@Test
108+
void getVehicle_shouldReturnCorrectVehicleById() {
109+
Vehicle fetched = vehicleService.getVehicle(savedCar.getVehicleId());
110+
111+
assertNotNull(fetched);
112+
assertInstanceOf(Car.class, fetched);
113+
assertEquals(savedCar.getVehicleId(), fetched.getVehicleId());
114+
}
115+
116+
@Test
117+
void getVehicle_shouldReturnNullForNonexistentId() {
118+
Vehicle result = vehicleService.getVehicle(Integer.MAX_VALUE);
119+
assertNull(result);
120+
}
121+
122+
@Test
123+
void getAllVehicles_shouldReturnAllSavedVehicles() {
124+
List<Vehicle> all = vehicleService.getAllVehicles();
125+
assertNotNull(all);
126+
// At minimum the 4 vehicles saved in @BeforeEach must be present
127+
assertTrue_containsId(all, savedCar.getVehicleId());
128+
assertTrue_containsId(all, savedTruck.getVehicleId());
129+
assertTrue_containsId(all, savedTrain.getVehicleId());
130+
assertTrue_containsId(all, savedFreighter.getVehicleId());
131+
}
132+
133+
@Test
134+
void updateVehicle_shouldPersistChanges() {
135+
Car car = (Car) savedCar;
136+
car.setModel("Model X");
137+
vehicleService.updateVehicle(car);
138+
139+
Vehicle updated = vehicleService.getVehicle(car.getVehicleId());
140+
assertEquals("Model X", updated.getModel());
141+
}
142+
143+
@Test
144+
void deleteVehicle_shouldRemoveFromRepository() {
145+
// Save a temporary vehicle and delete it
146+
Vehicle tmp = vehicleService.saveVehicle(new Car("BMW", "M3", 4, 3000));
147+
int tmpId = tmp.getVehicleId();
148+
149+
vehicleService.deleteVehicle(tmp);
150+
151+
assertNull(vehicleService.getVehicle(tmpId));
152+
}
153+
154+
// Helper: assert that a specific vehicle id exists in the list
155+
private void assertTrue_containsId(List<Vehicle> vehicles, int id) {
156+
boolean found = vehicles.stream().anyMatch(v -> v.getVehicleId() == id);
157+
if (!found) {
158+
throw new AssertionError("Expected vehicle with id=" + id + " in list but was not found.");
159+
}
160+
}
161+
}

0 commit comments

Comments
 (0)