DEV Community

Sardar Mudassar Ali Khan
Sardar Mudassar Ali Khan

Posted on

Advanced Software Testing with JUnit with Automation

JUnit Testing with JUnit Test Cases for Car Rental Management System in Java

Software testing is a critical part of the software development process, ensuring that your application functions correctly and reliably. In Java, JUnit is one of the most popular testing frameworks used for unit testing. In this article, we will explore how to use JUnit to create test cases for a Car Rental Management System in Java.

Introduction to JUnit

JUnit is a testing framework for Java that simplifies the process of creating and running tests. It provides annotations and assertions to define test methods and check whether they produce the expected results. JUnit is widely used for unit testing to ensure that individual components or units of code work as expected.

Setting Up the Environment

Before you begin writing JUnit test cases for your Car Rental Management System, make sure you have the following components set up:

  1. Java Development Kit (JDK): You need to have Java installed on your system. JUnit is a Java-based framework, and you'll be writing your test cases in Java.

  2. JUnit Library: Download and add the JUnit library to your project. You can do this using a build tool like Maven or Gradle, or you can manually add the JUnit JAR files to your project.

The Car Rental Management System

For this article, we will create a simple Car Rental Management System with two Java classes: Car and CarRentalSystem. The Car class represents individual cars, and the CarRentalSystem class manages the rental of these cars.

Car Class

public class Car {
    private String make;
    private String model;
    private boolean isRented;

    public Car(String make, String model) {
        this.make = make;
        this.model = model;
        this.isRented = false;
    }

    // Getter and setter methods
}
Enter fullscreen mode Exit fullscreen mode

CarRentalSystem Class

import java.util.ArrayList;
import java.util.List;

public class CarRentalSystem {
    private List<Car> cars;

    public CarRentalSystem() {
        this.cars = new ArrayList<>();
    }

    public void addCar(Car car) {
        cars.add(car);
    }

    public boolean rentCar(Car car) {
        if (!car.isRented) {
            car.isRented = true;
            return true;
        }
        return false;
    }

    public boolean returnCar(Car car) {
        if (car.isRented) {
            car.isRented = false;
            return true;
        }
        return false;
    }
}
Enter fullscreen mode Exit fullscreen mode

Writing JUnit Test Cases

Now, let's write JUnit test cases to ensure that the CarRentalSystem class functions correctly. Create a new Java class for your test cases and annotate it with @RunWith(JUnit4.class).

import junit.framework.TestCase;

public class CarRentalSystemTest extends TestCase {
    private CarRentalSystem rentalSystem;

    public void setUp() {
        rentalSystem = new CarRentalSystem();
    }

    public void testAddCar() {
        Car car = new Car("Toyota", "Camry");
        rentalSystem.addCar(car);
        assertTrue(rentalSystem.getCars().contains(car));
    }

    public void testRentCar() {
        Car car = new Car("Honda", "Civic");
        rentalSystem.addCar(car);
        assertTrue(rentalSystem.rentCar(car));
        assertFalse(rentalSystem.rentCar(car)); // Try to rent the same car again
    }

    public void testReturnCar() {
        Car car = new Car("Ford", "Focus");
        rentalSystem.addCar(car);
        assertTrue(rentalSystem.rentCar(car));
        assertTrue(rentalSystem.returnCar(car));
        assertFalse(car.isRented);
    }

    public void testRentNonExistentCar() {
        Car car = new Car("Nissan", "Altima");
        assertFalse(rentalSystem.rentCar(car));
    }

    public void testReturnNonExistentCar() {
        Car car = new Car("Chevrolet", "Malibu");
        assertFalse(rentalSystem.returnCar(car));
    }

    public void testRentAlreadyRentedCar() {
        Car car = new Car("Mazda", "CX-5");
        rentalSystem.addCar(car);
        assertTrue(rentalSystem.rentCar(car));
        assertFalse(rentalSystem.rentCar(car)); // Try to rent the same car again
    }

