Java Junit测试问题

use*_*384 7 java junit unit-testing testcase

我正在使用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);
    }

}
Run Code Online (Sandbox Code Playgroud)

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

fName can not be null
Run Code Online (Sandbox Code Playgroud)

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

public TestA() {
super("testAbc");
}
Run Code Online (Sandbox Code Playgroud)

它工作得很好.以前不是用JUnit 3.X我做错了或者他们改了:(抱歉,如果我不清楚的话

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

Pét*_*rök 16

在JUnit 4中,您无需扩展TestCase,而是使用@Test注释来标记您的测试方法:

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

作为旁注,测试static班级成员可能会使您的单元测试相互依赖,这不是一件好事.除非你有充分的理由,否则我建议删除static限定符.

  • 听起来你导入了一些不是注释的Test类.确保导入`org.junit.Test`,这应解决问题. (4认同)