简体   繁体   English

验证对静态外部类的私有静态嵌套类的非静态方法的调用

[英]Verifying the calling of a non-static method of a private static nested class of a static outer class

How would I verify that a non-static method of a private nested class has been called? 我如何验证私有嵌套类的非静态方法已被调用? This is what I have so far: 这是我到目前为止的内容:

new Verifications() {
    {
        Deencapsulation.invoke(MyClass.class.getDeclaredClasses()[0], "run" );
        times = 4;
    }
};

I'm getting this: 我得到这个:

java.lang.IllegalArgumentException: Attempted to invoke non-static method without an instance to invoke it on...


Update 1: 更新1:

@Dennis, I've tried the following, though I'm geting an NPE . @Dennis,尽管我正在获得NPE ,但我已经尝试了以下方法。 I'd like to verify the running of the run method against the singleton that's already in the JVM, and not create a new instance of it (as I do in the code below) in order to invoke method m . 我想针对已在JVM中的单例验证run方法的运行情况,而不是为了调用方法m而创建它的新实例(就像我在下面的代码中所做的那样)。 I tried calling m.invoke(null) without success. 我尝试调用m.invoke(null)失败。 Is there a way to verify the invocation (through it's invocation) in jmockit of a non-static method in a private nested class using reflection on the running instance of the outer class? 有没有办法在外部嵌套类的运行实例上进行反射来验证私有嵌套类中非静态方法的调用中的调用(通过调用)?

    new Verifications() {
        Class c = MyClass.class.getDeclaredClasses()[0];
        Method m = c.getDeclaredMethod("run");
        //Method m = MyClass.class.getDeclaredClasses()[0].getDeclaredMethod("run", Integer.class, CallableClient.class);

        //Deencapsulation.invoke(MyClass.class.getDeclaredClasses()[0], "run" );
        Class[] a = new Class[] { Integer.class, CallableClient.class};
        Object cc = Deencapsulation.newInstance(c, a);
        //Object cc = Deencapsulation.newInstance(c, withInstanceOf(Integer.class), withInstanceOf(CallableClient.class));
        {
            try {
                System.out.println(MyClass.class.getDeclaredClasses()[0].getName());
                m.invoke(cc);
                times = 3;

Update 2: 更新2:

After trying unsuccessfully to get the method like this: final Field runnable = MyClass.class.getDeclaredField("nestedClassInstance"); runnable.setAccessible(true); 尝试获得如下方法失败后: final Field runnable = MyClass.class.getDeclaredField("nestedClassInstance"); runnable.setAccessible(true); final Field runnable = MyClass.class.getDeclaredField("nestedClassInstance"); runnable.setAccessible(true); and then using getDeclaredMethods() as well as getMethods() , i've decided that there's just too much wrong with this approach to continue with this. 然后使用getDeclaredMethods()getMethods() ,我认为这种方法存在太多错误,无法继续进行下去。 I've changed what I'm testing in my test. 我已更改测试中要测试的内容。 Even though I introduced an instance of the inner nested class, I'm assuming that the reason why I can't see the run method may be to do with one or all of: the nested nature of its containing class, constructors, and java's behavior/decision to not display instance methods until it could ensure the successful construction of the instance. 即使我介绍了内部嵌套类的实例,但我还是假设看不到run方法的原因可能与以下一项或全部有关:包含类,构造函数和java的嵌套性质行为/决定,直到可以确保成功构造实例后才显示实例方法。 Curious to know why I couldn't though, with certainty. 好奇地知道为什么我不能确定。

Since you said you wanted to find out if you're trying to invoke a non-static method at runtime, then this bit of reflection should help. 既然您说过要查找是否要在运行时调用非静态方法,那么这种反思应该会有所帮助。

Assuming m is the Method instance corresponding to your method, then Modifier.isStatic(m.getModifiers()) will return true if and only if the method is static, so you can do this check before you actually call invoke . 假设m是与您的方法相对应的Method实例,则Modifier.isStatic(m.getModifiers())将在且仅当方法为静态时返回true,因此您可以在实际调用invoke之前进行此检查。 If you don't already have the Method object, then something like Class 's getMethod will be of use to you. 如果您还没有Method对象,那么将使用ClassgetMethod Class的东西。

Docs on Method , Modifier , and Class . MethodModifierClass文档。


EDIT: All of the above is helpful for checking whether or not the method is static before you call it via invoke . 编辑:上面所有这些都有助于在您通过invoke方法之前检查该方法是否静态。 If you want to use something like jmockit to be able to find out that such a method has been called, then I'm personally wondering how that'll help the IllegalArgumentException that you're getting, especially if it's being thrown by invoke because you're trying to do what the exception message says is not allowed. 如果您想使用类似jmockit的方法来发现已调用了这样的方法,那么我个人想知道这将如何帮助您获得的IllegalArgumentException ,特别是如果通过invoke其抛出, 因为试图做异常消息说不允许的事情。 Even if you could mock it out, that may be covering a deeper issue. 即使您可以模拟它,也可能涵盖了更深层次的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM