-
-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Add test coverage for single-table-inheritance pattern #3468
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rranjangupta
wants to merge
4
commits into
iluwatar:master
Choose a base branch
from
rranjangupta:add-single-table-inheritance-tests
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1f451f2
Add STI tests
rranjangupta 3fdda66
test: add delta for floating-point comparison in VehicleServiceTest
rranjangupta 0476a66
fix: guard teardown against nulls and use assertNotNull in VehicleSer…
rranjangupta 5c78862
style: format code in SingleTableInheritanceTest and VehicleServiceTe…
rranjangupta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
single-table-inheritance/src/test/java/com/iluwatar/SingleTableInheritanceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). | ||
| * | ||
| * The MIT License | ||
| * Copyright © 2014-2022 Ilkka Seppälä | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| * THE SOFTWARE. | ||
| */ | ||
| package com.iluwatar; | ||
|
|
||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
|
|
||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
|
|
||
| /** Tests that SingleTableInheritance example application runs without errors. */ | ||
| class SingleTableInheritanceTest { | ||
|
|
||
|
|
||
| @Test | ||
| void shouldExecuteWithoutException() { | ||
| assertDoesNotThrow(() -> SingleTableInheritance.main(new String[] {})); | ||
| } | ||
| } | ||
|
|
179 changes: 179 additions & 0 deletions
179
single-table-inheritance/src/test/java/com/iluwatar/service/VehicleServiceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| /* | ||
| * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). | ||
| * | ||
| * The MIT License | ||
| * Copyright © 2014-2022 Ilkka Seppälä | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| * THE SOFTWARE. | ||
| */ | ||
| package com.iluwatar.service; | ||
|
|
||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertInstanceOf; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
|
|
||
| import com.iluwatar.entity.Car; | ||
| import com.iluwatar.entity.Freighter; | ||
| import com.iluwatar.entity.Train; | ||
| import com.iluwatar.entity.Truck; | ||
| import com.iluwatar.entity.Vehicle; | ||
| import java.util.List; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.test.context.SpringBootTest; | ||
|
|
||
|
|
||
| /** | ||
| * Tests for {@link VehicleService} exercising CRUD operations and verifying that all concrete | ||
| * Vehicle subtypes are persisted and retrieved correctly via the single-table inheritance strategy. | ||
| */ | ||
| @SpringBootTest | ||
| class VehicleServiceTest { | ||
|
|
||
| private static final double DELTA = 0.001; | ||
|
|
||
| @Autowired private VehicleService vehicleService; | ||
|
|
||
|
|
||
| private Vehicle savedCar; | ||
| private Vehicle savedTruck; | ||
| private Vehicle savedTrain; | ||
| private Vehicle savedFreighter; | ||
|
|
||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| savedCar = vehicleService.saveVehicle(new Car("Tesla", "Model S", 4, 825)); | ||
| savedTruck = vehicleService.saveVehicle(new Truck("Ford", "F-150", 3325, 14000)); | ||
| savedTrain = vehicleService.saveVehicle(new Train("Siemens", "Velaro", 600, 8)); | ||
| savedFreighter = vehicleService.saveVehicle(new Freighter("Boeing", "747F", 100000, 8130)); | ||
| } | ||
|
|
||
|
|
||
| @AfterEach | ||
| void tearDown() { | ||
| vehicleService.deleteVehicle(savedCar); | ||
| vehicleService.deleteVehicle(savedTruck); | ||
| vehicleService.deleteVehicle(savedTrain); | ||
| vehicleService.deleteVehicle(savedFreighter); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| void saveVehicle_shouldPersistCarWithCorrectType() { | ||
| assertTrue(savedCar.getVehicleId() > 0); | ||
| assertInstanceOf(Car.class, savedCar); | ||
| assertEquals("Tesla", savedCar.getManufacturer()); | ||
| assertEquals("Model S", savedCar.getModel()); | ||
| assertEquals(825, ((Car) savedCar).getEngineCapacity()); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| void saveVehicle_shouldPersistTruckWithCorrectType() { | ||
| assertTrue(savedTruck.getVehicleId() > 0); | ||
| assertInstanceOf(Truck.class, savedTruck); | ||
| assertEquals(14000, ((Truck) savedTruck).getTowingCapacity()); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| void saveVehicle_shouldPersistTrainWithCorrectType() { | ||
| assertTrue(savedTrain.getVehicleId() > 0); | ||
| assertInstanceOf(Train.class, savedTrain); | ||
| assertEquals(8, ((Train) savedTrain).getNoOfCarriages()); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| void saveVehicle_shouldPersistFreighterWithCorrectType() { | ||
| assertTrue(savedFreighter.getVehicleId() > 0); | ||
| assertInstanceOf(Freighter.class, savedFreighter); | ||
| assertEquals(8130.0, ((Freighter) savedFreighter).getFlightLength(), DELTA); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| void getVehicle_shouldReturnCorrectVehicleById() { | ||
| Vehicle fetched = vehicleService.getVehicle(savedCar.getVehicleId()); | ||
| assertTrue(fetched != null); | ||
| assertInstanceOf(Car.class, fetched); | ||
| assertEquals(savedCar.getVehicleId(), fetched.getVehicleId()); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| void getVehicle_shouldReturnNullForNonexistentId() { | ||
| Vehicle result = vehicleService.getVehicle(Integer.MAX_VALUE); | ||
| assertNull(result); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| void getAllVehicles_shouldReturnAllSavedVehicles() { | ||
| List<Vehicle> all = vehicleService.getAllVehicles(); | ||
| assertTrue(all != null); | ||
| // At minimum the 4 vehicles saved in @BeforeEach must be present | ||
| assertTrue_containsId(all, savedCar.getVehicleId()); | ||
| assertTrue_containsId(all, savedTruck.getVehicleId()); | ||
| assertTrue_containsId(all, savedTrain.getVehicleId()); | ||
| assertTrue_containsId(all, savedFreighter.getVehicleId()); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| void updateVehicle_shouldPersistChanges() { | ||
| Car car = (Car) savedCar; | ||
| car.setModel("Model X"); | ||
| vehicleService.updateVehicle(car); | ||
|
|
||
|
|
||
| Vehicle updated = vehicleService.getVehicle(car.getVehicleId()); | ||
| assertEquals("Model X", updated.getModel()); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| void deleteVehicle_shouldRemoveFromRepository() { | ||
| // Save a temporary vehicle and delete it | ||
| Vehicle tmp = vehicleService.saveVehicle(new Car("BMW", "M3", 4, 3000)); | ||
| int tmpId = tmp.getVehicleId(); | ||
|
|
||
|
|
||
| vehicleService.deleteVehicle(tmp); | ||
|
|
||
|
|
||
| assertNull(vehicleService.getVehicle(tmpId)); | ||
| } | ||
|
|
||
|
|
||
| // Helper: assert that a specific vehicle id exists in the list | ||
| private void assertTrue_containsId(List<Vehicle> vehicles, int id) { | ||
| boolean found = vehicles.stream().anyMatch(v -> v.getVehicleId() == id); | ||
| if (!found) { | ||
| throw new AssertionError("Expected vehicle with id=" + id + " in list but was not found."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guard teardown against nulls: saved* references may be null if setup failed, which would cause a NullPointerException during cleanup. Add null checks before deleting.