java - org.mockito.exceptions.misusing.InvalidUseOfMatchersException when trying to create custom argument matcher -


i’m using mockito 1.10.18. i’m trying create custom argument matcher method has following signature …

saveresult[] com.sforce.soap.enterprise.enterpriseconnection.update(sobject[] sobjects) throws connectionexception 

i have written following custom argument matcher …

class accountmatcher extends argumentmatcher<sobject[]>  {     private set<string> idlist = new hashset<string>();     accountmatcher(final set<account> mainlist)     {         (final account acct : mainlist)         {             idlist.add(acct.getid());         }   //     }         public boolean matches(object param)      {         final sobject[] compset = ((sobject[]) param);         final set<string> compidlist = new hashset<string>();         (final sobject acct : compset)         {             compidlist.add(acct.getid());         }   //         return util.twosetsmatch(compidlist, idlist);     }   // matches  } 

however, when try , set in junit test …

    final set<account> firstbatch = new hashset<account>();     firstbatch.add(acct);     final sobject[] firstbatchaccts = matchers.argthat(new accountmatcher(firstbatch));     mockito.verify(mockconnection, mockito.times(1)).update(firstbatchaccts); 

i following exception when run …

org.mockito.exceptions.misusing.invaliduseofmatchersexception:  misplaced argument matcher detected here:  -> @ org.mainco.subco.sf.repo.accountdaoit.testupdateaccountmaxrowsreached(accountdaoit.java:129)  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, error might show because use argument matchers methods cannot mocked. following methods *cannot* stubbed/verified: final/private/equals()/hashcode(). mocking methods declared on non-public parent classes not supported.      @ org.mainco.subco.sf.repo.accountdaoit.testupdateaccountmaxrowsreached(accountdaoit.java:130)     @ sun.reflect.nativemethodaccessorimpl.invoke0(native method)     @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62)     @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43)     @ java.lang.reflect.method.invoke(method.java:497)     @ org.junit.runners.model.frameworkmethod$1.runreflectivecall(frameworkmethod.java:50)     @ org.junit.internal.runners.model.reflectivecallable.run(reflectivecallable.java:12)     @ org.junit.runners.model.frameworkmethod.invokeexplosively(frameworkmethod.java:47)     @ org.junit.internal.runners.statements.invokemethod.evaluate(invokemethod.java:17)     @ org.springframework.test.context.junit4.statements.runbeforetestmethodcallbacks.evaluate(runbeforetestmethodcallbacks.java:74)     @ org.springframework.test.context.junit4.statements.runaftertestmethodcallbacks.evaluate(runaftertestmethodcallbacks.java:83)     @ org.springframework.test.context.junit4.statements.springrepeat.evaluate(springrepeat.java:72)     @ org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:231)     @ org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:88)     @ org.junit.runners.parentrunner$3.run(parentrunner.java:290)     @ org.junit.runners.parentrunner$1.schedule(parentrunner.java:71)     @ org.junit.runners.parentrunner.runchildren(parentrunner.java:288)     @ org.junit.runners.parentrunner.access$000(parentrunner.java:58)     @ org.junit.runners.parentrunner$2.evaluate(parentrunner.java:268)     @ org.springframework.test.context.junit4.statements.runbeforetestclasscallbacks.evaluate(runbeforetestclasscallbacks.java:61)     @ org.springframework.test.context.junit4.statements.runaftertestclasscallbacks.evaluate(runaftertestclasscallbacks.java:71)     @ org.junit.runners.parentrunner.run(parentrunner.java:363)     @ org.springframework.test.context.junit4.springjunit4classrunner.run(springjunit4classrunner.java:174)     @ org.eclipse.jdt.internal.junit4.runner.junit4testreference.run(junit4testreference.java:86)     @ org.eclipse.jdt.internal.junit.runner.testexecution.run(testexecution.java:38)     @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:459)     @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:675)     @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.run(remotetestrunner.java:382)     @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.main(remotetestrunner.java:192) 

this because you've extracted firstbatchaccts (the result of matcher call) variable, not method. argthat , other mockito matchers work via side-effects, have called @ right time.

verify checks empty stack of matchers, resets expectations on mock don't interfere verification. matcher calls happen, , call method verified completes process: clears stack, checks method call happened, , switches mock stubbed behavior (if any).

the way have it, argthat gets called, , call verify happens, , verify checks no unused matcher calls have happened. triggers exception.

instead, inline call:

verify(mockconnection).update(argthat(new accountmatcher(firstbatch))); 

...or make method call instead, keeps order right:

public sobject[] arraymatchingaccounts(set<account> accountset) {   return argthat(new accountmatcher(accountset)); }  verify(mockconnection).update(arraymatchingaccounts(firstbatch)); 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -