Basic JUnit 4 Functionality

How is JUnit 4 suitable for creating test cases for using common functions: for example, a setting that is common to several unit test classes? What I did was create a test case and put the general functionality in the method @Before, then any test case that needs this will expand the base class. However, apparently, this requires execution: super.setUp()in each subclass.

Is there a better way?

EDIT

In fact, my proposed solution does not work. Sometimes JUnit will call the TWICE base class. Once this happens, to run the test in the base class first, and again when it reaches the child class (at least I think this is what happens). Thus, the best way to “inherit” the overall functionality of a test case would be great.

+5
source share
4 answers

You do not need to call super.setup()if you use @Beforeannotation :

, . @Before . @Before , .

- :

  @Before
  public void initForAll() {}

super/Main

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

.

EDIT:

.

  • @BeforeClass, TestClass.
  • , - / .

:

private boolean initialized = false;

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

  //Initialize everything

  initialized = true;
}
+5

. setUp() , , super.setUp(), , ?

0

. , , util , . @Before .

0

public void setUp(), , setUp , ( , , ).

The solution is to call the base class setup method something else, for example. baseSetup (). Then they will both be called automatically. Joachim's answer is addressing, but I would like to get a clearer picture of the effects of method overriding.

0
source

All Articles