0

I am trying to write a test to check the method is throwing an IllegalArgumentException. To do that I wrote a simple test code but it gives error.

Method is a constructor by the way.

public Range(double lower, double upper) {
        if (lower > upper) {
            String msg = "Range(double, double): require lower (" + lower 
                + ") <= upper (" + upper + ").";
            throw new IllegalArgumentException(msg);
        }
        this.lower = lower;
        this.upper = upper;
    }

Test code

    @Test(expected = IllegalArgumentException.class)
    public void testConstructor(){
        
        new Range(5,-2);
        
    }

What I got is

Í java.lang.IllegalArgumentException: Range(double, double): require lower (5.0) <= upper (-2.0).

as expected. But why Junit behaves like it is an error, even if I added @Test(expected = IllegalArgumentException.class) before the test?

peterh
  • 11,875
  • 18
  • 85
  • 108
posix
  • 33
  • 6

0 Answers0