Friday, February 17, 2006

JUnit4 w/ Eclipse

I gave JUnit4 a spin with Eclipse. JUnit4 allows you to create tests simply by tagging a method with @Test. You don't have to create a subclass of TestCase. I was able to get this code to run as a test case on Eclipse:









import static junit.framework.Assert.assertTrue;
import junit.framework.JUnit4TestAdapter;
public class TestTest
@org.junit.Test public void method() {
System.out.println("success!");
assertTrue(true);
}

public static junit.framework.Test suite() {
return new JUnit4TestAdapter(TestTest.class);
}
}







It didn't work right out of the box for me. I did a bunch of tweaks. I don't know which ones made it work and which ones didn't. Maybe you can tell me. ;-) Below are the stuff I did.
- I changed the "JUNIT_HOME" variable in Eclipse (Window --> Preferences --> Java --> Build Path --> Classpath Variable) to point to the location of JUnit4
- I downloaded and installed a patch found here.
- In my "Run" configuration, made sure that the JUnit4 jar was in the classpath.
Let me know if any of ya'll know a proper howto on setting up JUnit4 w/ Eclipse 3.1. Thanks!

UPDATE: The JUnit4 patch won't work with Eclipse 3.1. It's for Eclipse 3.2. Read this post.

No comments:

Post a Comment