7

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. ?

user238384
  • 2,396
  • 10
  • 35
  • 36
  • what is TestAgnes? It isn't mentioned in the first code snippet. Please clear your question. – Bozho Mar 21 '10 at 16:36

2 Answers2

16

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

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 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 the static qualifier.

Péter Török
  • 114,404
  • 31
  • 268
  • 329
  • 1
    thanks for your reply. When I tried this, it gave me error "Type mismatch: cannot convert from Test to Annotation" what should I to avoid this error? It gives me if I used @Test – user238384 Mar 21 '10 at 18:02
  • 4
    Sounds like you imported some other Test class which is not an annotation. Make sure you import `org.junit.Test`, that should resolve the issue. – Péter Török Mar 21 '10 at 19:14
  • 1
    I want to make an additional note, if you are getting the "Type mismatch" error above, you might be doing what I did and named your test class "Test" which conflicts with the import. – Niko Jan 08 '14 at 00:17
0

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.

user1
  • 945
  • 2
  • 13
  • 37