【问题标题】:@RunWith(PowerMockRunner.class) do not work with tycho surefire@RunWith(PowerMockRunner.class) 不适用于 tycho surefire
【发布时间】:2015-11-17 09:11:04
【问题描述】:

我目前想使用 Tycho-Surefire(OSGi 项目)在 CI 服务器上使用 PowerMockito 运行 JUnit。我准备了简单的类:

 @RunWith(PowerMockRunner.class)
 public class SampleFirstTest {


@Test
public void addingTest() {
    assertEquals(10, 10);
}

这个测试类在没有 @RunWith 注释时完美且成功地运行,但是当我添加这个注释时,我得到:

 org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.eclipse.tycho:tycho-surefire-plugin:0.24.0:test (default-cli) on project jenkins.junit: There are test failures.

(顺便说一句。这个类应该与注释一起使用,因为当我使用 eclipse 启动器时,它可以两种方式工作)

在测试报告中我得到:

 Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.827 sec <<< FAILURE! - in sample.SampleFirstTest
 initializationError(sample.SampleFirstTest)  Time elapsed: 0 sec  <<< ERROR!

org.powermock.reflect.exceptions.FieldNotFoundException: Field 'fTestClass' was not found in class org.junit.internal.runners.MethodValidator.
at org.powermock.reflect.internal.WhiteboxImpl.getInternalState(WhiteboxImpl.java:581)

我读到要解决这个问题,我应该使用 JUnit 版本 4.1.1 和 PowerMock 1.5.6,但它仍然失败......这是父 pom 的重要部分:

 <properties>
    <tycho-version>0.24.0</tycho-version>
    <release-version>1.0/20</release-version>
    <show-eclipse-log>true</show-eclipse-log>       
    <junit-version>4.1.1</junit-version>
    <powermock.version>1.5.6</powermock.version>

</properties>

<plugin>
    <groupId>org.eclipse.tycho</groupId>
    <artifactId>tycho-surefire-plugin</artifactId>
    <version>${tycho-version}</version>

    <configuration>
        <argLine>-XX:-UseSplitVerifier</argLine>

        <osgiDataDirectory>${basedir}/runtime-workspace-junit/</osgiDataDirectory>
        <deleteOsgiDataDirectory>false</deleteOsgiDataDirectory>
        <useUIHarness>false</useUIHarness>
        <useUIThread>false</useUIThread>


        <dependencyManagement>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit-version}</version>
                <scope>test</scope>
            </dependency>

            <dependency>
                <groupId>org.powermock</groupId>
                <artifactId>powermock-module-junit4</artifactId>
                <version>${powermock.version}</version>
                <scope>test</scope>
            </dependency>


            <dependency>
                <groupId>org.powermock</groupId>
                <artifactId>powermock-api-mockito</artifactId>
                <version>${powermock.version}</version>
                <scope>test</scope>
            </dependency>
        </dependencyManagement>


    </configuration>

</plugin>

<dependencies>

        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>${powermock.version}</version>
            <scope>test</scope>

        </dependency>

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>1.9.5</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.5</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-all</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>

           <dependency>
              <groupId>org.powermock</groupId>
              <artifactId>powermock-api-easymock</artifactId>
              <version>${powermock.version}</version>
              <scope>test</scope>
           </dependency>  

        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito</artifactId>
            <version>${powermock.version}</version>
            <scope>test</scope>

        </dependency>

        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-classloading-base</artifactId>
            <version>${powermock.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
              <groupId>org.powermock</groupId>
              <artifactId>powermock-core</artifactId>
              <version>${powermock.version}</version>
              <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit-version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>jboss</groupId>
            <artifactId>javassist</artifactId>
            <version>3.8.0.GA</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>15.0</version>
        </dependency>

        <dependency>
            <groupId>org.antlr</groupId>
            <artifactId>antlr</artifactId>
            <version>3.2</version>
        </dependency>


</dependencies>

为了测试我使用:

 mvn clean package target-platform-configuration:target-platform tycho-surefire:test -X

更新:

请注意,对于 OSGi,最新版本是 1.5.6 - https://code.google.com/p/powermock-osgi/

所以在我的情况下,我想留在 1.5.6 版本。还有 JUnit 4.11,但由于某种原因它仍然无法正常工作。

提前感谢重播! :)

【问题讨论】:

  • 您必须至少使用 PowerMock 1.6.1。见:stackoverflow.com/questions/26192929/…
  • 我读到了,有一个答案:如果您无法升级 PowerMock,那么您可以使用 JUnit 4.11。 testCompile 'junit:junit:4.11', 'org.powermock:powermock-core:1.5.6', 'org.powermock:powermock-module-junit4:1.5.6', 'org.powermock:powermock-api-mockito: 1.5.6' 这就是我所做的,顺便说一句。对于 OSGi 1.5.6 是最后一个版本:code.google.com/p/powermock-osgi
  • 您在使用 JUnit 4.11 时还有问题吗?
  • 是的,JUnit 4.1.1 和 PowerMock 1.5.6 的问题完全一样......

标签: java eclipse maven junit tycho-surefire-plugin


【解决方案1】:

好的,现在我知道是什么原因了。我有一个依赖于 org.junit 的插件,我只需要排除 4.12 版本就可以了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-11
    相关资源
    最近更新 更多