0

I have a Dropwizard application and I am trying to create unit tests for testing the application.

I read through a bunch of resources and I am now using DropwizardTestRule to kickstart the application and run some unit tests. I also have a custom config-test.yml that I use to point the application to my local database to run the queries against.

And the Dropwizard version we are using is 0.9.2. That is unfortunately a constraint that cannot be changed at the moment.

The problem I have is running migrations on the DB before the test suite runs.

The issue is:

  • mvn package runs unit tests as part of the packaging task.
  • Unit tests need that migrations be run as a pre-requisite.
  • Running migrations need the jar created by mvn package as a pre-requisite.

This seems like a cyclic dependency to me that can be resolved only by first calling mvn package -DskipTests=true which leads to the jar creation. Then running the migrations. Then running the tests as a separate maven task.

This means my build will now have to be a script of commands. Is there a better way of doing this? Or is this the only hack?

How do I tell Dropwizard to run the migrations before running the test suite?

LeoNeo
  • 739
  • 1
  • 9
  • 28
  • 1
    Why does a _unit test_ need a database connection? This sounds more a task for an _integration test_. – Seelenvirtuose Jun 23 '18 at 11:18
  • To test your queries, POJOs and Mappers. Also to create scenarios. Using objects created does help test a lot of scenarios but using DB will also help us test the code with larger dataset that you don't have to create. – LeoNeo Jun 27 '18 at 14:21
  • "To test your queries, POJOs and Mappers [...] but using DB will also help us test the code with larger dataset that you don't have to create" => And this is an integration test (as I said)! Maven can run integration tests, too. And they perfectly fit in the lifecycle as they run _after_ the packaging. – Seelenvirtuose Jun 28 '18 at 06:51
  • Alright I concede my folly in calling it a Unit Test. Let us call it Integration test. So what is the solution then now that the nomenclature debate is out of the way! And assume I throw that code out of the unit tests. Now.. How do I run migrations from the code? – LeoNeo Jul 01 '18 at 17:03
  • Well ... According to the [Maven Build Lifecycle](https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html) there is a `integration-test` phase after the `package` phase. So, you have a packaged JAR file which you can use in your integration tests. Note, that you also should do some steps in the phases `pre-integration-test` and `post-integration-test` and always run them via `mvn verify`. Have a look at the [Maven Failsafe Plugin](https://maven.apache.org/surefire/maven-failsafe-plugin/). – Seelenvirtuose Jul 02 '18 at 07:12
  • Side note: This is not only a _nomenclature debate_! The distinction is essential because unit tests and integration tests run at very different phases in the Maven Build Lifecycle. – Seelenvirtuose Jul 02 '18 at 07:16

0 Answers0