Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With jUnit 4, can I parameterize @BeforeClass?

I am using jUnit to manage integration tests for an application that accesses a database. Because setting up the test data is a time-consuming operation, I have been doing that in the @BeforeClass method, which is executed only once per test class (as opposed to the @Before method, which is run once per test method).

Now I want to try a few different permutations for the configuration of the data layer, running all of my tests on each different configuration. This seems like a natural use of the Parameterized test runner. Problem is, Parameterized supplies parameters to the class constructor, and the @BeforeClass method is abstract and is called before the class constructor.

A few questions,

Does Parameterized call the @BeforeClass method for each permutation of parameters, or does it only call once?

If the @BeforeClass method is called repeatedly, is there some way to access the parameter values from inside of it?

If none of these, what do people suggest as the best alternative approach to this problem?

like image 727
Dan Menes Avatar asked Jun 22 '12 20:06

Dan Menes


People also ask

Does JUnit support parameterized tests?

Dependency setupJUnit 5 doesn't provide support for running parameterized tests out of the box. To enable this feature, we have to include the junit-jupiter-params dependency into our project. In case you're using Maven, you need to include such a code snippet in your pom.

What is JUnit BeforeClass?

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.


2 Answers

I think you are going to need a custom test runner. I'm having the same issue you are having (needing to run the same tests using multiple, expensive configurations). You'd need a way to parameterize the set up, perhaps using @Parameter annotations similar to those used by the Parameterized runner but on static member fields instead of instance fields. The custom runner would have to find all static member fields with the @Parameter annotation and then run the test class (probably using the basic BlockJunit4ClassRunner) once per static @Parameter field. The @Parameter field should probably be a @ClassRule.

Andy on Software has done a good job of developing custom test runners, and he explains so pretty clearly in these blog posts here and here.

like image 53
Jake Toronto Avatar answered Sep 29 '22 02:09

Jake Toronto


@BeforeClass is only called once in your example. Which makes sense given the name - before class!

If your tests require different data, there are two choices I can think of:

  1. Set up that data in @Before so it is test specific
  2. Group the tests that you want to run with the same data into separate test classes and use @BeforeClass for each one.
like image 39
Jeanne Boyarsky Avatar answered Sep 29 '22 02:09

Jeanne Boyarsky