【问题标题】:Maven test fails if parent test class is in depenedncy如果父测试类依赖,Maven 测试失败
【发布时间】:2015-03-02 17:59:51
【问题描述】:

我有 2 个 maven 模块和测试如下:

Module1 
`src/test/java/Parent` 

Module2 
`src/test/java/Test  // Test extends Parent`

Module1 对 Module1 有 rumtime 依赖

当我运行 mvn test 时,出现以下错误:

org.apache.maven.surefire.util.SurefireReflectionException: java.lang.reflect.InvocationTargetException;嵌套异常是 java.lang.reflect.InvocationTargetException: null java.lang.reflect.InvocationTargetException 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:606) 在 org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164) 在 org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110) 在 org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175) 在 org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107) 在 org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68) 引起:java.lang.NoClassDefFoundError: Parent

知道我可能在做什么正确吗? eclipse中编译成功。

【问题讨论】:

    标签: maven-3


    【解决方案1】:

    通常在 Maven 中,单元测试受模块限制,不会被传播,这意味着您不能在其他模块中使用它们。不幸的是,正如您已经意识到的那样,Eclipse 的这种分离并不是 100% 正确的。

    如果需要,您可以创建一个called test-jar which contains 来自src/test/ 的文件,这使您可以重用来自其他模块的这些类。这意味着您需要将以下内容添加到您的Module1

    <project>
      ...
      <build>
        <plugins>
          ...
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.5</version>
            <executions>
              <execution>
                <goals>
                  <goal>test-jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          ...
        </plugins>
      </build>
      ...
    </project>
    

    在您的其他模块中,您需要使用以下方法将依赖项添加到这个单独创建的工件中:

    <project>
      ...
      <dependencies>
        <dependency>
          <groupId>groupId</groupId>
          <artifactId>artifactId</artifactId>
          <type>test-jar</type>
          <version>version</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
      ...
    </project>
    

    【讨论】:

      猜你喜欢
      • 2012-08-15
      • 2012-03-02
      • 2021-04-18
      • 1970-01-01
      • 2022-06-29
      • 2014-10-20
      • 1970-01-01
      • 1970-01-01
      • 2016-01-20
      相关资源
      最近更新 更多