java - How to mock this in an Enum class using jmockt? -
i trying write unit test below old legacy enum class. method trying unit test - tolocalpookstring
.
whenever running unit test code, going inside if statement
in tolocalpookstring
method since this
getting resolved corp
.
public enum datacenterenum { corp, phx, slc, lvs; private static final datacenterenum ourlocation = comparelocation(); private static datacenterenum comparelocation() { // code here } private string tolocalpookstring() { if (this == corp || !(testutils.getenvironmentname().equalsignorecase("production"))) { return "/pp/dc/phx"; } return "/pp/dc/" + name().tolowercase(); } public static final string beta_pook_string = ourlocation.tolocalpookstring(); }
is there way can mock this
phx
or slc
or lvs
apart corp
in tolocalpookstring
should not go inside if statement
? using jmockit here.
new mockup<testutils>() { @mock public string getenvironmentname() { return "production"; } }; string ss = datacenterenum.beta_pook_string; system.out.println(ss);
it pretty simple somehow not able understand how it? thoughts?
well, could mock enum, follows:
new mockup<datacenterenum>() { @mock datacenterenum comparelocation() { return datacenterenum.lvs; } };
however, because jvm can perform static initialization once given class/enum, test work if enum hadn't yet been loaded , initialized. so, more robust test following.
@test public void whendatacenterisnotcorpthenlocalpookstringshouldincludeenumname() { new mockup<testutils>() { @mock string getenvironmentname() { return "production"; } }; datacenterenum notcorp = datacenterenum.lvs; string ss = deencapsulation.invoke(notcorp, "tolocalpookstring"); asserttrue(ss.touppercase().endswith(notcorp.name())); }
this works, note writing separate tests private
methods considered bad form. ideally, tests should call public methods indirectly exercise private ones. in particular case, however, there no such public method, guess it's acceptable.
Comments
Post a Comment