Android JUnit测试无法调用任何Android API? [英] Android JUnit tests can't call any android api?

查看:118
本文介绍了Android JUnit测试无法调用任何Android API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android的测试基础知识中,您可以使用普通的JUnit来测试不调用Android API的类,或者使用Android的JUnit扩展来测试Android组件. 我相信这就是为什么以下测试中的spanString为null.有人可以确认这是原因吗?

From Android's Testing Fundamentals, "You can use plain JUnit to test a class that doesn't call the Android API, or Android's JUnit extensions to test Android components." I believe that's why spanString in the following test is null. Can someone confirm that is the reason?

import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class SpanPainterTest {
  @Test
  public void appliesColorPerRange () {
    SpannablesString spanString = new SpannableString("Aa Bb");
    SpanPainter painter = new SpanPainter(new ForegroundColorSpan(10));
    SpannableString coloredSpan = 
      painter.applyColor(spanString, new Pair<Integer, Integer>(0,1));
    ForegroundColorSpan[] colorArr = 
      coloredSpan.getSpans(0,0, ForegroundColorSpan.class);
    assertEquals(10, colorArr[0]);
  }
}

这是SpanPainter.java

Here's SpanPainter.java

public class SpanPainter {
  ForegroundColorSpan color;
  public SpanPainter (ForegroundColorSpan color) {
    this.color = color;
  }

  public SpannableString applyColor(SpannableString span,  
                                    Pair<Integer, Integer> pair) {
    span.setSpan(color,pair.first, pair.second, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
    return span;
  }
}

但是它说:使用Android的JUnit扩展来测试android组件.因此我扩展了AndroidTestCase,但是spanString仍然等于null.这使我认为没有单元测试可以在不将其设置为null的情况下使用android api对象.是吗?(这是我的第二个问题.)这是SpanPainter测试,它是一个AndroidTestCase,导致spanString = null.

But it says "Use Android's JUnit extension to test android components. So I extend AndroidTestCase, but spanString still equals null. This makes me think that no unit test can use an android api object without it being set to null. Is that right? (That's my second question.)This is the SpanPainter test as an AndroidTestCase, which resulted in spanString = null.

public class SpanPainterTest extends AndroidTestCase {
  @SmallTest
  public void testAppliesColorPerRange () {
    SpannableString spanString = new SpannableString("Aa Bb");
    SpanPainter painter = new SpanPainter(new ForegroundColorSpan(10));
    ...same as above.
    assertEquals(9, colorArr[0]);
  }
} 

这是我的两个JUnit问题.如果我不扩展AndroidTestCase,则将在android api中使用任何对象都将导致null.而且,如果我扩展AndroidTestCase,那么来自android api的任何对象都会自动设置为null.

Those are my two JUnit questions. If I don't extend AndroidTestCase, then will using any object in the android api result in null. And if I extend AndroidTestCase will any object from the android api be automatically set to null.

推荐答案

我遇到了与SpannableStringBuilder类似的问题.通过将测试移至androidTest目录解决了我的问题. (感谢此答案作为建议.)

I was running into a similar problem with a SpannableStringBuilder. My problem was solved by moving my test to the androidTest directory. (Thanks to this answer for the suggestion.)

这是新位置的测试:

/**
 * Instrumentation test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {

    @Test
    public void myStringTest() throws Exception {
        CharSequence unicode = "";
        MyClass myClass = new MyClass(unicode); 
        CharSequence result = myClass.getUnicodeText(); // uses SpannableStringBuilder internally
        CharSequence expected = unicode;
        assertEquals(expected, result);
    }
}

注意

您还可以尝试模拟Android类,但是对于像我这样的测试初学者来说,这要容易得多.我认为缺点是仪器测试的运行速度比单元测试慢.

You can also try mocking the Android classes, but this was a lot easier for a Testing beginner like me. The disadvantage, I suppose, is that the instrumentation tests run slower than Unit tests.

这篇关于Android JUnit测试无法调用任何Android API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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