Wow, what a headache to check this code. The main problems are that you cannot use mock objects as key objects in your calls to map.get(obj.getClass()) , and you try invoke() potentially simulate objects for your testing. I had to reorganize your class into a test so that we could mock functionality / behavior and be able to test its behavior.
So, this is your new implementation that needs to be tested with member variables, decoupling the various parts of the function and injected with a test class
public class ClassToTest { MethodStore methodStore; MethodInvoker methodInvoker; ClassToInvoke classToInvoke; ObjectRunner objectRunner; public void testMethod(InterfaceA obj) throws Exception { Method method = methodStore.getMethod(obj); boolean ok = false; if (method != null) { ok = methodInvoker.invoke(method, classToInvoke, obj); } if (ok) { objectRunner.run(obj); } } public void setMethodStore(MethodStore methodStore) { this.methodStore = methodStore; } public void setMethodInvoker(MethodInvoker methodInvoker) { this.methodInvoker = methodInvoker; } public void setObjectRunner(ObjectRunner objectRunner) { this.objectRunner = objectRunner; } public void setClassToInvoke(ClassToInvoke classToInvoke) { this.classToInvoke = classToInvoke; } }
This is your test class, which no longer requires PowerMock, because it cannot make fun of the Method class . It simply returns a NullPointerException.
public class MyTest { @Test public void test() throws Exception { ClassToTest classToTest = new ClassToTest(); ClassA inputA = new ClassA();
Additional classes you require are as follows
public class ClassToInvoke { public void someMethod() {}; } public class ClassA implements InterfaceA { @Override public void run() {
Put all this in your development environment and it will go with a green bar ... woohoo!
source share