Trying to do JUnit Test but Getting some error

Viewed 114

Below is the JUnit Tester Class

import java.lang.reflect.Method;

public class SimpleUnitTester {

    public int execute(Class clazz) throws Exception {
        int failedCount = 0;

        // your code
        
        execute(Reflection.class);
        Reflection reflection = null;
        
        try {
            reflection = (Reflection) clazz.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        Method[] methods = clazz.getDeclaredMethods();
        
        for (Method method : methods) {
            if (method.getName().startsWith("test") && method.getReturnType() == boolean.class) {
                
                try {                                       
                    boolean returnedValue = ((Boolean) method.invoke(reflection)).booleanValue();

                    if (!returnedValue) {
                        failedCount++;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                
            }
        }

        return failedCount;
    }
}

Below is the Reflection Class

public class Reflection {
   public void go() {
   }
   public boolean testA() {
       return true;
   }   
   public void foo() {     
   }
   public void bar() {     
   }
   public int testB() {
       return 0;
   } 
   public boolean testC() {
       return false;
   }
   public boolean testD() {
       return false;
   }
   public boolean testE() {
       return true;
   }
   public boolean anotherTest() {
       return false;
   }
}

I want to do test like, In the Reflection class if methods Start with keyword "test" and If Method have boolean returnType then it will Qualify the test. If the test is failed then Return the test faild value. But I am getting Null in Output.

0 Answers
Related