19

I intend on annotating some of my JUnit4 tests with an @Category annotation. I would then like to exclude that category of tests from running on my Maven builds.

I see from the Maven documentation that it is possible to specify a category of tests to run. I want to know how to specify a category of tests not to run.

Thanks!

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Adam
  • 43,763
  • 16
  • 104
  • 144

1 Answers1

24

You can do this as follows:

Your pom.xml should contain the following setup.

<configuration>
   <excludes>
       <exclude>**/TestCircle.java</exclude>
        <exclude>**/TestSquare.java</exclude>
   </excludes>
</configuration>

If you want regex support just use

<excludes>
    <exclude>%regex[.*[Cat|Dog].*Test.*]</exclude>
</excludes>

http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html

If you want to use Category annotation, you need to do the following:

@Category(com.myproject.annotations.Exclude)
@Test
public testFoo() {
   ....
}

In your maven configuration, you can have something like this

<configuration>
  <excludedGroups>com.myproject.annotations.Exclude</excludedGroups>
</configuration>
Kartik
  • 2,541
  • 2
  • 37
  • 59
  • I cant find anything referencing `excludedGroups` for surefire – Hafiz Oct 30 '17 at 10:06
  • @Hafiz Read this.. https://github.com/junit-team/junit4/wiki/categories – Kartik Jun 10 '18 at 20:35
  • i understand that `com.myproject.annotations.Exclude` is a class/interface (https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit-platform.html) it should be used as: @Category(com.myproject.annotations.Exclude.class) - see bottom of the https://baeldung-cn.com/junit-filtering-tests – Sasha Bond Mar 15 '22 at 20:47