如何在eclipse中运行简单的JUnit测试

1 java eclipse junit unit-testing junit3

所以这是我的代码:

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());
  } 
}
Run Code Online (Sandbox Code Playgroud)

当我尝试运行它时,我收到以下错误消息:

.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
Run Code Online (Sandbox Code Playgroud)

我的代码基于Martin Fowler的第4章"重构:改进现有代码的设计"中给出的例子(尽管我简化了它,但基本结构是相同的).另外,我运行eclipse 3.5.1并使用JUnit 3库(不知道它有多相关,但无论如何......)

你能给我一些关于我做错的提示吗?

Dra*_*kar 7

首先,我想说eclipse中有一个功能,让您无需在测试类中声明main即可运行测试.

这是一个教程,将向您展示如何使用它(使用JUnit4):http://www.vogella.de/articles/JUnit/article.html