Hello,

I am trying to build a basic unit framework. I have got this method in a class called KUnit3

public static void checkEquals(double value1,double value2) {

    if(value1 == value2) {
        addToReport(String.format(" %f == %f", value1, value2));
        passedChecks++;

    } else {
        addToReport(String.format("* %f == %f", value1, value2));
        failedChecks++;
      }
}

I have to access this method using Java reflection API from a class called 'TestASimpleClass' like for example

checkEquals(double value1, double value2)

Below is what I have done so far

    public static void main(String[] args) throws Exception{
    KunitTesting kunit = new KunitTesting();
        ASimpleClass simple = new ASimpleClass();
        Method [] method = kunit.getClass().getMethods();

        for (Method m : method) {
            if( m.getName().startsWith("check"))
               {
                  m.setAccessible(true);
                  Method cE = kunit.getClass().getDeclaredMethod(m.getName(), double.class, double.class);
                  System.out.println(cE);


               }
        }

I am stuck after that. Can someone help me?

Thanks

Recommended Answers

Now you have the Method object you can call it by using Method's invoke

Jump to Post

All 3 Replies

Now you have the Method object you can call it by using Method's invoke

Which is the method object? is it the method?
if method is the object, would it be this

cE.invoke(method, 40.02, 40.02);

How do I use the checkEquals method from the 'Kunit3' class/

Thanks for the help. I am really stuck

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.