junit - How to mock a class and its inner static class using Powermock -
i have class :
public class someclass { private string somefield; public someclass(string field) { somefield = field; } public string geturl() { return "http://" + somefield; } public static class builder { private string uri; public builder(string url) { this.uri = url; } public someclass build() { return new someclass(uri); } } }
the above class being called class as:
class mainclass { private someclass someclass; public boolean isurlavailable() { someclass = new someclass.builder("myuri").build(); string url = someclass.geturl(); if (url != null && url.length() > 10) { return true; } return false; } }
my requirement test mainclass, need mock someclass , someclass.builder class. tried mock 2 classes not achieve requirement.
the code showing should not require mock anything. uses builder build something. assuming have tested someclass/builder on own - verify method in main gives correct result.
anyway: mocking static class possible - see here. thing pay attention of: pre-conditions right, example have required annotations:
runwith(powermockrunner.class) @preparefortest(someclassbuilderclass.class)
beyond that: don't mock static in first place. only problem call new!
and - real answer here: pass in instance of someclass work in (instead of calling new builder
inside of method under test)!
Comments
Post a Comment