使用Maven 运行单元测试或Java class

170 阅读1分钟

运行单元测试

1. 运行指定单元测试类

mvn test -Dtest="TestClassName1, TestClassName2"

类名可以使用 * 通配符

2. 运行指定单元测试类的某个方法

mvn test -Dtest="TheSecondUnitTest#method1"

3. 运行指定单元测试类的多个个方法

mvn test -Dtest="TheSecondUnitTest#method1+method2"

或者使用通配符 * 指定多个方法

mvn test -Dtest="TheSecondUnitTest#methodABC*"

运行Java 类

需要在 pom.xml 中加入指定plugin

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.6.0</version>
    <executions>
      <execution>
        <goals>
          <goal>java</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <mainClass>com.example.Main</mainClass>
    </configuration>
  </plugin>  

运行pom中设定的 mainClass

mvn exec:java 

或者运行另外一个main class:

mvn exec:java -Dexec.mainClass="com.example.Main"

参考资料:

Run Java Main Method Maven