Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit 4 base functionality

What is the proper way with JUnit 4 to create test cases to use common functionality: e.g. setup that is common to several unit test classes? What I did was create a test case and put the common functionality in the @Before method, then any test case that needs this would extend the base class. However, this seems to require doing: super.setUp() in every subclass.

Is there a better way?

EDIT

Actually my proposed solution is not working. Sometimes JUnit will call the base class TWICE. Once if it happens to run the test on the base class first, and again when it reaches a child class (at least I think this is whats happening). So a better way of "inheriting" common test case functionality would be great.

like image 913
hlm Avatar asked Jun 07 '11 19:06

hlm


People also ask

What is the difference between JUnit 4 and JUnit 5?

Only one test runner can execute tests at a time in JUnit 4 (e.g. SpringJUnit4ClassRunner or Parameterized ). JUnit 5 allows multiple runners to work simultaneously. JUnit 4 never advanced beyond Java 7, missing out on a lot of features from Java 8. JUnit 5 makes good use of the Java 8 features.

What is the difference between JUnit 3 and 4?

JDK required. JUnit 4 uses a lot from Java 5 annotations, generics, and static import features. Although the JUnit 3. x version can work with JDK 1.2+, this usage requires that the new version of JUnit be used with Java 5 or higher.

How do you run a test in JUnit 4?

Step 1: The method implemented under the @BeforeAll annotation is executed once. Step 2: The method implemented under the @BeforeEach annotation executes before the first test. Step 3: The method implemented under the @Test annotation is executed.

What does JUnit @rule do?

A JUnit rule lets you define tasks that are common in your test class codebase and let you use it in your test class. You utilize the composition principle instead of inheritance when you use a JUnit rule in a test class. A JUnit rule is declared through the @Rule annotation in the test class.


1 Answers

You don't need to invoker super.setup() if you use the @Before annotation:

When writing tests, it is common to find that several tests need similar objects created before they can run. Annotating a public void method with @Before causes that method to be run before the Test method. The @Before methods of superclasses will be run before those of the current class.

I'd suggest something like this:

  @Before
  public void initForAll() {}

In the super/Main class

and any

  @Before
  public void initTest() {...}

In your Testcases.

EDIT:

To answer your questions in the edit.

  1. You could use @BeforeClass which will be invoked once per TestClass.
  2. But I Think you are looking for something like a lazy /static Initialisation.

I do this like this:

private boolean initialized = false;

@BeforeClass
public static void init()
{
  if(initialized)
  {
    return;
  }

  //Initialize everything

  initialized = true;
}
like image 55
oers Avatar answered Sep 23 '22 21:09

oers