0

Code Sample :

Test1 class would be a parent class which would be responsible to execute Test2 class method

import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method;

import org.testng.annotations.Test;

public class Test1 {

@Test
public void test() {
    Class test2 = Test2.class;
    Method method = null;
    Object obj = null;
    try {
        method = test2.getMethod("test", String.class);
    } catch (NoSuchMethodException | SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    method.setAccessible(true);

    try {
        obj = method.invoke(test2.newInstance(), "0");

    } catch (IllegalAccessException | IllegalArgumentException
            | InvocationTargetException | InstantiationException e) {
        // TODO Auto-generated catch block

        e.printStackTrace();
    }

}

}

Test2.class code :

import org.testng.Assert;

public class Test2 {

public void test(String obj){

    Assert.assertEquals(obj, "1");
}

} Here as per this example , assertion should fail , but testng report states as passed So how do i link assertion failure to the testng report

Rajiv
  • 179
  • 1
  • 2
  • 16
  • What are you trying to achieve by calling the test from another class? Why not use xml or testng programmatic invocation? – niharika_neo Jun 29 '15 at 07:41

1 Answers1

0

I think it would be better to isolate the handler of InvocationTargetException in a single catch block and check the result using getCause(). Here is what I tried;

try {
    Object obj = method.invoke(test2obj, "0");
} catch( InvocationTargetException e){
    Throwable e1 = e.getCause();
    System.out.println("Method invocation failed... due to " + e1); 
} catch (IllegalAccessException | IllegalArgumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

And I got this message;

Method invocation failed... due to org.junit.ComparisonFailure: expected:<[0]> but was:<[1]>
fcmonoid
  • 33
  • 1
  • 6