Re: [powermock] MissingMethodInvocationException: when() requires an argument which has to be 'a method call on a mock'.

4,539 views
Skip to first unread message

Johan Haleby

unread,
Sep 8, 2011, 7:43:17 AM9/8/11
to powe...@googlegroups.com
You need to use the PowerMockRunner. Have a look at the getting started page at www.powermock.org to find out which one you need. Note that JUnit3 is not recommend.

/Johan

On Thu, Sep 8, 2011 at 1:31 PM, tom <tome...@gmail.com> wrote:
I get this error:
MissingMethodInvocationException:  when() requires an argument which
has to be 'a method call on a mock'.

When i call:
Mockito.when(ClassWithStaticMethod.getStr()).thenReturn(MOCKED_STR);

in my example test, can anyone help?

details:

--------------------------------------------
MockingStaticMethodTest.java
--------------------------------------------
package org.unittests.examples.powermock;

import junit.framework.TestCase;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;

@PrepareForTest(ClassWithStaticMethod.class)
public class MockingStaticMethodTest extends TestCase {

   public void testGetStr_RealStaticMethod() {
       assertEquals(ClassWithStaticMethod.REAL_STATIC_CALLED, new
App().getStaticMethodVal());
   }

   /**
    * Test --> App.method --> otherclass.staticMethod --> returns the
static method mocked value instead of real value.
    */
   public void testGetStr_MockedStaticMethod() {
       final String MOCKED_STR = "mocked_str";
       PowerMockito.mockStatic(ClassWithStaticMethod.class);

Mockito.when(ClassWithStaticMethod.getStr()).thenReturn(MOCKED_STR);
       assertEquals(MOCKED_STR, new App().getStaticMethodVal());
   }

}


-------------
pom.xml
-------------
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://
www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>

 <groupId>org.unittests.examples.powermock</groupId>
 <artifactId>powermock-examples</artifactId>
 <version>1.0-SNAPSHOT</version>
 <packaging>jar</packaging>

 <name>powermock-examples</name>
 <url>http://maven.apache.org</url>

 <properties>
   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     <powermock.version>1.4.10</powermock.version>
 </properties>


 <dependencies>
   <dependency>
     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
     <version>4.9</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>
 </dependencies>
</project>

-------------
App.java
-------------
package org.unittests.examples.powermock;

public class App
{
   public static boolean mStaticInitializerCalled = false;

   static {
       mStaticInitializerCalled = true; // When called from powermock
we have the ability not to call static initializer so this var will
stay false (we will assert it from unit test).
   }

   public String getStaticMethodVal() {
       return ClassWithStaticMethod.getStr();
   }
}

-----------------------------------------
ClassWithStaticMethod.java
-----------------------------------------
package org.unittests.examples.powermock;

public class ClassWithStaticMethod {
   public static final String REAL_STATIC_CALLED = "real static
called";

   public static String getStr() {
       return REAL_STATIC_CALLED;
   }
}

--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To post to this group, send email to powe...@googlegroups.com.
To unsubscribe from this group, send email to powermock+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/powermock?hl=en.


Tomer B

unread,
Sep 8, 2011, 9:45:19 AM9/8/11
to powe...@googlegroups.com
thanks i managed to fix it buy updating the header to:
@RunWith(PowerMockRunner.class)
@SuppressStaticInitializationFor("org.unittests.examples.powermock.App")
@PrepareForTest( { App.class })
Reply all
Reply to author
Forward
0 new messages