@RunWith(PowerMockRunner.class)不适用于tycho surefire

时间:2015-11-17 09:11:05

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

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

 @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但由于某种原因它仍然无效。

提前感谢您的重播! :)

1 个答案:

答案 0 :(得分:0)

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