Java Junit测试问题
我正在使用 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
如果我使用 super 并这样做,
public TestA() {
super("testAbc");
}
它工作得很好。以前使用 JUnit 3.X 时不是这样的 我做错了吗还是他们改变了:( 抱歉,如果我不清楚,
有没有办法在没有 super 的情况下执行测试?或调用函数等?
I am using Junit 4. My whole program is working fine. I am trying to write a test case. But there is one error...
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
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
Is there any way to executre test without super? or calling functions etc. ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 JUnit 4 中,您不需要扩展
TestCase
,而是使用@Test
注释来标记您的测试方法:作为旁注,测试
static
你班上的成员可能会让你的单元测试相互依赖,这不是一件好事。除非您有充分的理由,否则我建议删除static
限定符。In JUnit 4 you need not extend
TestCase
, instead use the@Test
annotation to mark your test methods:As a side note, testing a
static
member 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 thestatic
qualifier.这不是您的情况,但此错误也可能意味着您的项目中有一个名为
Test.java
的文件。重命名它可以修复错误,但重构后@Test
将更改为@NewName
(至少在 Eclipse 中),因此请记住手动将其更改回@Test
。This is not your case but this error may also mean that you have a file named
Test.java
in your project. Renaming it will fix the error but after refactoring@Test
will be changed to@NewName
(at least in Eclipse) so remember to manually change it back to@Test
.