PowerMockito.whenNew Fail !

1,332 views
Skip to first unread message

jason0909

unread,
Nov 21, 2012, 2:32:31 AM11/21/12
to powe...@googlegroups.com
The following is my test code ,but some exception information is throw ? can you tell me why/?
package com.example.mock;

import java.io.File;

import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.testng.Assert;
import org.testng.annotations.Test;


public class TestMockito {
@Test
   public void testRegisterService2() throws Exception {  
       String s="A:/aa.txt";  
       File file=PowerMockito.mock(File.class);  

       PowerMockito.whenNew(File.class).withArguments(Mockito.anyString()).thenReturn(file);  
       PowerMockito.when(file.exists()).thenReturn(true);  
       PowerMockito.verifyNew(File.class).withArguments(s);
       File a=new File(s);  
       System.out.println(a.exists());
       Assert.assertEquals(true, a.exists());  
   }  
}

the exception information:
FAILED: testRegisterService2
java.lang.AssertionError: Wanted but not invoked java.io.File(
    "A:/aa.txt"
);
Actually, there were zero interactions with this mock.
at org.powermock.api.mockito.internal.invocationcontrol.InvocationControlAssertionError.throwAssertionErrorForNewSubstitutionFailure(InvocationControlAssertionError.java:93)
at org.powermock.api.mockito.internal.verification.DefaultConstructorArgumentsVerfication.invokeSubstitute(DefaultConstructorArgumentsVerfication.java:51)
at org.powermock.api.mockito.internal.verification.DefaultConstructorArgumentsVerfication.withArguments(DefaultConstructorArgumentsVerfication.java:44)
at com.example.mock.TestMockito.testRegisterService2(TestMockito.java:22)
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:597)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:702)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:894)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1219)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:768)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:87)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1185)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1110)
at org.testng.TestNG.run(TestNG.java:1022)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:109)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:202)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:173)


===============================================
    Default test
    Tests run: 1, Failures: 1, Skips: 0
===============================================

Johan Haleby

unread,
Nov 21, 2012, 2:39:48 AM11/21/12
to powe...@googlegroups.com
You're missing the RunWith and PrepareForTest annotation?

/Johan

--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To view this discussion on the web visit https://groups.google.com/d/msg/powermock/-/b9x9ug5mtB0J.
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.

jason0909

unread,
Nov 21, 2012, 3:09:44 AM11/21/12
to powe...@googlegroups.com
I use testng which have not RunWith annotation , i update my code and add PrepareForTest on class and get same exception .


在 2012年11月21日星期三UTC+8下午3时39分49秒,Johan Haleby写道:

Johan Haleby

unread,
Nov 21, 2012, 3:24:58 AM11/21/12
to powe...@googlegroups.com
Have you added the PowerMockRunner to suite.xml? Otherwise extend from PowerMockTestCase.

/Johan

To view this discussion on the web visit https://groups.google.com/d/msg/powermock/-/WoZji36AGxUJ.

jason0909

unread,
Nov 21, 2012, 3:58:35 AM11/21/12
to powe...@googlegroups.com
Oh, i don't extend from PowerMockTestCase 
now ,it is ok . 
thanks 


在 2012年11月21日星期三UTC+8下午4时24分59秒,Johan Haleby写道:
Reply all
Reply to author
Forward
0 new messages