11import static org .junit .jupiter .api .Assertions .assertEquals ;
22import static org .junit .jupiter .api .Assertions .assertNotNull ;
3+ import static org .junit .jupiter .api .Assertions .assertThrows ;
34
4- import com .iluwatar .Car ;
5- import com .iluwatar .Truck ;
6- import com .iluwatar .Vehicle ;
7- import com .iluwatar .VehicleDatabase ;
5+ import com .iluwatar .table . inheritance . Car ;
6+ import com .iluwatar .table . inheritance . Truck ;
7+ import com .iluwatar .table . inheritance . Vehicle ;
8+ import com .iluwatar .table . inheritance . VehicleDatabase ;
89import org .junit .jupiter .api .BeforeEach ;
910import org .junit .jupiter .api .Test ;
1011
1112/**
1213 * Unit tests for the {@link VehicleDatabase} class.
1314 * Tests saving, retrieving, and printing vehicles of different types.
1415 */
15- public class VehicleDatabaseTest {
16+ class VehicleDatabaseTest {
1617
1718 private VehicleDatabase vehicleDatabase ;
1819
@@ -28,7 +29,7 @@ public void setUp() {
2829 * Tests saving a {@link Car} to the database and retrieving it.
2930 */
3031 @ Test
31- public void testSaveAndRetrieveCar () {
32+ void testSaveAndRetrieveCar () {
3233 Car car = new Car (2020 , "Toyota" , "Corolla" , 4 , 1 );
3334 vehicleDatabase .saveVehicle (car );
3435
@@ -48,7 +49,7 @@ public void testSaveAndRetrieveCar() {
4849 * Tests saving a {@link Truck} to the database and retrieving it.
4950 */
5051 @ Test
51- public void testSaveAndRetrieveTruck () {
52+ void testSaveAndRetrieveTruck () {
5253 Truck truck = new Truck (2018 , "Ford" , "F-150" , 60 , 2 );
5354 vehicleDatabase .saveVehicle (truck );
5455
@@ -68,7 +69,7 @@ public void testSaveAndRetrieveTruck() {
6869 * Tests saving multiple vehicles to the database and printing them.
6970 */
7071 @ Test
71- public void testPrintAllVehicles () {
72+ void testPrintAllVehicles () {
7273 Car car = new Car (2020 , "Toyota" , "Corolla" , 4 , 1 );
7374 Truck truck = new Truck (2018 , "Ford" , "F-150" , 60 , 2 );
7475 vehicleDatabase .saveVehicle (car );
@@ -82,4 +83,87 @@ public void testPrintAllVehicles() {
8283 assertNotNull (retrievedCar );
8384 assertNotNull (retrievedTruck );
8485 }
86+
87+ /**
88+ * Tests the constructor of {@link Car} with valid values.
89+ */
90+ @ Test
91+ void testCarConstructor () {
92+ Car car = new Car (2020 , "Toyota" , "Corolla" , 4 , 1 );
93+ assertEquals (2020 , car .getYear ());
94+ assertEquals ("Toyota" , car .getMake ());
95+ assertEquals ("Corolla" , car .getModel ());
96+ assertEquals (4 , car .getNumDoors ());
97+ assertEquals (1 , car .getId ()); // Assuming the ID is auto-generated in the constructor
98+ }
99+
100+ /**
101+ * Tests the constructor of {@link Car} with invalid number of doors (negative value).
102+ */
103+ @ Test
104+ void testCarConstructorWithInvalidNumDoors () {
105+ IllegalArgumentException exception = assertThrows (IllegalArgumentException .class , () -> {
106+ new Car (2020 , "Toyota" , "Corolla" , -4 , 1 );
107+ });
108+ assertEquals ("Number of doors must be positive." , exception .getMessage ());
109+ }
110+
111+ /**
112+ * Tests the constructor of {@link Car} with zero doors.
113+ */
114+ @ Test
115+ void testCarConstructorWithZeroDoors () {
116+ IllegalArgumentException exception = assertThrows (IllegalArgumentException .class , () -> {
117+ new Car (2020 , "Toyota" , "Corolla" , 0 , 1 );
118+ });
119+ assertEquals ("Number of doors must be positive." , exception .getMessage ());
120+ }
121+
122+ /**
123+ * Tests the constructor of {@link Truck} with invalid load capacity (negative value).
124+ */
125+ @ Test
126+ void testTruckConstructorWithInvalidLoadCapacity () {
127+ IllegalArgumentException exception = assertThrows (IllegalArgumentException .class , () -> {
128+ new Truck (2018 , "Ford" , "F-150" , -60 , 2 );
129+ });
130+ assertEquals ("Load capacity must be positive." , exception .getMessage ());
131+ }
132+
133+ /**
134+ * Tests the constructor of {@link Truck} with zero load capacity.
135+ */
136+ @ Test
137+ void testTruckConstructorWithZeroLoadCapacity () {
138+ IllegalArgumentException exception = assertThrows (IllegalArgumentException .class , () -> {
139+ new Truck (2018 , "Ford" , "F-150" , 0 , 2 );
140+ });
141+ assertEquals ("Load capacity must be positive." , exception .getMessage ());
142+ }
143+
144+ /**
145+ * Tests setting invalid number of doors in {@link Car} using setter (negative value).
146+ */
147+ @ Test
148+ void testSetInvalidNumDoors () {
149+ Car car = new Car (2020 , "Toyota" , "Corolla" , 4 , 1 );
150+ IllegalArgumentException exception = assertThrows (IllegalArgumentException .class , () -> {
151+ car .setNumDoors (-2 );
152+ });
153+ assertEquals ("Number of doors must be positive." , exception .getMessage ());
154+ }
155+
156+ /**
157+ * Tests setting invalid load capacity in {@link Truck} using setter (negative value).
158+ */
159+ @ Test
160+ void testSetInvalidLoadCapacity () {
161+ Truck truck = new Truck (2018 , "Ford" , "F-150" , 60 , 2 );
162+ IllegalArgumentException exception = assertThrows (IllegalArgumentException .class , () -> {
163+ truck .setLoadCapacity (-10 );
164+ });
165+ assertEquals ("Load capacity must be positive." , exception .getMessage ());
166+ }
85167}
168+
169+
0 commit comments