1

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);
}

}

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I guess using eachRental as an argument sounds better, however to be honest I'm a beginner in unit testing, just to be sure does that look even close to testing this method? Customer customerBilling = new Customer("Virgin Gates"); Rental rental = new Rental(vehicle, 200, 200, true); customerBilling.getTotalMilage(rental); assertEquals(102, 12);
I think soo. First you arrange the state in which you want to test your unit. When it is arranged, you execute your function. After executing your test, you assert that the result is what it should be.
I apologize for the too many questions but for some reason I'm not able to create the unit test at all, can you please check out the code that I send in the previous comment and correct me?
Put the test int he question and add more information about Customer.class.
You are creating a rental with a Millage of 100 and in the method you are doing things if Millage is bigger than 200. You are initializating thisAmount only in statement() so, when you try to do return thisAmount-=thisAmount*0.05; is going to give an exception. Those are the main problems I can see without running the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.