Java – Maven 2 compiles my tests, but does not run them

I have a simple Maven 2 project that contains tests written for TestNG When I say MVN test Maven 2 compiles my tests but does not run them I have checked this page: http://maven.apache.org/general.html#test -property-name. This is not my case

Can I help you?

My directory structure:

pom.xml
src
  main
    java
      com ...
  test
    java
      com ...
target
  classes <— .class files go there
  test-classes <— .class files with tests go there

If I run MVN - x test (end of log), this is what I see:

...
[INFO] Surefire report directory: <mydir>/target/surefire-reports
Forking command line: /bin/sh -c cd <mydir> && /Sy@R_403_2354@/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home/bin/java -jar /var/folders/+j/+jAx0g2xGA8-Ns9lWNowgk+++TM/-Tmp-/surefirebooter7645642850235508331.jar /var/folders/+j/+jAx0g2xGA8-Ns9lWNowgk+++TM/-Tmp-/surefire4544000548243268568tmp /var/folders/+j/+jAx0g2xGA8-Ns9lWNowgk+++TM/-Tmp-/surefire7481499683857473873tmp

-------------------------------------------------------
  T E S T S
-------------------------------------------------------
Running TestSuite
Tests run: 0,Failures: 0,Errors: 0,Skipped: 0,Time elapsed: 0.191 sec

I have two tests in the project (maintest. Java and filesettest. Java)

My POM xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <groupId>com.SKIP</groupId>
    <artifactId>SKIP</artifactId>
    <name>SKIP</name>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <url>http://www.SKIP.com</url>

    <dependencies>
        <dependency>
            <groupId>org.antlr</groupId>
            <artifactId>antlr-runtime</artifactId>
            <version>3.2</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>5.8</version>
            <scope>test</scope>
            <classifier>jdk15</classifier>
        </dependency>
    </dependencies>
    <build>
        <defaultGoal>install</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.5</version>
            </plugin>            
            <plugin>
                <groupId>org.antlr</groupId>
                <artifactId>antlr3-maven-plugin</artifactId>
                <version>3.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>antlr</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>jsr14</target>
                    <sourceDirectory>src</sourceDirectory>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>com.SKIP.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>attached</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Solution

The "problem" has something to do with the fact that you compile tests using jsr14 target mode, which causes the compiler to issue bytecode compatible with JDK 1.4, so I think the comment was deleted and TestNG did not detect anything to run

My suggestion is to configure Maven compiler plugin to use different compliance levels when compiling code from main and test, as shown below:

<plugin>
  <artifactId>maven-compiler-plugin</artifactId>
  <configuration>
    <source>1.6</source>
    <target>jsr14</target>
  </configuration>
  <executions>
    <execution>
      <id>test-compile</id>
      <phase>process-test-sources</phase>
      <goals>
        <goal>testCompile</goal>
      </goals>
      <configuration>
        <source>1.6</source>
        <target>1.6</target>
      </configuration>
    </execution>
  </executions>
</plugin>

I did a quick test and seemed to work

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>