传递方法作为参数并获取错误:java.lang.NoSuchMethodException:

时间:2013-04-28 07:03:22

标签: java reflection

我收到错误:java.lang.NoSuchMethodException:

这是我的代码

public class ManageEnrollmentTest {
    @Test
    public void Test_Filter_By_Active() throws Exception{

        assertTrue("Log in failed", Helper.LoginTest());

        assertTrue("Activation failed", fitlerResults("Active"));

    }

    private Boolean fitlerResults(String dS){
        Boolean isOk = false;
        try{
            JavascriptExecutor js = (JavascriptExecutor)driver;
            js.executeScript("$('#dType').val('36').change().trigger(\"liszt:updated\");;");

            WebElement findButton = driver.findElement(By.id("findDealersBtn"));
            findButton.click();

            Method method = ManageEnrollmentTest.class.getMethod("verifyActive");           //////// Error
            isOk = loadEnrollmentTablePageByPageAndVerify(method);
        }
        catch(Exception e){
            e.printStackTrace();
            isOk = false;
        }
        return isOk;
    }

    private Boolean loadEnrollmentTablePageByPageAndVerify(Method method){
        return (Boolean)method.invoke(this);
    }

    //browse throw all dealers that are currently on page
    private Boolean verifyActive(){
        ....
        ....
        return isOk;
    }
}

1 个答案:

答案 0 :(得分:3)

您的方法是私有的,但getMethod()只返回公共方法。您需要使用getDeclaredMethod()

getMethod() - 返回一个Method对象,该对象反映此Class对象所代表的类或接口的指定 public 成员方法。