Hoang, I think you are a little confused between the statements of the Java language and the JUnit claims.
The assert keyword in Java was added in 1.4 and is intended to check for internal consistency in a class. Best practice recommendations were to use them in private methods to test program invariants. When an assertion fails, a java.lang.AssertionError is called and is generally not intended to be caught. The idea was that they could be turned on during debugging and turned off in production code (which is the default), but to be honest, I don’t think they were ever really caught. I have not seen them use a lot.
JUnit also claims as many different static methods in the org.junit.Assert package. They are intended to verify the results of this test. These statements also throw java.lang.AssertionError, but the JUnit framework is configured to catch and record these errors and generate a report of all failures at the end of the test run. This is a more common use of IMO statements.
Obviously, you can use one of them, but JUnit's statements are much more expressive, and you do not need to worry about enabling or disabling them. On the other hand, they are not intended for use in business code.
EDIT: Here is a sample code that works:
import org.junit.Assert; import org.junit.Test; public class MyTest { @Test public void test() { Object alo = null; assert alo != null
Here, the output from the launch with Java claims is disabled (by 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) ...
This runs with Java statements (-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) ...
Note that in the first example, the Java assert passes, and in the second, the failure.
source share