reset password
Author Message
ly.c.christopher
Posts: 5
Posted 22:47 Jan 26, 2014 |

How would you use a JUnit test for this problem? Unlike the rectangle example posted by RABBOT this problem has much more calculations. 

rectangle example http://csns.calstatela.edu/department/cs/forum/topic/view?id=4618727

rabbott
Posts: 1649
Posted 23:42 Jan 26, 2014 |

Look at the problem again. Construct a JUnit test for the method that gives you the costOfOwnership over a period of time.  You need not (and should not) design a JUnit test for the entire input-output program. JUnit tests are intended primarily to test individual methods.

ly.c.christopher
Posts: 5
Posted 23:55 Jan 26, 2014 |

When I test the method i get this.

Warning from last compilation 

assertEquals(double,double) in org.junit.Assert has been deprecated

This is my test method : 

@Test 
    public void test()
    {
        Hybrid general = new Hybrid(15000, 5);
        assertEquals(general.ownershipCost(25000,50,15000),11500);
    }

This is the method I'm testing:

public double ownershipCost(double carCost,double mpg , double resaleValue)
    {
        double gallonsUsed = milesPerYear/mpg;         //total amount of gas used in that year
        double costToDrive = gallonsUsed * gasPrices;  //the amount of money used for gas ONLY

        double totalCarGas = carCost + costToDrive;    //totals the cost of the car and gas spent 
        double total = totalCarGas - resaleValue;      //gets the amount spent on the car
        return total;
    }

constructor:

private double gasPrices;
    private int milesPerYear;
    
    /**
     * Constructor for objects of class Hybrid
     */
    public Hybrid(int a, double b)
    {
        milesPerYear = a;
        gasPrices = b;
}

Last edited by ly.c.christopher at 23:57 Jan 26, 2014.
rabbott
Posts: 1649
Posted 00:15 Jan 27, 2014 |

assert Equality( ) needs a third parameter when you are testing doubles. The 3rd parameter is the margin of error.

assertEquality(<expected value>, <expression to be tested>, <margin of error>);

For this problem 0.001 should be fine as the margin of error.

Last edited by rabbott at 09:22 Jan 27, 2014.
ly.c.christopher
Posts: 5
Posted 00:18 Jan 27, 2014 |

Thank you!