Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you implement @BeforeClass semantics in a JUnit 4 test written in scala?

I've inherited some JUnit tests written in scala that need to be fixed to use @BeforeClass semantics. I understand that the @BeforeClass annotation must be applied to static methods only. I understand that methods defined in "companion" objects (as opposed to scala classes) are static. How can I get a test method to be called once prior to the individual instance methods in a test class?

like image 903
Edgar Styles Avatar asked Apr 22 '11 02:04

Edgar Styles


People also ask

What is @BeforeClass in JUnit?

org.junitAnnotating a public static void no-arg method with @BeforeClass causes it to be run once before any of the test methods in the class. The @BeforeClass methods of superclasses will be run before those of the current class, unless they are shadowed in the current class.

How do I run a JUnit test in Scala?

To run a test suite using JUnit in Scala IDE, open the source file of the test, right-click in it, and select Run as → JUnit Test. You can re-run your test using the launch configuration which is automatically created. Check the Eclipse documentation for more details.

What is the difference between @before and @BeforeClass in JUnit?

Before and BeforeClass in JUnit The function @Before annotation will be executed before each of test function in the class having @Test annotation but the function with @BeforeClass will be execute only one time before all the test functions in the class.

What is BeforeClass and afterClass in JUnit?

The execution procedure is as follows − First of all, the beforeClass() method executes only once. The afterClass() method executes only once. The before() method executes for each test case, but before executing the test case. The after() method executes for each test case, but after the execution of test case.


1 Answers

object TestClass {

  @BeforeClass
  def stuff() {
    // beforeclass stuff
  }

}

class TestClass {

  @Test
  ...

}

seems to work...

like image 180
Antony Stubbs Avatar answered Sep 28 '22 09:09

Antony Stubbs