-3

Below is the code I want to test

  public class EPATestMode {

  public static final int INVALID_MPG = -1; 

  private int odometerAtReset;


  public EPATestMode() {
    odometerAtReset = 0;
  }


  public void resetReadings(int milesDriven) {
    // Use the current mileage as the new baseline
    odometerAtReset = milesDriven;
  }


  public double mpg(int currentOdometer, int gallonsBurnt) {
    if (gallonsBurnt == 0) {
      return INVALID_MPG;
    } else {

      int milesDriven = currentOdometer - odometerAtReset;

      return milesDriven / gallonsBurnt;
    }
  }
}

This is my first testcase I want to do , on the instance variable INvalid MPG but when I do this , there is a line crossing out "assertEquals". Very confused about this.(Also new to JUnit testing)

@Test
public void testInvalidMpg() {
    EPATestMode MpgTest = new EPATestMode();
    double results=MpgTest.INVALID_MPG;
    assertEquals(results,-1)

}
Kevin Welker
  • 7,719
  • 1
  • 40
  • 56
Clyde
  • 85
  • 6

2 Answers2

1

You don't state your environment, but most likely it behaves similar to Eclipse (maybe it is Eclipse?) in that a line through the code is an indication that you're using a deprecated API. In this case, if you go to the Junit API documentation, you'll see that assertEquals for doubles like you are calling is deprecated in favor of one that includes a delta. API

The idea is that floating point numbers are inherently inexact, and so comparing them is inexact at best. You need to also include a delta so that you can indicate how far apart the numbers can be and still be acceptable.

So basically you want:

assertEquals(results, -1, .000001); // For example.

On a side note, I understand that you're just trying to wrap your head around this - and consequently you're probably trying to come up with a simple test just to get "something working". But tests like that - where you compare a class constant to see if it's what you input - aren't particularly useful. I would be more inclined to test to make sure that supplying 0 as your "gallons burnt" returns the proper "INVALID_MPG" constant.

Chris Parker
  • 416
  • 2
  • 9
0

Line crossing out means the method is deprecated. http://junit.sourceforge.net/javadoc/org/junit/Assert.html#assertEquals(double, double)

The new method to use is below

public static void assertEquals(double expected, double actual, double delta)

The delta is how much difference the actual and expected can have.

Ashraff Ali Wahab
  • 1,088
  • 9
  • 19