I am new TestNG and unit-testing in general. I am using TestNG 6.9.6 with Mockito 1.10.19 and PowerMock 1.6.4. I want to verify whether the myMethod()
method in MyService
class internally calls the static method Util.myStaticMethod
with the correct arguments. Since verification of static methods is not natively supported in Mockito, I am using PowerMock along with it. My Test class is shown below:
public class MyTest
{
private MyService myService;
@Captor ArgumentCaptor<String> argCaptor;
@BeforeMethod
public void setup()
{
MockitoAnnotations.initMocks( this );
myService = new MyService();
}
@Test
@PrepareForTest(MyService.class)
public void myTest()
{
PowerMockito.mockStatic(Util.class);
myService.myMethod("arg");
PowerMockito.verifyStatic(10);
Util.myStaticMethod(anyString());
}
}
This test is expected to fail, as myMethod
calls the static method Util.myStaticMethod()
only once. But when i run the test, it always passes, no matter what value i pass to PowerMockito.verifyStatic()
.
Also, if I write another test method in this class and then run the test, I get the following error
org.mockito.exceptions.misusing.UnfinishedVerificationException:
Missing method call for verify(mock) here:
-> at mypackage.MyTest.myTest(MyTest.java:21)
Example of correct verification:
verify(mock).doSomething()
Also, this error might show up because you verify either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
Mocking methods declared on non-public parent classes is not supported.
at mypackage.MyTest.myTest.setup(MyTest.java:10)
Results :
Failed tests:
MyTest.setup:10 UnfinishedVerification
Missing method call for ver...
Tests run: 3, Failures: 1, Errors: 0, Skipped: 1
It fails at the verifyStatic()
method, which makes me think that the verifyStatic() method needs something more that i am not providing. Also, it indicates the total number of tests as 3 whereas in this case I have only two test methods.
Any help would be appreciated.
EDIT : As suggested, I tried putting MyUtil
class in the the @PrepareForTest
annotation, it still gives the same error.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…