title: Compare the return value of a method when expected return type and return value are passed as string parameters
body: <p>To give a little background on what I am trying to do, please refer my previous question :</p>
  
  <p><a href="https://stackoverflow.com/questions/26919701/run-a-java-method-by-passing-class-name-and-method-name-as-parameter">Run a java method by passing class name and method name as parameter</a></p>
  
  <p>So basically I am trying to call a method and test its return value. I'll read the following parameters from an xml or database : The method class,name,parameters and return value.
  Then I'll execute the method and compare the output.</p>
  
  <p>The code looks like this as of now :</p>
  
  <pre><code>    public static void main(String[] args) throws IOException { 
          runTheMethod("CarBean","getColor","java.lang.String","Red");
      }
  
      public static void runTheMethod(String className, String methodName, String expectedReturnType, Object expectedReturnValue){
          try {
              Object classObj = Class.forName(className).newInstance();
              Method method = classObj.getClass().getMethod(methodName);
              Object returnVal = method.invoke(classObj);
              if(expectedReturnValue.getClass().getName().equals(expectedReturnType)){
  // This is the problem portion
                  System.out.println("Test passed : " + expectedReturnValue.equals(returnVal));
              }else{ 
                  System.out.println("Expected return object type does not match actual return object type");
              }
          } catch (InstantiationException | IllegalAccessException
                  | ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  </code></pre>
  
  <p>Carbean is a user defined pojo :</p>
  
  <pre><code>public class CarBean {
  
      private String brand;
      private String color = "Red";
  
      public CarBean (){
      }
  
      public CarBean (String brand, String color){
          this.brand= brand;
          this.color= color;
      }
  
      /**
       * @return the brand
       */
      public String getBrand() {
          return brand;
      }
  
      /**
       * @param the brand to set
       */
      public void setBrand(String brand) {
          this.brand= brand;
      }
  
      /**
       * @return the color
       */
      public String getColor() {
          return color;
      }
  
      /**
       * @param the color to set
       */
      public void setColor(String color) {
          this.color= color;
      }
  
      @Override
      public boolean equals(Object o){
          if(o == null)               
              return false;
          if(!(o instanceof CarBean))
              return false;
  
          CarBean other = (CarBean) o;
          if(this.brand!=null &amp;&amp; this.color!=null){
              if(this.brand.equals(other.brand) &amp;&amp; this.color.equals(other.color))
                  return true;
              else
                  return false;
          }else{
                  return false;
          }
      }
  
      @Override
      public int hashCode() {
          int hash = 17;
          hash = 31 * hash + this.brand.hashCode();
          hash = 31 * hash + this.color.hashCode();
          return hash;
      }
  
  }
  </code></pre>
  
  <p>Now this works fine for the current code - return type is String and I can compare using equals.
  But what if the method returns a BigDecimal or List? Is there a generic way I can compare multiple object types?</p>
  
  <p>I am assuming that for a user defined java beans (pojo) I can override the equals() and hashcode() to compare it.  Referring my blog for details:
  <a href="http://javareferencegv.blogspot.com/2014/10/overriding-equals-and-hashcode-for-pojo.html" rel="nofollow noreferrer">http://javareferencegv.blogspot.com/2014/10/overriding-equals-and-hashcode-for-pojo.html</a></p>
  
  <p>Any further suggestion is appreciated.</p>