如何在 eclipse 中运行简单的 JUnit 测试
这是我的代码:
package tests;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
class StatementTester extends TestCase {
public StatementTester (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());
}
}
当我尝试运行此代码时,我收到以下错误消息:
.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
我的代码基于 Martin Fowler 的“重构:改进现有代码的设计”第 4 章中给出的示例(虽然我简化了它,但是基本结构相同)。另外,我运行 eclipse 3.5.1 并使用 JUnit 3 库(不知道它有多相关,但无论如何......)
你能给我一些关于我做错了什么的提示吗?
So here's my code:
package tests;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
class StatementTester extends TestCase {
public StatementTester (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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先我想说的是,eclipse中有一个功能可以让你运行测试而不必在测试类中声明main。
这是一个教程,将向您展示如何做到这一点(使用 JUnit4):
http://www.vogella.de/articles/JUnit/article.html
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
当前文件位于编辑器中时,ALT-SHIFT-X、T。或者右键->运行方式->JUnit Test。
With your file currently in the editor, ALT-SHIFT-X, T. Or right click->Run As->JUnit Test.