How to run a simple JUnit test in eclipse
So here's my code:
package tests;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
class StatementTester extends TestCase {
public Statemen开发者_开发百科tTester (String name) {
super(name);
}
protected void setUp() {
}
protected void tearDown() {
}
public void test1() {
System.out.println("testing");
assert(true);
}
public static Test suite() {
TestSuite suite= new TestSuite();
suite.addTest(new StatementTester("test1"));
return suite;
}
public static void main (String[] args) {
junit.textui.TestRunner.run (suite());
}
}
When I try to run this I get the following error message:
.E
Time: 0.001
There was 1 error:
1) test1(tests.StatementTester) at tests.StatementTester.main(StatementTester.java:30)
FAILURES!!!
Tests run: 1, Failures: 0, Errors: 1
I based my code on the example given in Chapter 4 of Martin Fowler's "Refactoring: Improving the design of existing code" (although I simplified it, but the base structure is the same). Also, I run eclipse 3.5.1 and use the JUnit 3 library (don't know how relevant it is, but anyway...)
Can you give me any hints on what I'm doing wrong?
First of all I'd like to say that there is a feature in eclipse that let you run the test without having to declare a main in your test class.
Here is a tutorial that will show you how to do it (with JUnit4) : http://www.vogella.de/articles/JUnit/article.html
With your file currently in the editor, ALT-SHIFT-X, T. Or right click->Run As->JUnit Test.
精彩评论