dictionary - How do I trap a function inputs using powermock? -


lets have function test below:

boolean myfunction (string input1, text rowkey) {    int = 10;    = + 10;    return context.write(rowkey,a); } 

note context.write function writes database.

i mock function , check whether inputs passed correct. how do this?

basically, can below (which cant seem work):

   powermockito.when(context.write((text) anyobject(),   (int) anyobject())).then(compareresult(input1,input2));   private answer<boolean> compareresults(input1, input2) {      asserttrue(input1,this.test1input1acceptancecriteria)      asserttrue(input2,this.test1input2acceptancecriteria)  } 

you should not need so!

assuming context field of enclosing class, "simply" have find way provide mocked version of such context object class under test.

the typical way here: dependency injection

like:

public class foo {    private final context context;     // external constructor use create foo objects    public foo() { (new context(...)); }     // internall ctor, used unit tests    foo(context context) { this.context = context;  } 

and then, can write unit tests such as

@test public void testmyfunction() {   context context = ... create mock   foo undertest = new foo(context);   undertest.myfunction(... 

using above approach, whole need power mocking vanishes. can use "ordinary" mocking frameworks such mokito or easymock prepare/verify context object!

you see, in end, problem code created hard test, unless start thinking dependency injection. if serious testing, watch these videos; give broad introduction how create testable code.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -