0
我当前想要在使用Tycho-Surefire(OSGi项目)的CI服务器上使用PowerMockito运行JUnits。我准备了简单的类:@RunWith(PowerMockRunner.class)不能与tycho surefire一起工作
@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和1.5.6 PowerMock,但它仍然失败......这是父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,但有一些原因,它仍然无法正常工作。
预先感谢重播! :)
可能重复[无法使用PowerMockRunner运行JUnit测试](http://stackoverflow.com/questions/26192929/unable-to-run-junit-test-with-powermockrunner) –
您必须至少使用PowerMock 1.6.1。请参阅:http://stackoverflow.com/questions/26192929/unable-to-run-junit-test-with-powermockrunner –
我读过,有一个答案: 如果你不能升级PowerMock,那么你可以使用JUnit 4.11 。 testCompile '的junit:junit的:4.11', 'org.powermock:powermock核:1.5.6', 'org.powermock:powermock模块-junit4:1.5.6', “org.powermock:powermock -api-mockito:1.5.6' 这就是我做的,顺便说一句。对于OSGi 1.5.6是最后一个版本: https://code.google.com/p/powermock-osgi/ –