Junchen Liu
Junchen Liu

Reputation: 5614

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!

Upvotes: 1

Views: 1206

Answers (2)

MaDa
MaDa

Reputation: 10762

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.

Upvotes: 3

abpan
abpan

Reputation: 111

DBUnit provides exactly the thing you are looking for. Its a JUnit extension only.

  • It has setUp operation options like Clean_Insert - which means the db will be cleaned and required data will be automatically insterted. and many others like Refresh, Update, Insert etc
  • Event easier is - In order to use Dbunit you are not required to extend the DBTestCase class. You can override the standard JUnit setUp() method and execute the desired operation on your database.
  • database configuration operation
  • alot of options on dataset - flat, xml, database , streaming

Upvotes: 1

Related Questions