dimanche 29 mai 2016

How to test static java methods in groovy SPOCK framework?

I am trying to test static java method in SPOCK groovy framework using Maven.

Here is the java class public class DataController {

 private DataInterface userService;

    public void setUserService(DataInterface userService) {
        this.userService = userService;
    }
   public static int addNumber(){
        int a = 10;
        int b = 20;
        return a+b;
    }   

}

Here is the SPOCK test in groovy

@PrepareForTest([DataController.class])
class BasicMockStaticTest extends Specification {

@Rule PowerMockRule powerMockRule = new PowerMockRule();

    def "When mocking static"() {
        setup :
            PowerMockito.mockStatic(DataController.class)

        when :
            Mockito.when(DataController.addNumber()).thenReturn(30);

        then :
            DataController.addNumber() == 30
    }
 }

and the POM File snippet

   <dependencies>
    <!-- Spock Framework basic dependencies: -->
    <dependency>
        <groupId>org.codehaus.groovy</groupId>
        <artifactId>groovy-all</artifactId>
        <version>2.3.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.spockframework</groupId>
        <artifactId>spock-core</artifactId>
        <!-- The version have to be compatible with Groovy -->
        <version>1.0-groovy-2.3</version>
        <scope>test</scope>
    </dependency>
    <!-- To use Hamcrest matchers: -->
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-core</artifactId>
        <version>1.3</version>
        <scope>test</scope>
    </dependency>
    <!-- To mock classes: -->
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib-nodep</artifactId>
        <version>3.1</version>
        <scope>test</scope>
    </dependency>
    <!-- Use with cglib to mock classes without default constructor: -->
    <dependency>
        <groupId>org.objenesis</groupId>
        <artifactId>objenesis</artifactId>
        <version>2.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
  <groupId>org.codehaus.gmavenplus</groupId>
  <artifactId>gmavenplus-plugin</artifactId>
  <version>1.5</version>
 </dependency>
 <!-- Power mock dependencies -->
 <dependency>
 <groupId>org.powermock</groupId>
 <artifactId>powermock-module-junit4</artifactId>
 <version>1.5.4</version>
 <scope>test</scope>
 </dependency>

 <dependency>
 <groupId>org.powermock</groupId>
 <artifactId>powermock-module-junit4-rule</artifactId>
 <version>1.5.4</version>
 <scope>test</scope>
 </dependency>

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-classloading-xstream</artifactId>
<version>1.5.4</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
    <testSourceDirectory>E:\Workspace\Mars\rg\Spock\src\test\groovy</testSourceDirectory>
<pluginManagement>
    <plugins>
        <!-- GMavenPlus plugin -->
        <plugin>
            <groupId>org.codehaus.gmavenplus</groupId>
            <artifactId>gmavenplus-plugin</artifactId>
            <version>1.5</version>
            <executions>
                <execution>
                    <goals>
                     <goal>addSources</goal>
          <goal>addTestSources</goal>
          <goal>generateStubs</goal>
          <goal>compile</goal>
          <goal>testGenerateStubs</goal>
          <goal>testCompile</goal>
          <goal>removeStubs</goal>
          <goal>removeTestStubs</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
         <!-- Only required if names of spec classes don't match default    Surefire patterns (`*Test` etc.) -->
         <plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.6</version>
    <configuration>
      <useFile>false</useFile>
      <includes>
        <include>**/*Test*.*</include>
        <include>**/*Spec*.*</include>

      </includes>

I have 4 test cases in the groovy folder , 3 are passing , but this static method test is giving error as

 When mocking static(BasicMockStaticTest)  Time elapsed: 0.104 sec  <<< ERROR!
   java.lang.IllegalStateException: Extension API internal error:   org.powermock.api.extension.proxyframework.ProxyFrameworkImpl could not be  located in classpath.
    at   org.powermock.modules.junit4.rule.PowerMockClassloaderExecutor.registerProxyframework(PowerMockClassloaderExecutor.java:60)
    at  org.powermock.modules.junit4.rule.PowerMockClassloaderExecutor.forClass(PowerMoc kClassloaderExecutor.java:50)
    at org.powermock.modules.junit4.rule.PowerMockRule.apply(PowerMockRule.java:31)
at org.spockframework.runtime.extension.builtin.MethodRuleInterceptor.intercept(MethodRuleInterceptor.java:37)
at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:87)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:59)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:120)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:103)
at org.apache.maven.surefire.Surefire.run(Surefire.java:169)
at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:350)
at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1021)

I am running mvn test to test these junits , I tried changing the version of cglib-nodep from 3.1 to 2.2.2 but it did not work.

I checked in the java build path following jar files are included

powermock-module-junit4-1.5.4
powermock-module-junit4-common-1.5.4
powermock-reflect-1.5.4
powermock-module-junit4-rule-1.5.4
powermock-classloading-base-1.5.4
powermock-classloading-xstream-1.5.4
powermock-api-support-1.5.4
powermock-core-1.5.4
groovy-all-2.3.1.jar
spock-core-1.0-grovy-2.3.jar

I also added powermock-mockito-release-full-1.5.4 but after adding that none of the test cases ran and build was success but that was not my intent.

I am suspecting may be some of the dependencies are missing or some of the existing are conflicting but not able to move forward.

Can any one tell what is wrong , I can move forward with the test case even if it fails , I will correct it but I need to remove the error first .

On a side note I did try groovyMock also but it gave nullpointer exception for static method , then I searched and found static method did not work with groovyMock.

I have tried top google link results with popular blogs and tutorial but none seems to work.

Aucun commentaire:

Enregistrer un commentaire