7

我正在使用 Junit 4。我的整个程序运行良好。我正在尝试编写一个测试用例。但是有一个错误...

这是非常基本的样本测试

public class di  extends TestCase{
    private static Records testRec;
    public void testAbc() {
        Assert.assertTrue(
            "There should be some thing.",
            di.testRec.getEmployee() > 0);
    }

}

当我运行它时,它给了我错误

fName can not be null

如果我使用超级并且这样做

public TestA() {
super("testAbc");
}

一切正常。在 JUnit 3.X 之前不是这样我做错了还是他们改变了它:( 抱歉,如果我不清楚

有没有办法在没有超级的情况下执行测试?或调用函数等?

4

2 回答 2

16

在 JUnit 4 中,您不需要 extend TestCase,而是使用@Test注解来标记您的测试方法:

public class MyTest {
    private static Records testRec;
    @Test
    public void testAbc() {
        Assert.assertTrue(
            "There should be some thing.",
            MyTest.testRec.getEmployee() > 0);
    }
}

附带说明一下,测试static班级中的成员可能会使您的单元测试相互依赖,这不是一件好事。除非您有充分的理由,否则我建议您删除static限定符。

于 2010-03-21T16:34:48.357 回答
0

这不是您的情况,但此错误也可能意味着Test.java您的项目中有一个名为的文件。重命名它将修复错误,但重构@Test后将更改为@NewName(至少在 Eclipse 中),因此请记住手动将其更改回@Test.

于 2016-11-29T09:57:39.647 回答