简体   繁体   English

android单元测试spannablestringbuilder

[英]android unit test spannablestringbuilder

One of my classes uses SpannableStringBuilder and I need to write unit test code for it.我的一个类使用SpannableStringBuilder ,我需要为它编写单元测试代码。 This class is for general use and does not interact with any UI components.此类用于一般用途,不与任何 UI 组件交互。 I considered it as a local unit test but Android Studio complains that 'unit test not mocked'.我将其视为本地单元测试,但 Android Studio 抱怨“未模拟单元测试”。 I've checked Android Mock and robolectric but I can't figure out which instrumentation I have to mock.我已经检查了 Android Mock 和 robolectric,但我不知道我必须模拟哪种仪器。 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'.我还尝试将测试放在AndroidTest目录下并将其作为仪器测试运行,但仍然出现错误“找不到类:“.....”空测试服”。

The unit test code:单元测试代码:

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.最终我得到了它的工作,我会在这里发布解决方案供您参考。

As metioned in my question, this unit test should run on an Android device or an emulator.正如我在问题中提到的,这个单元测试应该在 Android 设备或模拟器上运行。 So the first thing to do is moving it into the androidTest directory.所以首先要做的是将它移动到androidTest目录中。 Only in this way will Android Studio recognize it as an instrumentation test.只有这样,Android Studio 才会将其识别为仪器测试。 Also remember to update your app's gradle.build as described here .还请记住按照此处所述更新您的应用程序的gradle.build But Android Studio (Mine is v2.1) won't allow you to run this test directly (as least I couldn't).但是 Android Studio(我的是 v2.1)不允许你直接运行这个测试(至少我不能)。 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 {}

Today I meet a question that when I do junit test under test directory.When I create a SpannableStringBuilder,for example, SpannableStringBuilder ssb = new SpannabelStringBuilder(str), then the value of ssb is always "null".今天遇到一个问题,我在test目录下做junit test的时候,创建一个SpannableStringBuilder,比如SpannableStringBuilder ssb = new SpannableStringBuilder(str),那么ssb的值总是“null”。 But I remove it under androidTest directory, and extends AndroidTestCase,it effect.但是我在androidTest目录下删除了它,并扩展了AndroidTestCase,它的效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM