jmockit - Why is there a NPE on @Injectable class -
i have class(looks this) i'm trying test.
@component @path("/user") @produces(mediatype.application_json) public class userresource extends baseresource { @autowired userservice userservice; @post @path("register") public user registeruser(user user){ ... user registereduser = userservice.register(user); ... homecoming registereduser; } }
the test looks this.
public class userresourcetest { @tested userresource userresource; @injectable userservice userservice; @mocked baseresource baseresource; @test public void registershoulddosomething(){ user user = new user(); final user registerresult = new user(); ... new nonstrictexpectations() {{ userservice.register((user)any); result = registerresult; }}; userresource.registeruser(user); ... } }
for reason in tested class, userservice null , throwing npe when register called on (userservice class not interface/abstract btw). i'm wondering if perhaps of annotations(javax or spring) may clashing jmockit or something(although i've tried removing them)?
i've tried switching injectable @mocked, , i've tried removing , having @mocked test method param. nil seems solving npe.
i had same issue, problem mixing spring annotations - autowired java resource. if mix them java injected. more details please take at: mockit.internal.expectations.injection.injectionpoint mockit.internal.expectations.injection.fieldinjection discardfieldsnotannotatedwithjavaxinjectifatleastoneisannotated method.
this dirty solution problem:
package mockit.internal.expectations.injection; import java.lang.reflect.field; import java.lang.reflect.modifier; public class dirtyhack { static { seek { setfinalstatic(injectionpoint.class.getdeclaredfield("with_injection_api_in_classpath"), false); } grab (exception e) { e.printstacktrace(); } } static void setfinalstatic(field field, object newvalue) throws exception { field.setaccessible(true); field modifiersfield = field.class.getdeclaredfield("modifiers"); modifiersfield.setaccessible(true); modifiersfield.setint(field, field.getmodifiers() & ~modifier.final); field.set(null, newvalue); } }
then test need illustration extends class.
jmockit
No comments:
Post a Comment