官术网_书友最值得收藏!

Even more assertions

If the assertions that are reviewed previously do not seem to be enough for your tests' needs, there is still another class included in the Android framework that covers other cases. This class is MoreAsserts (http://developer.android.com/reference/android/test/MoreAsserts.html).

These methods are also overloaded to support different parameter types. Among the assertions, we can find the following:

  • assertAssignableFrom: This asserts that an object is assignable to a class.
  • assertContainsRegex: This asserts that an expected Regex matches any substring of the specified String. It fails with the specified message if it does not.
  • assertContainsInAnyOrder: This asserts that the specified Iterable contains precisely the elements expected, but in any order.
  • assertContainsInOrder: This asserts that the specified Iterable contains precisely the elements expected, but in the same order.
  • assertEmpty: This asserts that an Iterable is empty.
  • assertEquals: This is for some Collections not covered in JUnit asserts.
  • assertMatchesRegex: This asserts that the specified Regex exactly matches the String and fails with the provided message if it does not.
  • assertNotContainsRegex: This asserts that the specified Regex does not match any substring of the specified String, and fails with the provided message if it does.
  • assertNotEmpty: This asserts that some Collections not covered in JUnit asserts are not empty.
  • assertNotMatchesRegex: This asserts that the specified Regex does not exactly match the specified String, and fails with the provided message if it does.
  • checkEqualsAndHashCodeMethods: This is a utility used to test the equals() and hashCode() results at once. This tests whether equals() that is applied to both objects matches the specified result.

The following test checks for an error during the invocation of the capitalization method called via a click on the UI button:

@UiThreadTest
public void testNoErrorInCapitalization() {
String msg = "capitalize this text";
editText.setText(msg);

button.performClick();

String actual = editText.getText().toString();
String notExpectedRegexp = "(?i:ERROR)";
String errorMsg = "Capitalization error for " + actual;
assertNotContainsRegex(errorMsg, notExpectedRegexp, actual);
}

If you are not familiar with regular expressions, invest some time and visit http://developer.android.com/reference/java/util/regex/package-summary.html because it will be worth it!

In this particular case, we are looking for the word ERROR contained in the result with a case-insensitive match (setting the flag i for this purpose). That is, if for some reason, capitalization doesn't work in our application, and it contains an error message, we can detect this condition with the assertion.

Note

Note that because this is a test that modifies the user interface, we must annotate it with @UiThreadTest; otherwise, it won't be able to alter the UI from a different thread, and we will receive the following exception:

INFO/TestRunner(610): ----- begin exception -----
INFO/TestRunner(610): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
INFO/TestRunner(610):     at android.view.ViewRoot.checkThread(ViewRoot.java:2932)
[...]
INFO/TestRunner(610):     at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
INFO/TestRunner(610): ----- end exception -----
主站蜘蛛池模板: 彰化县| 综艺| 鄂尔多斯市| 重庆市| 成武县| 大安市| 永州市| 嘉禾县| 铁岭市| 吉木萨尔县| 东丰县| 亳州市| 洛阳市| 阿坝| 凯里市| 盐山县| 灵山县| 大厂| 炉霍县| 独山县| 葫芦岛市| 福州市| 白朗县| 章丘市| 井冈山市| 九寨沟县| 孝感市| 元朗区| 清丰县| 岱山县| 铜山县| 宝兴县| 麦盖提县| 剑川县| 大荔县| 抚顺县| 洞头县| 巍山| 上饶县| 英山县| 荥经县|