如何配置maven项目,以便在主代码更改时测试不会中断?

如何配置maven项目,以便在主代码更改时测试不会中断?,maven,maven-surefire-plugin,Maven,Maven Surefire Plugin,我注意到,在maven项目配置中,我可能会遇到主源代码和测试源代码之间的二进制兼容性问题。当我更改主代码并运行maventesttarget时,会重新编译主代码,但相关测试不会。这可能会导致java.lang.NoSuchMethodError 下面是一个简单的例子: 项目结构: mavenbug |-src/main/java/App.java |-src/test/java/AppClassTest.java |-pom.xml App.java: package mavenbug; pu

我注意到,在maven项目配置中,我可能会遇到主源代码和测试源代码之间的二进制兼容性问题。当我更改主代码并运行maven
test
target时,会重新编译主代码,但相关测试不会。这可能会导致
java.lang.NoSuchMethodError

下面是一个简单的例子:

项目结构:

mavenbug
|-src/main/java/App.java
|-src/test/java/AppClassTest.java
|-pom.xml

App.java:

package mavenbug;

public class App {

    public static class BaseData {}

    public static class DerivedData extends BaseData {}

    public void method(BaseData data) {
        System.out.println("hello2 " + data.getClass().toString());
    }
}
AppClassTest.java:

package mavenbug;

import mavenbug.App.DerivedData;

import org.junit.Test;

public class AppClassTest {

    @Test
    public void test() {
        new App().method(new DerivedData());
    }

}
pom.xml:

<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>
  <groupId>com.example</groupId>
  <artifactId>mavenbug</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>mavenbug</name>
  <description>simplest example of bug</description>
  <dependencies>
     <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
     </dependency>
  </dependencies>
  <build>
    <resources />
    <testResources />
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.5.1</version>
        <executions>
          <execution>
            <id>default-compile</id>
            <phase>compile</phase>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>
          <execution>
            <id>default-testCompile</id>
            <phase>test-compile</phase>
            <goals>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.12.4</version>
        <executions>
          <execution>
            <id>default-test</id>
            <phase>test</phase>
            <goals>
              <goal>test</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  <reporting />
</project>
很好,一切都好。现在将
App.java
中的方法签名更改为:

public void method(DerivedData data) {
这不需要在
AppClassTest.java
中进行任何源代码更改,因为调用匹配新的参数类型

让我们重新运行maven target
test

[INFO] Scanning for projects...
[INFO] 
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building mavenbug 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mavenbug ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ mavenbug ---
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to E:\workax4\mavenbug\target\classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mavenbug ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ mavenbug ---
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to E:\workax4\mavenbug\target\test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mavenbug ---
[INFO] Surefire report directory: E:\workax4\mavenbug\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running mavenbug.AppClassTest
hello2 class mavenbug.App$DerivedData
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.063 sec

Results :

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

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] 
[INFO] Scanning for projects...
[INFO] 
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building mavenbug 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mavenbug ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ mavenbug ---
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to E:\workax4\mavenbug\target\classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mavenbug ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ mavenbug ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mavenbug ---
[INFO] Surefire report directory: E:\workax4\mavenbug\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running mavenbug.AppClassTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.063 sec <<< FAILURE!
test(mavenbug.AppClassTest)  Time elapsed: 0.007 sec  <<< ERROR!
java.lang.NoSuchMethodError: mavenbug/App.method(Lmavenbug/App$BaseData;)V
    at mavenbug.AppClassTest.test(AppClassTest.java:11)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)


Results :

Tests in error: 
  test(mavenbug.AppClassTest): mavenbug/App.method(Lmavenbug/App$BaseData;)V

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

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.071 s
[INFO] Finished at: 2015-02-12T12:50:17+01:00
[INFO] Final Memory: 38M/64M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project mavenbug: There are test failures.
[ERROR] 
[ERROR] Please refer to E:\workax4\mavenbug\target\surefire-reports for the individual test results.
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
[INFO]正在扫描项目。。。
[信息]
[信息]使用线程计数为1的生成器org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder
[信息]
[信息]------------------------------------------------------------------------
[信息]构建mavenbug 0.0.1-SNAPSHOT
[信息]------------------------------------------------------------------------
[信息]
[信息]---maven资源插件:2.6:resources(默认资源)@mavenbug---
[警告]使用平台编码(实际上是UTF-8)复制过滤后的资源,即构建依赖于平台!
[信息]正在复制0资源
[信息]
[信息]---maven编译器插件:2.5.1:编译(默认编译)@mavenbug---
[警告]未使用平台编码UTF-8设置文件编码,即生成依赖于平台!
[信息]正在将1个源文件编译为E:\workax4\mavenbug\target\classes
[信息]
[信息]---maven资源插件:2.6:testResources(默认testResources)@mavenbug---
[警告]使用平台编码(实际上是UTF-8)复制过滤后的资源,即构建依赖于平台!
[信息]正在复制0资源
[信息]
[信息]---maven编译器插件:2.5.1:testCompile(默认testCompile)@mavenbug---
[信息]无需编译-所有类都是最新的
[信息]
[信息]---maven surefire插件:2.12.4:test(默认测试)@mavenbug---
[信息]Surefire报告目录:E:\workax4\mavenbug\target\Surefire报告
-------------------------------------------------------
T T S T S
-------------------------------------------------------
运行mavenbug.AppClassTest

测试运行:1,失败:0,错误:1,跳过:0,所用时间:0.063秒为什么要在两个插件中显式配置执行,默认值将完全满足您的要求。我已在此处上载了eclipse为我创建的有效pom.xml。所以我的eclipse配置可能有点问题。对于更简单的配置也会发生同样的情况,请参见Ok,这里您看到的是增量编译的结果。恐怕除了使用mvn clean test之外,没有简单的答案(有关更多信息,请参阅本博客)