java - How to key off a parameter to a stubbed method using Mockito -


greetings.

i mocking search engine testing in web app. search engine returns xml documents different schemas. schema depends on parameter known collection set. returning different schemas based on collection sets part that's difficult mock, because specifying collection set setup method, , void 1 @ that. search engine external jar file can't modify api. have work what's been provided. here's example:

engine engine = factory.getengine(); search search = engine.getsearch(); search.addcollectionset(somecollectionset); searchresult result = search.getsearchresult(); document[] documents = result.getalldocuments(); 

then each document, can xml calling:

document.getdocumenttext(); 

when i'm using mock objects, getdocumenttext() returns xml string, created generator, conforms schema. want use different type of generator depending on collection set provided in step 3 in first code snippet above. i've been trying this:

    doanswer(new answer() {         object answer(invocationonmock invocation) {             if (args == "foo") {                 searchresult result = getmocksearchresult();                 when(search.getsearchresult()).thenreturn(result);             }         }     }).when(search.addcollectionset(anystring())); 

but results in lots of red highlighting :)

basically, goal key off of addcollectionset(somecollectionset) when it's called, can kind of switch off of parameter , ensure different generator used. know how can accomplish this? or there maybe form of dependency injection used conditionally wire generator?

thanks!

update

i've changed factory object never returns engine, rather, search , find objects engine, can this:

search search = factory.getsearch(collectionset);

so i'd this:

when(factory.getsearch(anystring()).thenanswer(new answer() {     object answer(invocationonmock invocation) {         switch(args[0]) {             case fooset: return foosearch; break;             case barset: return barsearch; break;    

in other words, still want key off string passed in getsearch in switch statement. admittedly, more felix has suggested below, i'd rather have cases wrapped in switch. can provide example of how done? thanks!

update

i've seen can capture arguments passed mocked calls, these captured arguments used later assertions. haven't seen way can key off these arguments call mock return different values depending on arguments. seems there has way this, don't have enough experience mockito figure out. surely does!

i recommend wrapping call legacy code own object. end own method along these lines:

class searchenginewrapper {   public string getsearchresult(string collection){     engine engine = factory.getengine();     search search = engine.getsearch();     search.addcollectionset(somecollectionset);     searchresult result = search.getsearchresult();     ...     return document.getdocumenttext();   } } 

now can mock out method. method nicely documents intent. test actual implementation in integration test.

when(searchenginewrapper.getsearchresult("abc").thenreturn("foo"); when(searchenginewrapper.getsearchresult("xyz").thenreturn("bar"); 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -