使用的Mockito核心为创建final类的模拟? [英] Use Mockito-core for create mock of final class?

查看:832
本文介绍了使用的Mockito核心为创建final类的模拟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<我一个href=\"https://github.com/relayr/android-sdk/blob/master/tests/src/androidTest/java/io/relayr/ble/service/OnBoardingServiceTest.java\"相对=nofollow>找到github上例如如何与非标准做的Mockito最终类(BluetoothGatt.class)实例:

I find in github example how with standart Mockito make instance of final class (BluetoothGatt.class) :

...
@RunWith(RobolectricTestRunner.class)
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public class OnBoardingServiceTest {

    private BluetoothGattCharacteristic characteristic;
    private OnBoardingService service;
    private BluetoothGattReceiver receiver = new BluetoothGattReceiver();

    @Before public void initialise() {
        BleDevice bleDevice = mock(BleDevice.class);
        when(bleDevice.getType()).thenReturn(BleDeviceType.WunderbarMIC);
        BluetoothGatt gatt = mock(BluetoothGatt.class);
...

但是从常见问题的Mockito

什么是中的Mockito的局限性

What are the limitations of Mockito


      
  • 需要Java 1.5 +

  •   
  • 不能嘲笑final类

  •   
  • ...

  •   

我检查是从标准的Andr​​oid的SDK BluetoothGatt,所以它看起来像模拟final类。现在,我尝试建立的项目,肯定本次测试工作。
它怎样才能让这里的核心模拟的Mockito最后一类?
而如果code终于没工作,你有什么想法如何让最后一类模拟为Android仪器测试? (我已经尝试PowerMock )。谢谢

I checked it is BluetoothGatt from standard android-sdk, so it look like mock final class. Now i try build project , for sure this test working. How it can make mock final class here with core mockito? And if it code finally not working, have you any idea how make mock of final class for android instrumentation testing? (i'm already try PowerMock). Thanks

推荐答案

Robolectric旨在创建和替代Android的标准类的实现,包括最后的类和方法。引擎盖下,它的工作原理非常相似的方式来PowerMockito,利用自身的类加载器来建立有利于自己的嘲弄的类路径。

Robolectric is designed to create and substitute implementations of Android standard classes, including final classes and methods. Under the hood, it works in very similar ways to PowerMockito, using its own classloader to establish a classpath that favors its own mocks.

Android的班Robolectric mock实现被称为的阴影的,和图书馆是不完整的;你可能会想创建一个适合您的需要自定义阴影

Mock implementations of Android classes in Robolectric are called shadows, and the library is incomplete; you'll probably want to create a custom shadow that suits your needs.

它仍然可能不容易使用为的Mockito方法调用,但你可以使用Robolectric方法来获得你的影子的实例,并写阴影实现,保存方法的参数为实例(等等)。

It still may not be easy to use Mockito for method calls, but you can use Robolectric methods to get instances of your shadows, and write shadow implementations that save method arguments into instances (and so forth).

这篇关于使用的Mockito核心为创建final类的模拟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