JUnit test with Maven in a Jenkins plugin

15,702

Several things:

  • add you JUnit classes to src/test/java not src/test
  • for your JUnit class use the scope "test"

E.g.:

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

Also I recommend to manage your repositories in a repository manager such as Nexus, not in your POM:

http://books.sonatype.com/nexus-book/reference/maven-sect-single-group.html

Share:
15,702
23ars
Author by

23ars

I'm a software engineer and for now I work in embedded development. I also worked for 1 and a half year as Java developer. In this period I maintained and developed some Jenkins plug-ins for a company.

Updated on June 14, 2022

Comments

  • 23ars
    23ars almost 2 years

    I have a problem when I tried to run JUnit tests with Maven. For a Jenkins plug-in I wrote a class for test. For example: I have the class ConsoleParser in package com.jenkins_plugin in folder src/main/java. The JUnit test case is ConsoleParserTest in package com.jenkins_plugin in folder src/test. For running the JUnit test I added the dependency in the pom:

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version> 
            </dependency>
    </dependencies>
    

    And I ran in cmd the next command: mvn -Dtest=ConsoleParserTest test

    The problem is that I've got the following error:

    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.
    12:test (default-test) on project swatt_jenkins: No tests were executed!  (Set -
    DfailIfNoTests=false to ignore this error.) -> [Help 1]
    

    The full pom.xml is:

    <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/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <build>
            <finalName>Jenkins_Plugin</finalName>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.3.2</version>
                    <configuration>
                        <source>1.6</source>
                        <target>1.6</target>
                    </configuration>
    
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.12</version>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>emma-maven-plugin</artifactId>
                    <version>1.0-alpha-3</version>
                    <inherited>true</inherited>
                    <executions>
                        <execution>
                            <phase>process-classes</phase>
                            <goals>
                                <goal>instrument</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
        <parent>
            <groupId>org.jenkins-ci.plugins</groupId>
            <artifactId>plugin</artifactId>
            <version>1.526</version>
        </parent>
    
    
        <groupId>Jenkins_Plugin</groupId>
        <version>1.0</version>
        <packaging>hpi</packaging>
    
    
    
        <repositories>
            <repository>
                <id>repo.jenkins-ci.org</id>
                <url>http://repo.jenkins-ci.org/public/</url>
            </repository>
        </repositories>
    
        <pluginRepositories>
            <pluginRepository>
                <id>repo.jenkins-ci.org</id>
                <url>http://repo.jenkins-ci.org/public/</url>
            </pluginRepository>
        </pluginRepositories>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version> 
            </dependency>
        </dependencies>
    </project>
    

    Also, I must mention that with those settings I ran successfully the test but after adding findbugs it crashed. So, I removed findbugs and try to run the test again but the problem that I mentioned appeared. So, can someone explain what I have to do? I tried to add another version of JUnit, Surefire but I had the same problem and I don't understand why.