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.