title: Unit testing by passing runtime parameters to java reflect method
body: <p>I am making a class object using the a java bean in my code. Then I am calling a particular method of that class obj</p>
  
  <pre><code>public static void runUnitTest(String className, String methodName, List&lt;Object&gt; inputParams, Object expectedReturnValue){
                  try {
          // Make the class object to be tested on
          Object classObj = Class.forName(className).newInstance();
          Method calledMethod = classObj.getClass().getMethod(methodName,inputParams.get(0).getClass());
          Object returnVal = calledMethod.invoke(classObj,inputParams.get(0));
          }catch (InstantiationException | IllegalAccessException
                          | ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException e) {
                      e.printStackTrace();
                  }
      }
  </code></pre>
  
  <p>I call it this way :</p>
  
  <pre><code>public static void main(String[] args) throws IOException { 
          List&lt;Object&gt; inputParams = new ArrayList&lt;Object&gt;();
          inputParams.add(new BigDecimal(1234));
          runUnitTest("NumberTest","getOutputNumber",inputParams,new BigDecimal(5678));
      }
  </code></pre>
  
  <p>The code of NumberTest:</p>
  
  <pre><code>public class NumberTest{
  
      public BigDecimal getOutputNumber(BigDecimal numberId) {
          if(numberId.intValue() == 1234)
          {
              return new BigDecimal(5678);
          }else 
              return new BigDecimal(0);
      }
  
      public BigDecimal getAdditionalOutputNumber(BigDecimal numberId, String additionalInfo) {
          if(numberId.intValue() == 1234 &amp;&amp; "Pass".equals(additionalInfo))
          {
              return new BigDecimal(5678);
          }else 
              return new BigDecimal(0);
      }
  }
  </code></pre>
  
  <p>This works fine as I know that the method <code>getOutputNumber</code> has only one parameter. But when I have to call the same code for multiple methods where number of parameters differ (e.g. <code>getAdditionalOutputNumber</code>) I can't use the same code. I don't want to use a multiple if else or case block on the basis of size of inputParams. 
  Is there a generic way of calling the below :</p>
  
  <pre><code>Method calledMethod = classObj.getClass().getMethod(methodName,**?? What to pass here ??**);
  Object returnVal = calledMethod.invoke(classObj,**?? What to pass here ??**);
  </code></pre>