这是我的单元测试。当我用非空字符串参数创建实例化SpannableStringBuilder实例时,它似乎是空的。这个密码怎么了?
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());
}
}发布于 2017-12-19 14:20:42
问题在测试目录中。一旦SpannableStringBuilder成为Android的一部分,它就无法在常规的JUnit测试目录中被解析。当我将类移到androidTest目录时,一切都像预期的那样开始工作。
发布于 2019-12-09 21:01:17
@RunWith(RobolectricTestRunner::class)使用Robolectric解决了这个问题。
发布于 2017-12-19 11:00:31
您调用的是断言所比较的actualStr.toString(),而不是actualStr本身,因此通过SpannableStringBuilder查找路由,以查看从toString()中产生null的结果。testStr可能是null。
https://stackoverflow.com/questions/47885073
复制相似问题