1

Is it possible to ask junit to run a prescript before doing tests ?

I don't wan't to run @Before as it will only initialize one test. Neither setUp as I think it does the same thing.

There is ClassRule but it require to maintain a list of the Test.

A nice thing would be to have an entry test file so I could run my java code from here.

Example of usage :

  • "the whole test require a database to be started."
  • "a migration script need to be run"

I know people would tell me to run this in gradle before or another build tool.

I am using flyway for the database migration and the version number is stored in my spring boot yaml file. I choose to configure flyway using java instead of gradle because gradle needs to use the command line to do migration so it can't be run with jenkins.

Also, I don't know how I could read the value of my yaml in gradle, neither to run a script.

Dimitri Kopriwa
  • 13,139
  • 27
  • 98
  • 204
  • I marked this as a possible duplicate because "prescript" is ambiguous if you are just talking about Junit. Junit is simply a Java tool that runs unit tests. There are several ways to run Junit (including Gradle). The duplicate question addresses how to run set up steps in a portable _Junit_ way, but you may be asking the wrong question and may want to ask about how to do this in Gradle. – rmlan Dec 13 '16 at 19:04

1 Answers1

2

You need to use @BeforeClass annotation.

It indicates that the static method to which is attached must be executed once and before all tests in the class. That happens when the test methods share computationally expensive setup (e.g. connect to database, connection pool).

For example an expensive operation of Hibernate SessionFactory initialization:

private static SessionFactory sessionFactory;

@BeforeClass
public static void beforeClass() {
    sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
}
DimaSan
  • 12,264
  • 11
  • 65
  • 75