testng - How to mock a method to return a string appended with its argument(java) using powermock -


public class abc{ public gettable(string table){     //some complex logic here.//to skipped while testing     return "schemaname." + table; }  public buildquerymethod1(){     string sql = "select fields "+gettable("table1") +                 "union" +                 "select fields " + gettable("table2") +                 .                 .                 .     return sql; }  } 

i want test buildquerymethod1(). how mock gettable method returns "abc." string argument passed , return resulting string number of times independent of argument passed , number of times method called.

even though should injecting in separate class table name generation, can use mockito's spy functionality. looks code won't compile but, in test this:

public class exampletest() {      private abc example = new abc();      @test     public void test() {         abc spy1 = spy(abc);          stub(spy1. gettable("table1")).toreturn("abc");         stub(spy1. gettable("table2")).toreturn("abc")          // testing on example...     } } 

you have import mockito static stub , spy methods.


Comments