Android 机器人自定义阴影书写

Android 机器人自定义阴影书写,android,robolectric,Android,Robolectric,嗨,我为Robolectric2.3项目写了一个类,如下所示 @Implements(Bitmap.class) public class MyShadowBitmap extends ShadowBitmap { public MyShadowBitmap() { // can also be some other config value setConfig(Bitmap.Config.ARGB_8888); } } 我的问题是如何编写Cus

嗨,我为Robolectric2.3项目写了一个类,如下所示

@Implements(Bitmap.class)
public class MyShadowBitmap extends ShadowBitmap {

    public MyShadowBitmap() {
       // can also be some other config value
        setConfig(Bitmap.Config.ARGB_8888);
    }
}

我的问题是如何编写CustomTestRunner类来扩展RobolectrictTestRunner,以及如何在robolectric中使用MyShadowBitmap进行单元测试,请帮助。

我正在使用robolectric 2.3,并且能够使用robolectric的@Config注释使用自定义阴影

以下是我所拥有的:

       // ShadowLog class, to print android.util.Log to the console when running tests
       @Implements(Log.class)
       public class ShadowLog {
            @Implementation
            public static void d(String tag, String msg) {
                print(tag, "D", msg, null);
            }
            // other implementation
            private static void print(String tag, String level, String msg, Throwable throwable) {
                System.out.printf("%s(%s): %s. %s\n", tag, level, msg, (null == throwable ? "" : throwable));
            }
       }

       // Test class
       @RunWith(RobolectricTestRunner.class)
       @Config(shadows = {ShadowLog.class})
       public class MyTest {
           private static final String TAG = MyTest.class.getSimpleName();

           @Test
           public void testLog {
               Log.d(TAG, "debug log");
               Log.i(TAG, "info log");
               Log.w(TAG, "warning log");
               Log.e(TAG, "error log");
           }
       }
这是我在控制台中看到的:


MyTest(D):调试日志。
MyTest(I):信息日志。
MyTest(W):警告日志。
MyTest(E):错误日志。


注意:您还可以将@Config应用于单个测试用例,而不是整个类。

您解决了这个问题吗?如果是,请选择一个答案。