0

I created the following method that I use to validate Roman numbers converter. In JUnit the test passes but the program doesn't throw any exception. Where did I wrong?

public void validateState(String number){
    if(!number.matches("^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$"))
        throw new IllegalArgumentException("Invalid number");

    System.out.println("Invalid number");
}

Test:

@Test(expected = IllegalArgumentException.class)
public void test15() throws Exception {
    new RomanNumber("").validateState("MMMMM");
}

Thank you for the help.

4
  • Use a Debugger to find it out Commented Aug 2, 2016 at 11:08
  • It is probably in your REGEX, check you regex for validity Commented Aug 2, 2016 at 11:08
  • Small suggestion, a variable name "number" which is a string could lead to potential confusion. Commented Aug 2, 2016 at 11:09
  • 3
    The test is behaving as expected: if the exception gets thrown, the test passes. demo Commented Aug 2, 2016 at 11:12

3 Answers 3

5

If the test passes, it would mean that the IllegalArgumentException is thrown in your method.
Otherwise, the JUnit test would fail because here you do an assertion that the IllegalArgumentException exception is thrown:

@Test(expected = IllegalArgumentException.class)

Remove @Test(expected = IllegalArgumentException.class) and you would see that the test fails.


Edit to answer to your comment : why I don't see "Invalid number" message

You see the exception with a stacktrace in the output (terminal) if the exception is not caught by any caller object . But as I have just explained in my comment, in your case, you don't see the exception because it is trapped by JUnit (which is a caller object) as you used the expected attribute in the @Test annotation. When you run your method, the JUnit runner intercepts your thrown IllegalArgumentException because the JUnit runner know that it must check that an IllegalArgumentException was thrown.
The JVM writes the exception in the output (the error flow in this case) only if the exception goes back until the top of the call stack( it means that no one in the executed code has caught the exception)

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

12 Comments

is it normal that I don't see the message "Invalid number"?
@user1526278 How could you see something that is written after the statement that throws exception and terminates execution? The sysout you have there should be rather "valid number"
@user1526278 That depends on how you run tests. If you are using eclipse you should see "invalid number" by clicking on the green progressbar near the test in the junit tab. Of course the displayed text would be the exception message and stack trace, not the System.out.println after the exception gets thrown.
@user1526278 Also, don't forget to use braces. In this case, your print statement is outside the if statement. You wouldn't have been able to even compile that if it was inside.
You should add braces around the two instructions and reverse the order of the two instructions in order to see the message : { System.out.println("Invalid number"); throw new IllegalArgumentException("Invalid number"); }
|
0

The test must pass, your logic is correct. Your test says it is expecting to throw an exception and it really does. Junit catches the exception, validates it and makes the test pass that's it.

expected = IllegalArgumentException.class 

Remove this line from your @Test annotation and it will start throwing exception.

4 Comments

If I delete @Test(expected = IllegalArgumentException.class) the test runs on the others test cases and the compiler jumps what interest to me.
No, don't delete the whole annotation, just "expected = IllegalArgumentException.class " if you want to see errors !
but my test code is correct? @Test(expected = IllegalArgumentException.class) public void test15() throws Exception { new RomanNumber("").validateState("MMMMM"); }
@ Adelin: of course. But my doubt is why I don't see the message which the IllegalArgumentException sends? Only this one. There is an error in the test code?
0

You seem to be asking why

public void validateState(String number) {
    if (!number.matches(...))
        throw new IllegalArgumentException("Invalid number");

    System.out.println("Invalid number");
}

does not throw an exception AND print the error message at the same time.

The reason is that when the exception is thrown, it will propagate skipping statements until the exception is caught in an enclosing try ... catch. In this case, the println call will be skipped. This is normal Java behavior ...

If you want the exception to be thrown and the message to be printed, write it like this:

public void validateState(String number) {
    if (!number.matches(...)) {
        System.out.println("Invalid number");
        throw new IllegalArgumentException("Invalid number");
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.