5

I am trying to test a java app from Maven using JUnit.

I have the following POM (relevant section only).

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-junit4</artifactId>
                        <version>2.7.2</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <includes>
                        <include>**/*Test.java</include>
                    </includes>
                </configuration>
  </plugin>

It seems "something" is being run. Here is the output from "mvn -X test":

Running TestSuite
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.769 sec

Results :


Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

Notice the "Running TestSuite". However it still fails to find my test methods. I suspect it might have to do with this:

[DEBUG] Adding to surefire test classpath: /home/julien/.m2/repository/org/apache/maven/surefire/surefire-testng/2.4.3/surefire-testng-2.4.3.jar
[DEBUG] Adding to surefire test classpath: /home/julien/.m2/repository/org/apache/maven/maven-artifact/2.0/maven-artifact-2.0.jar
[DEBUG] Adding to surefire test classpath: /home/julien/.m2/repository/org/codehaus/plexus/plexus-utils/1.0.4/plexus-utils-1.0.4.jar
[DEBUG] Adding to surefire test classpath: /home/julien/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar
[DEBUG] Adding to surefire test classpath: /home/julien/.m2/repository/org/apache/maven/surefire/surefire-api/2.4.3/surefire-api-2.4.3.jar

It says it is using version 3.8.1 of Junit...

However it goes on to say:

Test Classpath :

 [DEBUG]   /home/julien/.m2/repository/junit/junit/4.8.2/junit-4.8.2.jar

I wonder what is the difference between the "surefire test classpath" and the "Test classpath"?

Here is how my test method is named and annotated:

 @Test
    public void test() {

...

It is located here:

src/test/java/com/cheetah/web/pages

Any idea what I am getting wrong?

Here is the effective POM:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>cheetah</artifactId>
    <groupId>com.cheetah</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <groupId>com.cheetah</groupId>
  <artifactId>cheetah-web</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>cheetah-web</name>
  <build>
    <sourceDirectory>/home/julien/Documents/donnees/projets/site-emploi-finance/java/cheetah/web/src/main/java</sourceDirectory>
    <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>/home/julien/Documents/donnees/projets/site-emploi-finance/java/cheetah/web/src/test/java</testSourceDirectory>
    <outputDirectory>/home/julien/Documents/donnees/projets/site-emploi-finance/java/cheetah/web/target/classes</outputDirectory>
    <testOutputDirectory>/home/julien/Documents/donnees/projets/site-emploi-finance/java/cheetah/web/target/test-classes</testOutputDirectory>
    <resources>
      <resource>
        <mergeId>resource-2</mergeId>
        <directory>/home/julien/Documents/donnees/projets/site-emploi-finance/java/cheetah/web/src/main/java</directory>
        <includes>
          <include>**/*.tml</include>
          <include>**/*.properties</include>
        </includes>
      </resource>
      <resource>
        <mergeId>resource-3</mergeId>
        <directory>/home/julien/Documents/donnees/projets/site-emploi-finance/java/cheetah/web/src/main/resources</directory>
        <includes>
          <include>**/*.xml</include>
          <include>**/*.properties</include>
        </includes>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <mergeId>resource-1</mergeId>
        <directory>/home/julien/Documents/donnees/projets/site-emploi-finance/java/cheetah/web/src/test/resources</directory>
      </testResource>
    </testResources>
    <directory>/home/julien/Documents/donnees/projets/site-emploi-finance/java/cheetah/web/target</directory>
    <finalName>cheetah-web-1.0-SNAPSHOT</finalName>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-4</version>
        </plugin>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>2.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>2.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.4</version>
        </plugin>
        <plugin>
          <artifactId>maven-ear-plugin</artifactId>
          <version>2.3.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-ejb-plugin</artifactId>
          <version>2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-javadoc-plugin</artifactId>
          <version>2.5</version>
        </plugin>
        <plugin>
          <artifactId>maven-plugin-plugin</artifactId>
          <version>2.5</version>
        </plugin>
        <plugin>
          <artifactId>maven-rar-plugin</artifactId>
          <version>2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-release-plugin</artifactId>
          <version>2.0-beta-8</version>
        </plugin>
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>2.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>2.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-source-plugin</artifactId>
          <version>2.0.4</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.4.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>2.1-beta-1</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.0.2</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.1-beta-1</version>
      </plugin>
      <plugin>
        <artifactId>maven-help-plugin</artifactId>
        <version>2.1.1</version>
      </plugin>
    </plugins>
  </build>
  <repositories>
    <repository>
      <id>java.net</id>
      <url>http://download.java.net/maven/1</url>
      <layout>legacy</layout>
    </repository>
    <repository>
      <id>maven2-repository.dev.java.net</id>
      <name>Java.net Repository for Maven</name>
      <url>http://download.java.net/maven/2</url>
    </repository>
    <repository>
      <id>jboss</id>
      <name>jboss</name>
      <url>http://repository.jboss.org/maven2/</url>
    </repository>
    <repository>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
      <snapshots>
        <updatePolicy>never</updatePolicy>
      </snapshots>
      <id>jboss-public-repository-group</id>
      <name>JBoss Public Maven Repository Group</name>
      <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
    </repository>
    <repository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Maven Repository Switchboard</name>
      <url>http://repo1.maven.org/maven2</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
      <snapshots>
        <updatePolicy>never</updatePolicy>
      </snapshots>
      <id>jboss-public-repository-group</id>
      <name>JBoss Public Maven Repository Group</name>
      <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
    </pluginRepository>
    <pluginRepository>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Maven Plugin Repository</name>
      <url>http://repo1.maven.org/maven2</url>
    </pluginRepository>
  </pluginRepositories>
  <dependencies>
    <dependency>
      <groupId>com.cheetah</groupId>
      <artifactId>cheetah-domain</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>com.cheetah</groupId>
      <artifactId>cheetah-service</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>org.apache.tapestry</groupId>
      <artifactId>tapestry-core</artifactId>
      <version>5.2.6</version>
    </dependency>
    <dependency>
      <groupId>org.apache.tapestry</groupId>
      <artifactId>tapestry-beanvalidator</artifactId>
      <version>5.2.6</version>
    </dependency>
    <dependency>
      <groupId>org.apache.tapestry</groupId>
      <artifactId>tapestry-spring</artifactId>
      <version>5.2.6</version>
    </dependency>
    <dependency>
      <groupId>org.apache.tapestry</groupId>
      <artifactId>tapestry-test</artifactId>
      <version>5.2.6</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>3.0.5.RELEASE</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.easymock</groupId>
      <artifactId>easymock</artifactId>
      <version>2.5.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-web-api</artifactId>
      <version>6.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <reporting>
    <outputDirectory>/home/julien/Documents/donnees/projets/site-emploi-finance/java/cheetah/web/target/site</outputDirectory>
  </reporting>
</project>
Raedwald
  • 46,613
  • 43
  • 151
  • 237
balteo
  • 23,602
  • 63
  • 219
  • 412
  • What is the artifact type of your pom? – crowne Jul 19 '11 at 16:12
  • @crowne pom for the parent and war for the one containing the tests. – balteo Jul 19 '11 at 17:15
  • You could try move the java source and test into a java module, with jar packaging, and see how the test runs from there. Then possibly leave it in that structure and add the java module as a dependency to the war project. – crowne Jul 19 '11 at 18:57
  • Alternatively, I suspect that because of the war structure the default location of the java test sources may be overridden somehow. You might be able to find a system property that can be set in the pom file to specify what it is. – crowne Jul 19 '11 at 18:59
  • @Crowne, Notice that it does run the "test suite" (see: Running TestSuite). Do you really think that it cannot locate the test sources? – balteo Jul 19 '11 at 19:39
  • 1
    here's your answer ... http://stackoverflow.com/questions/2021771/surefire-is-not-picking-up-junit-4-tests – crowne Jul 19 '11 at 20:11

3 Answers3

3

Running junit tests in maven requires minimal setup. Add junit to your dependencies:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.7</version>
    <scope>test</scope>
</dependency>

Create a class in src/test/java/com/cheetah/web/pages:

public class FooTest {
    @Test
    public void writingToStdOut_succeeds() throws Exception {
        System.out.println("I work!");
    }
}

Run mvn test. That's all. There's no need to configure anything in the surefire plugin. It's no different for a war project vs. a jar project. If you do these two things and it doesn't work, then something is misconfigured somewhere else, and we'll probably need to see the whole pom or, better yet, the effective pom to tell what's going on.

Note that Surefire will only run either testng tests or junit tests but not both in the same build.

Stas Bichenko
  • 13,013
  • 8
  • 45
  • 83
Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • 1
    Ryan. Thanks. I remove the surefire config. It is still not working at all. I have generated the effective POM and still cannot see what's wrong with it... – balteo Jul 19 '11 at 20:27
  • @balteo: either post your pom, maybe using [pastebin](http://pastebin.com/), or start with a fresh pom, following the steps I gave, then add things back in a little at a time until you find what's breaking it. Without more info, I won't be able to help much more. – Ryan Stewart Jul 19 '11 at 20:31
  • 4
    @balteo: Except that I just looked back over your post and saw that testng is listed on your classpath. You need to get that out of there. Last I checked, surefire will only run *either* testng tests *or* junit tests but not both in the same build. Unless you have some other kind of integration between them, you need to pick one. – Ryan Stewart Jul 19 '11 at 20:35
  • Hi @RyanStewart i'm in the situation where it doesn't work. How can i post here the entire POM? I'm working on a quite big project which has many modules. I'm working specifically on a single module, so which POM would you need me to make you see? Thank you so much – Aerox May 02 '15 at 15:10
  • The problem was the wrong import. I used `org.junit.Test` intends of `org.testng.annotations.Test` and so i repeatedly received errors. Anyway, thank you ;-) – Aerox May 03 '15 at 15:52
  • Any help with this one? https://stackoverflow.com/questions/48639730/maven-not-running-integration-test-methods – Rohit Barnwal Feb 06 '18 at 11:09
  • My answer was in the line: "Note that Surefire will only run either testng tests or junit tests but not both in the same build." I had to exclude testng coming from one of the dependency. – Rohit Mar 16 '22 at 14:18
1

Just to mention what everyone already knows, but what still catches me out every now and again. Remember that there is a naming convention to what tests will be "seen" and if you don't name the test class correctly, it is ignored.

running

mvn -X test

shows us (trimmed down):

[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test' with basic configurator -->
[DEBUG]   (s) additionalClasspathElements = []
...
[DEBUG]   (s) excludes = [**/Abstract*.java]
[DEBUG]   (s) includes = [**/*Tests.java, **/*Test.java]

So, by default, only classes ending "Test.java" or "Tests.java" will be picked up.

ISparkes
  • 1,695
  • 15
  • 15
0

In my case problem was with maven version dependecy. I added it manually and I made a mistake.

Advise:

You should always add dependencies by "Insert Dependency" menu. It can be displayed by using shortcut "crtl"+"space" when your cursor is between <dependency> and </dependency> in your xml file. When you in "Select Dependency" menu enter "junit" in "Enter..." textbox and choose your version of junit. After clicking "ok" your dependency will be automatically added. This helps avoid problem with incorrect version typed manually.

Adam Stepniak
  • 815
  • 6
  • 21