首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建新的SpannableStringBuilder返回null

创建新的SpannableStringBuilder返回null
EN

Stack Overflow用户
提问于 2017-12-19 10:38:06
回答 3查看 1.7K关注 0票数 2

这是我的单元测试。当我用非空字符串参数创建实例化SpannableStringBuilder实例时,它似乎是空的。这个密码怎么了?

代码语言:javascript
复制
public class CardNumberTextWatcherTest {

private CardNumberTextWatcher watcher;

@Before
public void setUp() throws Exception {
    watcher = new CardNumberTextWatcher();
}


@Test
public void afterTextChanged_cardNumberDividedWithSpaces() {
    assertTransformText("1111222233334444", "1111 2222 3333 4444");
}

private void assertTransformText(final String testStr,
                                 final String expectedStr) {
    final CardNumberTextWatcher watcher = new CardNumberTextWatcher();
    final Editable actualStr = new SpannableStringBuilder(testStr);
    // When I debug I see that actualStr value is null. Therefore test 
    //doesn't pass. What's the problem?
    watcher.transformText(actualStr);
    assertEquals(expectedStr, actualStr.toString());
}


}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-12-19 14:20:42

问题在测试目录中。一旦SpannableStringBuilder成为Android的一部分,它就无法在常规的JUnit测试目录中被解析。当我将类移到androidTest目录时,一切都像预期的那样开始工作。

票数 8
EN

Stack Overflow用户

发布于 2019-12-09 21:01:17

代码语言:javascript
复制
@RunWith(RobolectricTestRunner::class)

使用Robolectric解决了这个问题。

票数 4
EN

Stack Overflow用户

发布于 2017-12-19 11:00:31

您调用的是断言所比较的actualStr.toString(),而不是actualStr本身,因此通过SpannableStringBuilder查找路由,以查看从toString()中产生null的结果。testStr可能是null

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47885073

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档