7

I'm applying UnitTest just for a while, and today I met something very strange. Considering the following code:

TestObject alo = null; assert alo != null; // Pass!!! Assert.assertNotNull(alo); // Fail, as expected. 

I googled around and find that assert is java-built-in, while assertNotNull is JUnit supported. But I can't understand why assert don't complain anything about the null object?

7
  • 8
    Make sure you have assertions enabled when you run (-ea at the command line). Commented Apr 6, 2012 at 4:22
  • 2
    If you run code in maven (test task) assertion are enabled by default. Commented Apr 6, 2012 at 5:59
  • @KeithRandall and ajozwik: yes, I think I understand the problem now. When I run Unit test by Eclipse JUnit, it seems that it lacks the "-ea" parameter. But when I use maven to build, maven report errors. I even thought that they use different version of jdk. Commented Apr 6, 2012 at 6:54
  • 1
    cis.upenn.edu/~matuszek/cit594-2004/Pages/… Commented Apr 6, 2012 at 7:10
  • 1
    @KeithRandall thanks for mentioning this! Did not have it in when running and I was wondering why it was passing. For others, it's java -ea mainClassName Commented Dec 5, 2015 at 7:29

3 Answers 3

10

Hoang, I think you're getting a little confused between Java language asserts and JUnit asserts.

  • The assert keyword in Java was added in 1.4, and intended to allow to verify internal consistency in a class. The best practice recommendation was to use them in private methods to test program invariants. When an assert fails, a java.lang.AssertionError is thrown and is generally not intended to be caught. The idea was they could be enabled during debugging and disabled in production code (which is the default), but frankly, I don't think they ever really caught on. I haven't seen them used much.

  • JUnit also has asserts, in the form of many different static methods in the org.junit.Assert package. These are intended to verify the results of a given test. These asserts also throw a java.lang.AssertionError, but the JUnit framework is set up to catch and record those errors and to generate a report of all failures at the end of the test run. This is the more common usage of asserts, IMO.

You can obviously use either one, but the JUnit asserts are far more expressive, and you don't have to worry about enabling or disabling them. On the other hand, they aren't really for use in business code.

EDIT: Here's a code example that works:

import org.junit.Assert; import org.junit.Test; public class MyTest { @Test public void test() { Object alo = null; assert alo != null // Line 9 Assert.assertNotNull(alo); // Line 10 } } 

Here's the output from a run with Java assertions disabled (the default):

c:\workspace\test>java -cp bin;lib\junit-4.8.1.jar org.junit.runner.JUnitCore MyTest JUnit version 4.8.1 .E Time: 0.064 There was 1 failure: 1) test(MyTest) java.lang.AssertionError: at org.junit.Assert.fail(Assert.java:91) ... at MyTest.test(MyTest.java:10) ... 

Here's a run with Java assertions enabled (-ea):

c:\workspace\test>java -ea -cp bin;lib\junit-4.8.1.jar org.junit.runner.JUnitCore MyTest JUnit version 4.8.1 .E Time: 0.064 There was 1 failure: 1) test(MyTest) java.lang.AssertionError: at MyTest.test(MyTest.java:9) ... 

Notice in the first example, the Java assert passes, and in the second, it fails.

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

4 Comments

thank you for your insight, but do you have any clue why the assert fail to perform yet? I have written quite a lot unit tests, and it seems lots of work to change every assert statement
I updated my answer with a code sample to show you the difference.
yes, I have tested it myself. Sadly I can't find anyway to enable "-ea" in Eclipse "Run as" command. Maybe I should switch to jUnit...
Piece of cake! Instead of "Run as" use "Run configurations". Click on the Arguments tab and put "-ea" (no quotes) in the "VM arguments" box. Then click Run. That will do it for you.
4

The assert keyword is disabled by default for the Java VM (see http://docs.oracle.com/cd/E19683-01/806-7930/6jgp65ikq/index.html). Different Java tools may or may not be configured to enable assertions by default.

Eclipse currently does NOT enable assertions by default when running JUnit tests. (See the discussion on https://bugs.eclipse.org/bugs/show_bug.cgi?id=45408 - the reason why not is to preserve backwards compatibility).

However, since Eclipse 3.6 there is a simple way in Eclipse to ensure that assertions are enabled by default for JUnit tests. Open JUnit preferences (Windows | Preferences | Java | Junit) and select the option "Add '-ea' to VM arguments when creating a new JUnit launch configuration". Note that this setting won't change existing JUnit launch configurations, for which you will need to manually add the "-ea" setting to the VM argument box for each such launch configuration.

Comments

1

It's a bug

It's a bug inside Eclipse.

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.