0

Is there any way with Maven to run a piece of code as setup before testing each module? Cleanup for each module as well would be a plus.

To give an example, let's say I have modules A and B, with test classes AA, AB, BA, BB and BC, each of which containing a couple of test method. So module A would run the test classes AA and AB, the first containing the test methods AA.1 and AA.2 and the second AB.1 and AB.2.

Given that, I'd like test setups run like this:

  1. Before module A
  2. Before class AA
  3. Before method AA.1
  4. Before method AA.2
  5. Before class AB
  6. Before method AB.1
  7. Before method AB.2
  8. Before module B
  9. Before class BA
  10. and so forth

It's easy to setup most of this, but I see no way to perform steps 1 and 8, and I'd like to.

EDIT

Yeah, this is the kind of thing that ought to be integration testing, but it isn't, and we are talking about dozens of modules, let alone tests. I need to prepare these resources per-module mostly so the module testing can be made to run in parallel.

Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681

3 Answers3

1

It's not a great solution, but you could always attach an arbitrary plugin and goal to the process-test-classes build phase, which runs by default just before test. See http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference. Obviously this code wouldn't run in the same JVM as your test code, but if you're looking to prepare some external resources, it might be suitable.

If you are preparing external resources, you are probably really performing integration tests, and therefore the pre-integration-test phase is what you'd want to attach to.

Donald_W
  • 1,773
  • 21
  • 35
1

That sounds like an integration tests. This means you can use the pre-integration-test, integration-test and post-integration-test phase to define your setup etc. which is needed to do your appropriate testing. You can bind whatever plugin to the pre-integration-test phase to make a kind of setup (like database setup via sql-maven-plugin etc).

Furthermore you could use TestNG which support groups etc. and furthermore it supports things like this:

@BeforeSuite
@BeforeTest
@BeforeGroups
@BeforeClass
@BeforeMethod
khmarbaise
  • 92,914
  • 28
  • 189
  • 235
0

You can do that in JUnit by defining a nested test suite along the lines outlined here, with @Before and @After methods on the sub-suites for the modules.

You then configure maven to only run your new test suite, not the individual tests.

More ambitiously, you could do this based on a naming convention, without having to specify all the individual tests involved, by:

  • setting up a dynamic AllTests top level test
  • Using Guava ClassPath (or similar) to find all visible test suites and test classes
  • adding them to the top level, or nested, test suites according to the package hierarchy
Community
  • 1
  • 1
soru
  • 5,464
  • 26
  • 30
  • JUnit is only one of many testing frameworks. But I'm curious about something... is there any _automated_ way of defining the test suits, or do I have to type hundreds of class names? Just one of my modules has 228 test _classes_ -- hand-wiring just doesn't scale. – Daniel C. Sobral Mar 04 '15 at 07:25
  • Despite being the most commonly used tool for de-facto integration testing, junit is definitely missing some features in this area. I've updated my answer with some suggestions on how to add them. – soru Mar 04 '15 at 12:23