Android的单元测试使用JUnit:测试网络/蓝牙资源 [英] Android unit testing with Junit: testing network/bluetooth resources

查看:1069
本文介绍了Android的单元测试使用JUnit:测试网络/蓝牙资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我慢慢开始沉迷于单元测试。我想我可以使用测试驱动的开发,开发尽可能多的软件。我使用JUnit单元测试我的Andr​​oid应用程序。

I am slowly becoming obsessed with unit testing. I am trying to develop as much software as I can using test-driven development. I am using JUnit to unit test my android applications.

我一直在使用蓝牙的应用程序,我有一个很难单元测试。我有一个使用BluetoothAdapter获得配对和已发现的设备列表中的活动。虽然它的作品,我想知道如何进行单元测试。

I have been working on an app that uses bluetooth and am having a hard time unit testing it. I have an Activity that uses BluetoothAdapter to obtain a list of paired and discovered devices. Although it works, I would like to know how to unit test it.

要获得配对的设备列表,我叫getBondedDevices()上BluetoothAdapter的实例。问题是我不知道如何存根或嘲笑这种方法(或任何其他bluetoothAdapter方法​​,我的活动通话),所以我不能对配对设备的不同列表测试我的活动。

To get the list of paired devices, I call getBondedDevices() on the instance of BluetoothAdapter. The problem is I don't know how to stub or mock this method (or any other bluetoothAdapter method that my Activity calls) so I can't test my Activity against different lists of paired devices.

我想过使用的Mockito或试图继承BluetoothAdapter以某种方式存根出我感兴趣的方法,但它是一个final类,所以我都做不到。

I thought about using Mockito or trying to subclass BluetoothAdapter to somehow stub out the methods I'm interested in, but it's a final class so I can't do either.

这是我怎么能测试使用BluetoothAdapter或其他资源所(据我所知)很难或不可能存根或模拟程序的任何想法?再举一个例子,你会如何测试使用套接字程序?

Any ideas on how I could test programs that use BluetoothAdapter or other resources that are (as far as I know) difficult or impossible to stub or mock? As another example, how would you test a program that uses sockets?

在此先感谢您的帮助

aleph_null

aleph_null

推荐答案

要测试你的活动,你可能会重构code。介绍一个 BluetoothDeviceProvider 有一个默认的实现。

To test your activity, you may refactor your code. Introduce a BluetoothDeviceProvider with a default implementation.

public interface BluetoothDeviceProvider {
    Set<BluetoothDevice> getBluetoothDevices();
}

public class DefaultBluetoothDeviceProvider implements BluetoothDeviceProvider {
    public Set<BluetoothDevice> getBluetoothDevices() {
        return new BluetoothAdapter.getDefaultAdapter().getBondedDevices();
    }
}

然后注入这个新的接口,在活动:

then inject this new interface, in the activity :

public class MyActivity extends Activity {
    private BluetoothDeviceProvider bluetoothDeviceProvider;

    public MyActivity(BluetoothDeviceProvider bluetoothDeviceProvider) {
        this.bluetoothDeviceProvider = bluetoothDeviceProvider;
    }

    protected void onStart() {
        Set<BluetoothDevice> devices = bluetoothDeviceProvider.getBluetoothDevices();
        ... 
    }
    ...
}

现在的活动似乎单元测试。但BluetoothDevice类仍是最终的,你不能在你的活动注入模拟。所以,你必须重构这个新的code和引进新的接口,封装了BluetoothDevice类... - >一个抽象层,经核Android班

Now the activity seems unit-testable. But the BluetoothDevice is still final and you can't inject a mock in your activity. So you have to refactor this new code and introduce a new interface that wraps the BluetoothDevice... -> an abstraction layer upon the core android classes.

在结束的活动行为可以通过各种单元测试...检查那么新引入的接口的实现有待检验。要做到这一点,您可以:

At the end the activity behavior can be checked via various unit tests... So the implementations of the newly introduced interfaces remains to test. To do this, you can :

  • 请他们不要(单位)进行测试,对我来说不是什么大问题,因为他们只是做代表团

  • keep them not (unit) tested, not a big problem for me since they just do delegation

PowerMock

另外,请查阅有关嘲讽final类使用PowerMock 此Wiki网页

Also check this wiki page about mocking final classes using PowerMock.

这篇关于Android的单元测试使用JUnit:测试网络/蓝牙资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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