java - Custom ClassLoader to proxy static library class in JUnit tests -


question
possible provide implementation of class, using custom classloader, utilized static context?

background
working framework recommends use static class wire dependencies.
works this..

public class myclass {      @thisisadependency     private mydependency mydependency;      public void initialize() {         frameworkprovideddependencyresolver.resolvedependencies(this);     }  } 

as might expect, nightmare test with, and, sure enough frameworkprovideddependencyresolver (not real name) throws nullpointerexception unless called within active framework environment not possible junit.

what do, provide custom classloader can use in junit tests provide custom frameworkprovideddependencyresolver wires mock dependencies or whatever.

ok, here's i'd unit tests like:

@runwith(mytestrunner.class) public class testmyclass {      @test     public void testinitialization() {         myclass myclass = new myclass();         myclass.initialize();         // not of test, know     }  } 

mytestrunner opt use custom classloader..

public class mytestrunner extends blockjunit4classrunner {      public mytestrunner(class<?> clazz) throws initializationerror {         super(getfrommyclassloader(clazz));     }      private static class<?> getfrommyclassloader(class<?> clazz) throws initializationerror {         try {             classloader testclassloader = new myclassloader();             return class.forname(clazz.getname(), true, testclassloader);         } catch (classnotfoundexception e) {             throw new initializationerror(e);         }     }  } 

thanks @automatedmike.

ok, slips myclassloader mix can chance swap out frameworkprovideddependencyresolver custom dependency resolver testing..

public class zktestclassloader extends urlclassloader {      public zktestclassloader() {         super(((urlclassloader) getsystemclassloader()).geturls());     }      @override     public class<?> loadclass(string name) throws classnotfoundexception {         class<?> loadedclass = findloadedclass(name);         if (loadedclass != null) {             return loadedclass;         }         system.out.println("loading " + name);         if (name.startswith("my.test.classes")) {             // make sure use myclassloader load test classes,             // classes loads (eg: myclass) come through here.             return super.findclass(name);         } else if (name.endswith("frameworkprovideddependencyresolver")) {             // should here?         }         return super.loadclass(name);     }  } 

ok, , in position load custom frameworkprovideddependencyresolver instead of 1 provided framework.. how do that?

i can ignore request 'frameworkprovideddependencyresolver' , return class, say, 'mymockframeworkprovideddependencyresolver'. that's fine when myclass.initialize calls frameworkprovideddependencyresolver static context, noclassdeffounderror. makes sense.

i can try naming mymockframeworkprovideddependencyresolver same real frameworkprovideddependencyresolver , put in package (eg: i.hate.my.framework.frameworkprovideddependencyresolver). doesn't work myclass looking @ real frameworkprovideddependencyresolver, package , all.

i can try naming class real frameworkprovideddependencyresolver , putting in same package provided framework.. don't need classloader. jvm confused 2 , load whichever appropriate classpath, mine. problem here applies tests; not solution i'm looking for.

lastly, cannot use proxy because frameworkprovideddependencyresolver not interface.

ok, restate question:
possible provide implementation of class, using custom classloader, utilized static context? perhaps, can have class in it's own unique path unique name, can edit load it, such appear in jvm expected path , name i'm trying override? other solution is, of course, welcome.

first, should question if it's necessary mock static resolvedependencies() method. instead, make initialize() delegate object/method , mock that. or use half-mock (e.g. via mockito spy) mocks initialize method on class under test. or make myclass small (by moving functionality other classes) no longer needs (unit) tested. or maybe can prevent initialize() being called , own initialization instead.

if come conclusion absolutely need mock static methods, means use mocking framework supports this, rather inventing own solution (which difficult). 2 well-known contenders in market powermock , jmockit.

ps: it's not clear me why deliberately invoking initialize method test. what's intent?


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -