13

I have following tests structure:

public class WorkerServiceTest {

    public class RaiseErrorTest extends AbstractDbUnitTest{
        @Test
        public void testSomething(){
        } 

        ...
    }

    ...
}

It's done because I don't want to create a separate class file for every test case extending AbstractDbUnitTest.

The problem is that mvn test doesn't run test from my inner class. Is there is a way how to configure Maven to run such tests? Of course I can create methods in the parent class calling the inner class methods but I want a clearer way.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Vladimir
  • 12,753
  • 19
  • 62
  • 77

2 Answers2

13

Yes, this is possible using the new (well, it's not new anymore) Enclosed runner (since JUnit 4.5) that runs all static inner classes of an outer class.

To use it, just annotate the outer class with @RunWith(Enclosed.class) and make the inner classes static.

@RunWith(Enclosed.class)
public class WorkerServiceTest {

    public static class RaiseErrorTest extends AbstractDbUnitTest{
        @Test
        public void testSomething(){
        } 

        ...
    }

    ...
}

And mvn test will run them.

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • I'm trying this with maven 3.0.4 and Junit 4.11 and still doesn't work. From Eclipse works just fine, but not with `mvn test`. Any ideas? – Chirlo Dec 11 '13 at 15:45
  • What works for me, was to modify the configuration of the "maven-surefire-plugin"... I put the following configuration: ` ` Details at [here](http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html) – Carlitos Way Oct 05 '15 at 23:18
  • @CarlitosWay Can you give some details, it is not showing in your comment. I tried including the src/main/java, and this still does not work. Thanks. – Josef.B Nov 19 '15 at 01:25
  • 1
    There are a few limitations with `@RunWith(Enclosed.class)`: if the outer class (`WorkerServiceTest`) is abstract, the inner classes are still ignored. Second, if the inner classes extend the outer class (this is entirely valid Java BTW) the build will fail. The other solution - configuring `maven-surefire-plugin` excludes - does not have these limitations. – BrunoMedeiros Oct 12 '16 at 13:38
13

I explain (a little more) the solution that I found...

Maven (AFAIK) uses by default the plugin "maven-surefire-plugin" to run any tests defined at your maven project. According to the documentation of this plugin, by default, it excludes tests that are enclosed at inner static classes (or at least it was with version that i'm using - 2.18.1).

So what i did was to put a empty exclude rule; resulting with a pom's build section like this:

<build>
  <plugins>
  ...
  <!-- ~~~~~~~~~~ SUREFIRE -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.18.1</version>
      <configuration>
        <excludes>
          <exclude></exclude>
        </excludes>
      </configuration>
    </plugin>
    ...
  </plugins>
</build>
Carlitos Way
  • 3,279
  • 20
  • 30
  • That worked!!! I used surefire 2.19, but I doubt that was an issue. I, of course, had to change the test source directory. Thanks. – Josef.B Nov 20 '15 at 02:15
  • More specifically the documentation can be found at the [includes](http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#includes) section. Although I think the preferred way would be to use the `Enclosed` runner instead. – bric3 Oct 10 '16 at 12:43
  • 1
    This fixes [JUnit 5 issue 1377](https://github.com/junit-team/junit5/issues/1377) – Raedwald Oct 21 '18 at 00:22