I am a powermock newbie writing my first TestNG test class. I have a
static class that just returns true if a String argument
equalsIgnoreCase "John" and I am calling that from TestNG test class.
I am mocking with Mockito 1.8x. I have use these blog posts and sample
code as as tutorial examples.
1.
http://code.google.com/p/powermock/source/browse/trunk/modules/module-test/powermock/testng-test/src/test/java/samples/testng/MockStaticExtendsPowerMockTestCaseTest.java
2. http://blog.jayway.com/2009/10/28/untestable-code-with-mockito-and-powermock/
3. http://blog.jayway.com/2009/12/14/powermock-testng-true/
I am not sure what I am doing wrong. Any advice would be appreciated.
Stack Trace
java.lang.IllegalArgumentException:
PowerMockDemoTest_$$_javassist_0@32e003
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke
(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:
607)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:517)
........
Code
---------
public class Static {
public static boolean isEqualToJohn(String name) {
return name != null && name.equalsIgnoreCase("John") ? true : false;
}
}
Test Class
---------------
@PrepareForTest(Static.class)
public class PowerMockDemoTest {
@Test
public void testWithoutDataProvider() throws Exception {
Name john = new Name("abc");
PowerMockito.mockStatic(Static.class);
Mockito.when(Static.isEqualToJohn(Mockito.anyString())).thenReturn
(true);
Assert.assertEquals(Static.isEqualToJohn("abc"), true);
PowerMockito.verifyStatic();
}
private class Name {
private final String name;
public Name(String name) {
this.name = name;
}
public int isNameEqualToJohn() {
boolean flag = Static.isEqualToJohn(name);
return flag ? 1 : 0;
}
}
}
TestSuite:
<suite name="test" verbose="10" object-
factory="org.powermock.modules.testng.PowerMockObjectFactory">
<test name = "demo">
<classes>
<class name = "PowerMockDemoTest"/>
</classes>
</test>
</suite>
Verifying a static method is (unfortunately) a two step process. You could try that and see if it makes any difference. You could also try extending PowerMockTestCase and see if you get the same error message (or perhaps you get a better one).PowerMockito.verifyStatic();Static.isEqualToJohn("abc");
--
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.
java.lang.reflect.InvocationTargetException at org.powermock.modules.testng.internal.PowerMockTestNGMethodHandler.invoke(PowerMockTestNGMethodHandler.java:48) at org.test.PowerMockDemoTestNGTest_$$_javassist_0.testWithoutDataProvider(PowerMockDemoTestNGTest_$$_javassist_0.java) at org.test.PowerMockDemoTestNGTest.main(PowerMockDemoTestNGTest.java:23) Caused by:org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Misplaced argument matcher detected here: at org.test.PowerMockDemoTestNGTest.testWithoutDataProvider(PowerMockDemoTestNGTest.java:29)You cannot use argument matchers outside of verification or stubbing. Examples of correct usage of argument matchers: when(mock.get(anyInt())).thenReturn(null); doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject()); verify(mock).someMethod(contains("foo")) Also, this error might show up because you use argument matchers with methods that cannot be mocked. Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode() methods. atorg.powermock.api.mockito.PowerMockito.verifyStatic(PowerMockito.java:155) at org.powermock.api.mockito.PowerMockito.verifyStatic(PowerMockito.java:129) at org.test.PowerMockDemoTestNGTest.testWithoutDataProvider(PowerMockDemoTestNGTest.java:31) at org.test.PowerMockDemoTestNGTest_$$_javassist_0._d3testWithoutDataProvider(PowerMockDemoTestNGTest_$$_javassist_0.java) ... 27 more
Thanks for the quick reply. My JUnit4 tests run successfully now.
When I try to run my TestNG tests with Eclipse plugin, my Mockito
without Powermock test passes but Powermock with Mockito fails, even
after copying your code in TestNG. When I try to run TestNG test using
Eclipse TestNG plugin (latest one available), then I get the same
error. My TestNG test code using PowerMock
@Test
public void testWithoutDataProvider() throws Exception {
PowerMockito.mockStatic(Static.class);
Mockito.when(Static.isEqualToJohn(Mockito.anyString())).thenReturn
(true);
Assert.assertTrue(Static.isEqualToJohn("abc"));
PowerMockito.verifyStatic();
Static.isEqualToJohn("abc");
}
But this is expected as you clearly mention in this blog (http://
blog.jayway.com/2009/12/14/powermock-testng-true/) that if you don't
specify the object factory in your test suite, PowerMock functionality
does not get picked up and the tests fail.
When I try to run my test suite xml, both tests fail with
IllegalArgumentException with the error message. I tried to see if
this is an isolated incident (I disabled above test by setting @Test
(enabled = false), then running the TestNG test with eclipse plugin.
Tests not dependant upon power mock run successfully with Eclipse
plugin but fail when using running with test suite that defines
PowerMockObjectFactory.
This may sound like a stupid question but have I missed something
here?
Thanks,
Kartik
On Jan 12, 10:44 am, Johan Haleby <johan.hal...@gmail.com> wrote:
> Hi,
>
> Thanks for the files but next time please go to our google
> group<http://groups.google.com/group/powermock>and upload the files
> On Tue, Jan 12, 2010 at 6:46 PM, Kartik Kumar <krishnan.1...@gmail.com>wrote:
>
> > Hi Johan,
>
> > Thanks for taking a look at this. I am attaching all files for your
> > perusal.
>
> > Thanks,
>
> > Kartik
>
> > On Tue, Jan 12, 2010 at 12:12 AM, Johan Haleby <johan.hal...@gmail.com>wrote:
>
> >> Great description! Do you have these things checked in somewhere? Or could
> >> you upload the code to the google group? I'll try to look at it later this
> >> evening.
>
> >> /Johan
>
> ...
>
> read more »