Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run tests from inner classes via Maven

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.

like image 350
Vladimir Avatar asked Jan 18 '10 14:01

Vladimir


2 Answers

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.

like image 118
Pascal Thivent Avatar answered Oct 03 '22 08:10

Pascal Thivent


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>
like image 23
Carlitos Way Avatar answered Oct 03 '22 06:10

Carlitos Way