SE450: Assertions: Assertions in JDK 1.3 [7/24] Previous pageContentsNext page

So if you are stuck using JDK 1.3, what do you do?

You can use the Assert class provided by JUnit. This class has a number of static methods that you have already used. To use it, you just need to include junit.jar in your classpath and import the class in the file you want to use it in.


import junit.framework.Assert;

public class MyClass2 {


    public static void main(String args[]) {
	Assert.assertTrue( false );
    }
}

Here's what it will look like when you run it.

C:\temp>d:\jdk1.3.1_02\bin\javac -classpath .;d:\junit3.7\junit.jar MyClass2.java

C:\temp>d:\jdk1.3.1_02\bin\java -classpath .;d:\junit3.7\junit.jar MyClass2
Exception in thread "main" junit.framework.AssertionFailedError
        at junit.framework.Assert.fail(Assert.java:51)
        at junit.framework.Assert.assertTrue(Assert.java:38)
        at junit.framework.Assert.assertTrue(Assert.java:45)
        at MyClass2.main(MyClass2.java:7) 

With this option, there are no flags to turn assertions on or off. They are always enabled.

Previous pageContentsNext page