Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 访问Mockito/Powermock中的匿名可运行实例_Java_Mockito_Runnable_Powermock - Fatal编程技术网

Java 访问Mockito/Powermock中的匿名可运行实例

Java 访问Mockito/Powermock中的匿名可运行实例,java,mockito,runnable,powermock,Java,Mockito,Runnable,Powermock,这是我的测试代码: Activity activityMock = Mockito.mock(TestActivity.class); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocationOnMock) throws Throwable { Object[] args = invocationOnMock.getArguments(); // -

这是我的测试代码:

Activity activityMock = Mockito.mock(TestActivity.class);    

doAnswer(new Answer() {
     @Override
     public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
     Object[] args = invocationOnMock.getArguments(); // ->>>> args contains one Foo instance called "foo"
     return invocationOnMock;
  }
}).when(activityMock).runOnUiThread(any(Runnable.class));

runDialogOnUiThread(activityMock, new Foo());
到以下生产代码:

public static void runDialogOnUIThread(final Activity activity, final Foo foo) {
    activity.runOnUiThread(new Runnable() {
        @Override public void run() {
            doSmth();
        }
    });
}
请参阅测试代码中的注释。我希望invocationMock.getArguments()返回可运行实例,但它返回的是Foo实例(这对我来说毫无意义)

我想

doAnswer(new Answer(){..}).when(b).bMethod(any(C.class)) 
在b上的任意位置调用bMethod()时进行拦截,它将C.class的实例传递给doAnswer(),使其在“new Answer()…”回调中可用


如何访问在生产代码中创建的匿名可运行实例?

我猜您的匿名可运行实例是在
Foo
中定义的,对吗

以下是我为再现您的问题而编写的一些代码:

public class MockitoTest {

    @Test
    public void test() {
        final Activity activityMock = Mockito.mock(TestActivity.class);

        doAnswer(new Answer() {
             @Override
             public Object answer(final InvocationOnMock invocationOnMock) throws Throwable {
                 final Object[] args = invocationOnMock.getArguments();
                 System.out.println("Is argument a Runnable? " + (args[0] instanceof Runnable));
                 System.out.println("What is the argument toString()? " + args[0].toString());
                 return invocationOnMock;
             }
        }).when(activityMock).runOnUiThread(any(Runnable.class));

        runDialogOnUIThread(activityMock);
    }


    public void runDialogOnUIThread(final Activity activity) {
        final Runnable r = new Runnable() {
            @Override public void run() {
                System.out.println("***");
            }};
        activity.runOnUiThread(r);
    }

    public static interface Activity {
        void runOnUiThread(Runnable r);
    }

    public static class TestActivity implements Activity {
        @Override
        public void runOnUiThread(final Runnable r) { r.run(); }
    }

}
基本上是一样的,但为了说明我认为你在哪里感到困惑,我们对它进行了删减

输出为:

Is argument a Runnable? true
What is the argument toString()? stackoverflow.MockitoTest$2@6b143ee9
Is argument a Runnable? true
What is the argument toString()? ANON RUNNABLE
请注意,第二个输出包含
toString()
输出的
MockitoTest
,没有任何关于它是
Runnable
的内容。这是因为在匿名
Runnable
中没有明确定义
toString()
方法

我们将
Runnable
更改如下:

    final Runnable r = new Runnable() {
        @Override public void run() {
            System.out.println("***");
        }

        @Override public String toString() {
            return "ANON RUNNABLE";
        }
    };
那么输出是:

Is argument a Runnable? true
What is the argument toString()? stackoverflow.MockitoTest$2@6b143ee9
Is argument a Runnable? true
What is the argument toString()? ANON RUNNABLE
我怀疑您被绊倒的地方是,toString()输出看起来与创建匿名
Runnable
的类同名