Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell Maven2 to not test non-@Test-annotated classes in test source folder?

Just got a strange error. Working on project using JUnit and Maven 3.0.3 I've created in my test/src/java folder one test class - ClassifierUtilTest.java, with @Test-annotated methods and stuff, and two utility classes, just for the use in the testing env (one with few static methods for bypassing private visibility scopes and one mock for tested interface).

It works good under Maven 3.0.3 (mvn test), and in Eclipse 3.7 (run as / JUnit test), but when someone else tried to 'mvn test' it with Maven 2.2.1 it failed. Apparently it tried to treat those util classes as test classes and failed due to 'no @Test-annotated methods' and 'more than one constructor'.

It's not JUnit fault (at least it shouldn't be, maven dependency is the same, junit:junit:4.9), so it seems to be strictly maven or maven-surefire-plugin fault. I was wondering if there is some widely-known workaround for Maven 2.2.1 for this problem?

like image 418
r3mbol Avatar asked Dec 13 '22 07:12

r3mbol


1 Answers

maven-surefire-plugin by default runs all the classes the have Test prefix or suffix (like yours ClassifierUtilTest) and TestCase suffix. Just change the name to ClassifierTestUtil and you'll be fine.

You can also exclude certain files/directories in pom.xml, see Inclusions and Exclusions of Tests:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.11</version>
  <configuration>
    <excludes>
      <exclude>**/ClassifierUtilTest.java</exclude>
    </excludes>
  </configuration>
</plugin>
like image 169
Tomasz Nurkiewicz Avatar answered Dec 27 '22 11:12

Tomasz Nurkiewicz