O.D
O.D

Reputation: 49

Unit testing using Junit without arguments

I have a very big method that requires unit testing, so I had to refactor the code to make it as simple and as readable as possible, I did that however, the last step should be to perform unit testing on different methods, so my question is how to perform unit testing for a method that doesn't have arguments, I have updated my question to include arguments and that is the unit test I performed but it's not correct at all, Your assistance is appreciated.

Customer.java

public class Customer {
private String customerName;
private Vector<Rental> customerRentals = new Vector<Rental>();
double thisAmount;
Rental eachRental;
int rewardPoints;

public Customer(String customerName) {
    this.customerName = customerName;
}

public void addRental(Rental rental) {
    customerRentals.addElement(rental);
}

public String getCustomerName() {
    return customerName;
}

public String statement() {
    double totalAmount = 0;
    rewardPoints = 0;
    Enumeration<Rental> rentals = customerRentals.elements();
    String result = "Rental Record for:" + getCustomerName() + "\n";
    while (rentals.hasMoreElements()) {
        thisAmount = 0;
        eachRental = (Rental) rentals.nextElement();

        thisAmount=50.0;

        switch (eachRental.getVehicle().getRateCode()) {

            case Vehicle.SEDAN:
                thisAmount += 100*eachRental.getDaysRented();
                if (eachRental.getMileage() > eachRental.getDaysRented()*50)
                {
                    thisAmount += (eachRental.getMileage() - 
                    eachRental.getDaysRented()*50) * 2;
                }
                break;

            case Vehicle.FOURxFOUR:
                thisAmount += 200*eachRental.getDaysRented();
                break;

            case Vehicle.SUV:
                thisAmount += 150*eachRental.getDaysRented();
                if (eachRental.getMileage() > eachRental.getDaysRented()*70)
                    thisAmount += (eachRental.getMileage() - 
                    eachRental.getDaysRented()*70) * 2;
                break;
            default:
                    thisAmount+=0;
        }

        getTotalMilage(eachRental);

        isLate();

        result += "\t\"" + eachRental.getVehicle().getMakeAndModel() + 
                  "\"\tLE " +
                String.valueOf(thisAmount) + "\n";

        totalAmount+=thisAmount;

    }

    result += "Amount owed is LE " + String.valueOf(totalAmount) + "\n";

    result += "You earned: " + String.valueOf(rewardPoints) +
            " new Reward Points\n\n";
    return result;
}
public double getTotalMilage(Rental rentalMilage)
{
 if (rentalMilage.getMileage() > 200)
    {
        if (rentalMilage.getDaysRented() > 10 && 
            rentalMilage.getVehicle().getRateCode()== Vehicle.FOURxFOUR)
        {
            return thisAmount-=thisAmount*0.05;
        }
        else if (rentalMilage.getVehicle().getRateCode() == Vehicle.SUV)
        {
            System.out.println(eachRental);
            return thisAmount-=thisAmount*0.05;
        }
    }

 return thisAmount;
}

public boolean isLate(){
 if (!eachRental.isLate()) 
 {
        rewardPoints++;

        if ((eachRental.getVehicle().getRateCode() == Vehicle.FOURxFOUR)) {
            rewardPoints *= 2;
        }
 }

        if ((eachRental.getVehicle().getRateCode() == Vehicle.SUV) && 
             eachRental.getDaysRented() > 5) {
            rewardPoints += (eachRental.getDaysRented() - 5);
        }

 else
    {
        thisAmount+=thisAmount*0.03;
    }
        return true;
    }
}

CustomerTest.java

public class BillingTests {
Vehicle vehicle = new Vehicle("Renault", Vehicle.SEDAN);
Rental rental = new Rental(vehicle, 100, 100, false);
@Test
public void StatementCheck() {
    Customer customerBilling = new Customer("Virgin Gates");
    rental = new Rental(vehicle,rental.getMileage(), rental.getDaysRented(), 
                        rental.isLate());
    double expected = customerBilling.getTotalMilage(rental);
    assertEquals(102.1, expected, 10.0);
}

}

Upvotes: 1

Views: 448

Answers (1)

Iker Obregon Reigosa
Iker Obregon Reigosa

Reputation: 243

In that case, eachRental should be an argument to the function.

Other option (not so good in my opinion), is changing the customer object's values to the ones needed for an scenario an after that calling getTotalMilage and assert that thisAmount is what it should be for that scenario.

Upvotes: 1

Related Questions