Search code examples
unit-testingmavenjunitjunit4junit3

Do we have a @beforeTests method which can run once, before all the tests starts


In Junit, I know there is a @beforeclass , @before annotation, do we have a annotation or design, allow us to write a method to run ONLY once before the whole testing process?

we have a script, which setup some database data (config, static, lookup table etc.....) for the test, but its too expensive to run before each individual test, we would like it to set it, only once before start running any test.

thanks!


Solution

  • Since you tagged your question with maven, I'll go this way: you could use the pre-integration-test phase to run this one-time expensive script (symmetrically, you clean up in post-integration-test).

    You could use exec-maven-plugin for this:

    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>exec-maven-plugin</artifactId>
      <version>...</version>
      <executions>
        <execution>
          <id>some-execution</id>
          <phase>pre-integration-test</phase>
          <goals>
            <goal>exec</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <executable><!-- runnable command or file name here --></executable>
      </configuration>
    </plugin> 
    

    JUnit does not have this kind of annotation, because it does not make any assumption about the environment: its goal is to test one class at a time in an isolated manner.