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.
Whenever your test passes and you add one more test and then it fails, it means that the earlier test itself was failing.
RépondreSupprimerThere is an issue with Mockito that it validates the values in the next state by then the test is already passed (i don't remember the exact issue but something similar to that).
Solution: You can write Mockito.validateMockitoUsage() in the end of every test.