    public void testReturnAlreadyReturnedCar() {
        Car car = new Car("Subaru", "Outback");
        rentalSystem.addCar(car);
        assertTrue(rentalSystem.returnCar(car));
        assertFalse(rentalSystem.returnCar(car)); // Try to return the same car again
    }

    public void testAddMultipleCars() {
        Car car1 = new Car("Kia", "Soul");
        Car car2 = new Car("Hyundai", "Elantra");
        rentalSystem.addCar(car1);
        rentalSystem.addCar(car2);
        assertTrue(rentalSystem.getCars().contains(car1));
        assertTrue(rentalSystem.getCars().contains(car2));
    }

    public void testRentMultipleCars() {
        Car car1 = new Car("Volkswagen", "Jetta");
        Car car2 = new Car("Jeep", "Cherokee");
        rentalSystem.addCar(car1);
        rentalSystem.addCar(car2);
        assertTrue(rentalSystem.rentCar(car1));
        assertTrue(rentalSystem.rentCar(car2));
    }

    public void testReturnMultipleCars() {
        Car car1 = new Car("Ford", "Escape");
        Car car2 = new Car("Toyota", "Corolla");
        rentalSystem.addCar(car1);
        rentalSystem.addCar(car2);
        assertTrue(rentalSystem.rentCar(car1));
        assertTrue(rentalSystem.rentCar(car2));
        assertTrue(rentalSystem.returnCar(car1));
        assertTrue(rentalSystem.returnCar(car2));
    }

    public void testRentAfterReturning() {
        Car car = new Car("Nissan", "Rogue");
        rentalSystem.addCar(car);
        assertTrue(rentalSystem.rentCar(car));
        assertTrue(rentalSystem.returnCar(car));
        assertTrue(rentalSystem.rentCar(car)); // Try to rent the car after returning
    }

    public void testReturnUnrentedCar() {
        Car car = new Car("Chevrolet", "Equinox");
        rentalSystem.addCar(car);
        assertFalse(rentalSystem.returnCar(car)); // Try to return an unrented car
    }

    public void testRentAndReturnMixed() {
        Car car1 = new Car("Audi", "A4");
        Car car2 = new Car("Mercedes-Benz", "C-Class");
        rentalSystem.addCar(car1);
        rentalSystem.addCar(car2);
        assertTrue(rentalSystem.rentCar(car1));
        assertTrue(rentalSystem.returnCar(car1));
        assertTrue(rentalSystem.rentCar(car2));
    }

    public void testAddAndRemoveCars() {
        Car car1 = new Car("Lexus", "RX");
        Car car2 = new Car("BMW", "3 Series");
        rentalSystem.addCar(car1);
        rentalSystem.addCar(car2);
        rentalSystem.getCars().remove(car1);
        assertFalse(rentalSystem.getCars().contains(car1));
        assertTrue(rentalSystem.getCars().contains(car2));
    }
}

Enter fullscreen mode Exit fullscreen mode

In the code above, we have defined three test cases using the @Test annotation. The @Before annotation is used to set up the test environment before each test case is executed. In this case, we create a new CarRentalSystem instance for each test case.

Running the Test Cases

To run the test cases, you can use an Integrated Development Environment (IDE) like IntelliJ IDEA or Eclipse, or you can run them from the command line using a build tool like Maven or Gradle. Most modern IDEs have built-in support for JUnit testing, making it easy to run and analyze the results of your tests.

When you run the tests, JUnit will execute each test method and report the results. If all the assertions within your test cases pass, you will see a green bar, indicating that your code is working as expected. If any assertions fail, you will see a red bar, and JUnit will provide information about which test case failed and why.

Conclusion

JUnit is a powerful testing framework that simplifies the process of writing and running tests in Java. By creating comprehensive test cases for your Car Rental Management System, you can ensure that it behaves as expected and that any future code changes do not introduce unexpected issues.

Top comments (0)