Java Junit 测试问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2487789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:26:43  来源:igfitidea点击:

Java Junit testing problem

javaunit-testingjunittestcase

提问by user238384

I am using Junit 4. My whole program is working fine. I am trying to write a test case. But there is one error...

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

here is very basic sample test

这是非常基本的样本测试

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

}

and when i run this it give me error that

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

fName can not be null

if i use super and do like this

如果我使用 super 并且这样做

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

it work all fine. It wasn't this before with JUnit 3.X am I doing wrong or they changed it :( Sorry if I am not clear

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

Is there any way to executre test without super? or calling functions etc. ?

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

回答by Péter T?r?k

In JUnit 4 you need not extend TestCase, instead use the @Testannotation to mark your test methods:

在 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);
    }
}

As a side note, testing a staticmember in your class may make your unit tests dependent on each other, which is not a good thing. Unless you have a very good reason for this, I would recommend removing the staticqualifier.

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

回答by user1

This is not your case but this error may also mean that you have a file named Test.javain your project. Renaming it will fix the error but after refactoring @Testwill be changed to @NewName(at least in Eclipse) so remember to manually change it back to @Test.

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