2015-11-19 47 views
0

我創建了一個測試套件,並希望增加一些新的考驗,但suite.addTest()方法似乎如我所料不工作:Junit3 suite.addTest(新mytest的())獲得「空」的錯誤

public static Test suite() { 
    TestSuite suite = new TestSuite(AllTests.class.getName()); 
    //$JUnit-BEGIN$ 
    // This line works 
    suite.addTestSuite(TestAdd.class); 
    // This line will cause a 'null' failure 
    suite.addTest(new TestAdd()); 
    //$JUnit-END$ 
    return suite; 
} 

代碼TestAdd的:

import junit.framework.TestCase; 
public class TestAdd extends TestCase { 
    public void test1() 
    { 
     assertEquals(1, 1); 
    } 
} 

難道我錯過了什麼?

+0

請發表您的TestAdd代碼。它可能是導致問題的構造函數。你嘗試過使用調試器嗎? – EkcenierK

+0

嗨,我用TestAdd類更新了這個問題。這是一個微不足道的。 – Zippon

+0

你也可以發佈你得到的完整錯誤和堆棧跟蹤嗎? – EkcenierK

回答

1

從的Javadoc:

TestCase()

No-arg constructor to enable serialization. This method is not intended to be used by mere mortals without calling setName().

TestCase(java.lang.String name)

Constructs a test case with the given name.

看樣子你必須使用TestCase的構造函數傳遞測試的名稱字符串。使用無參數構造函數僅用於啓用測試用例的序列化。

嘗試:

suite.addTest(new TestAdd("add")); 
+0

謝謝。 suite.addTest(new TestAdd(「add」))不起作用,它失敗: [junit.framework.AssertionFailedError:方法「add」未找到] suite.addTest(new TestAdd(「test1」))works 。 字符串參數必須是TestAdd類的方法,但這不是我所期望的... – Zippon

+0

解決方法是添加一個新的構造函數[public TestAdd(String methodName,)。謝謝! – Zippon