Angelo.Hannes
Angelo.Hannes

Reputation: 1729

Does the assert statement swallows exceptions?

I have the following test:

@Test
public void testValues() {
    User user = new User();

//  user.getCode().equals("CODE");
    assert user.getCode().equals("CODE");
}

Expected behavior for this test would be to fail, because getCode was not yet initialized and therefore evaluates to null. I would expect a NullPointerException to be thrown. But that does not happen, but instead the test passes.

Uncommenting the line, the test fails as expected with a NullPointerException.

So is here my conception false, or is something going wrong?

I run the test with the eclipse JUnit plug-in. I also have the -ea option enabled under Windows → Preferences → Java → JUnit, so assertions should be enabled.

Upvotes: 0

Views: 405

Answers (4)

NoDataFound
NoDataFound

Reputation: 11959

Don't use assert for Junit test. Use assertEquals and such.

  1. Java assertion are only enabled when using the -ea java option.
  2. Junit assertions will throw you a nice message for certain type (like assertEquals("Foobar", "f00bar");)
  3. assert will ignore exception if not enabled because the associated code will not be executed (ignored), but you'll have to "throw" any checked exception.

Upvotes: 0

alexander zak
alexander zak

Reputation: 939

No, assert does not swallow exceptions. How do you run the tests? Perhaps assertions are disabled in you r run. I ran your test on Intellij Idea and received an exception.

I suggest you use org.easytesting.fest-assert. This allows you to write assertions like this:

    assertThat(user.getCode()).isEqualTo("CODE");

or more complex stuff like:

    assertThat(user.getCode()).containsIgnoringCase("CODE");

This way when the assertion fails you also get a more detailed exception.

Upvotes: 2

T.J. Crowder
T.J. Crowder

Reputation: 1074268

I suspect you've forgotten to enable assertions at runtime (the -ea option to the java tool, or somewhere in your runtime config if running through an IDE). If assertions are not enabled, the entire expression is completely ignroed (not executed), so you don't get an NPE.

For example, with this code:

class Temp {
    public static void main(String args[]) throws Exception{
        System.out.println("Testing");
        (new Temp()).testValues(null);
        System.out.println("Done");
    }
    public void testValues(String str) {

        assert str.equals("CODE");
    }
}

If I run it without assertions enabled, naturally no exception occurs because str.equals("CODE") is never evaluated.

But if I run it with assertions enabled, I get an NPE as expected.

Upvotes: 2

E_net4
E_net4

Reputation: 29983

When making JUnit tests, you are expected to use JUnit assertions. The built-in assert is a different thing. Try something like this to see what happens.

@Test
public void testValues() {
    User user = new User();

    Assert.assertEquals(user.getCode(), "CODE");
}

You will see that the test fails. Your previous code wasn't throwing, most likely because the JVM didn't have assertions enabled (the -ea flag). By default, the Java Virtual Machine will ignore all asserts. That is something you can test as well: running the same code you have in your question with -ea.

Upvotes: 3

Related Questions