mercredi 16 décembre 2015

How can I instantiate an object in jUnit, that needs to reflectively create another object inside its constructor?

I'm trying to write a junit test for one of my classes. The design was not done by me; this is a fairly old application, java7, struts1, and clydeDB framework.

The classes are set up like this: ProcessObj, IProcessObj (interface), ProcessHome,

public class ProcessHome {

   private static ProcessHome instance = new ProcessHome();

   //default Constructor
   private ProcessHome() {
   }

   public static ProcessHome getInstance() {
      return instance;
   }

   public IProcessObj getProcessObj() throws POException {
      return ProcessObj.getInstance(); //this is below
   }
}

public class ProcessObj implements IProcessObj {
   // instance
   private static IProcessObj instance;
   ...
   //constuctor
   private ProcessObj() throws POException {
      init();
   }

   static IProcessObj getInstance() throws POException {
      if (instance == null) {
            instance = new ProcessObj();
      }
      return instance;
   }
   //jUnit test setUp

   @Before
   public void setUp() throws Exception {

      public static IProcessObj iPO;
      iPAO = ProcessHome.getInstance()
            .ProcessObj();

      Constructor<ProcessObj> pa = ProcessObj.class
            .getDeclaredConstructor();
      pa.setAccessible(true);
      iPO = pa.newInstance();`

     ...

It works fine up to here, but then in the ProcessObj, the initialization method goes through another set of classes that are set up exactly like the process objects that are above, for the data access layer.

Is there a way that I can create a usable instance of the process object? Can someone explain to me what exactly is going on here? I keep getting a InvocationTargetException. thanks in advance!





Aucun commentaire:

Enregistrer un commentaire