0

I currently want to run JUnits using PowerMockito on CI server using Tycho-Surefire (OSGi project). I prepared simple class:

 @RunWith(PowerMockRunner.class)
 public class SampleFirstTest {


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

This test class perfectly and successfully runs when has no @RunWith annotation, but when I add this annotation I get:

 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.

(Btw. This class should work with annotation because when I use eclipse launcher it works in both ways)

And In test report I get:

 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)

I read that to fix that problem I should use JUnit version 4.1.1 and PowerMock 1.5.6 but It still fails... This is important part of parent 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>

To test I use:

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

Update:

Notice that for OSGi the last version is 1.5.6 - https://code.google.com/p/powermock-osgi/

So in my case I want to stay in 1.5.6 version. And JUnit 4.11 but with some reason it still do not work.

Thanks in advance for replays ! :)

P.Kurek
  • 71
  • 1
  • 4
  • Possible duplicate of [Unable to run JUnit test with PowerMockRunner](http://stackoverflow.com/questions/26192929/unable-to-run-junit-test-with-powermockrunner) – Stefan Birkner Nov 17 '15 at 09:42
  • You have to use at least PowerMock 1.6.1. See: http://stackoverflow.com/questions/26192929/unable-to-run-junit-test-with-powermockrunner – Stefan Birkner Nov 17 '15 at 09:43
  • 1
    I read that and there is an answer: If you cannot upgrade PowerMock then you can use 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' And this is what I did, btw. For OSGi 1.5.6 is last version: https://code.google.com/p/powermock-osgi/ – P.Kurek Nov 17 '15 at 09:48
  • Do you still have the problems with JUnit 4.11? – Stefan Birkner Nov 17 '15 at 19:13
  • Yes I do, exacly the same problem with JUnit 4.1.1 and PowerMock 1.5.6 ... – P.Kurek Nov 18 '15 at 08:17
  • Could you please run `mvn dependency:tree` and explicitly check the real version of JUnit. – Stefan Birkner Nov 18 '15 at 09:02
  • Stefan thanks for your replays. I run mvn dependency:tree and build was succeess for all plugins and Junit was 4.11 org.powermock:powermock-module-junit4:jar:1.5.6:test \- org.powermock:powermock-module-junit4-common:jar:1.5.6:test +- org.mockito:mockito-core:jar:1.9.5:test +- org.hamcrest:hamcrest-core:jar:1.1:test +- org.mockito:mockito-all:jar:1.9.5:test +- org.hamcrest:hamcrest-all:jar:1.3:test +- org.powermock:powermock-core:jar:1.5.6:test \- org.javassist:javassist:jar:3.18.2-GA:test +- junit:junit:jar:4.11:test – P.Kurek Nov 18 '15 at 10:30

1 Answers1

0

Ok now I know what was the reason. I had a plugin which has dependency to org.junit, I just had to exclude version 4.12 and everything is ok.

P.Kurek
  • 71
  • 1
  • 4