Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Junit testing problem

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

like image 441
user238384 Avatar asked Mar 21 '10 16:03

user238384


People also ask

Why is my JUnit test failing?

When writing unit tests with JUnit, there will likely be situations when tests fail. One possibility is that our code does not meet its test criteria. That means one or more test cases fail due to assertions not being fulfilled.

How do I run a JUnit test in Java?

Create Test Runner Class It imports the JUnitCore class and uses the runClasses() method that takes the test class name as its parameter. Compile the Test case and Test Runner classes using javac. Now run the Test Runner, which will run the test case defined in the provided Test Case class. Verify the output.

Can we're run failed test cases in JUnit?

In the JUnit test framework, a class that allows you to perform retry failed tests called TestRule. This class will rerun tests that have failed without interrupting your test flow.


1 Answers

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.

like image 88
Péter Török Avatar answered Sep 22 '22 01:09

Péter Török