android单元测试spannablestringbuilder [英] android unit test spannablestringbuilder

查看:29
本文介绍了android单元测试spannablestringbuilder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个类使用 SpannableStringBuilder,我需要为它编写单元测试代码.此类用于一般用途,不与任何 UI 组件交互.我将其视为本地单元测试,但 Android Studio 抱怨未模拟单元测试".我已经检查了 Android Mock 和 robolectric,但我不知道我必须模拟哪种仪器.我还尝试将测试放在 AndroidTest 目录下并将其作为仪器测试运行,但仍然出现错误找不到类:....."空测试套件.

One of my classes uses SpannableStringBuilder and I need to write unit test code for it. This class is for general use and does not interact with any UI components. I considered it as a local unit test but Android Studio complains that 'unit test not mocked'. I've checked Android Mock and robolectric but I can't figure out which instrumentation I have to mock. I also tried putting the test under the AndroidTest directory and ran it as an instrumentation test but still got an error 'Class not found: "....." Empty test suit'.

单元测试代码:

package xxxxxxx;  // Sorry but I should keep it secret now.

import android.support.test.filters.SdkSuppress;
import android.test.suitebuilder.annotation.SmallTest;
import android.text.SpannableString;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import xxxxxxx.HtmlParser;

import static org.junit.Assert.*;

@RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion=16)
@SmallTest
public class HtmlParserTest {
    @Test
    public void TestGetHtml() {
        // assertion goes here
    }

    @Test
    public void TestGetPlainText() {
        // assertion goes here
    }
}

推荐答案

我不知道是不是因为我做错了什么.最终我得到了它的工作,我会在这里发布解决方案以供您参考.

I don't know if it was because I did something wrong. Eventually I got it work and I'll post the solution here for your reference.

正如我在问题中提到的,这个单元测试应该在 Android 设备或模拟器上运行.所以首先要做的是将它移动到 androidTest 目录中.只有这样,Android Studio 才会将其识别为仪器测试.还要记住按照 gradle.build="nofollow">这里.但是 Android Studio(我的是 v2.1)不允许你直接运行这个测试(至少我不能).您必须创建一个如下所示的测试套件:

As metioned in my question, this unit test should run on an Android device or an emulator. So the first thing to do is moving it into the androidTest directory. Only in this way will Android Studio recognize it as an instrumentation test. Also remember to update your app's gradle.build as described here. But Android Studio (Mine is v2.1) won't allow you to run this test directly (as least I couldn't). You have to create a test suite which looks like:

package xxxxxxx.htmltestsuite;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;


@RunWith(Suite.class)
@Suite.SuiteClasses({
    HtmlParserTest.class
})

// place holder class
public class HtmlTestSuite {}

这篇关于android单元测试spannablestringbuilder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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