@RunWith(AndroidJUnit4.class)
public class AssertDemo {
String TAG = "TAG";
@Test
public void mainTest() {
UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
UiObject2 obj = mDevice.findObject(By.text("Some Fake String."));
if (obj == null) {
Log.d(TAG, "obj is null.");
} else {
Log.d(TAG, "obj is not null.");
}
assert obj != null;
Assert.assertNotNull(obj); // Line 33
}
}
Log:
TAG: obj is null.
junit.framework.AssertionFailedError
...
at xxx.mainTest(AssertDemo.java:33)
So, in junit
test, use Assert.assertNotNull(obj)
rather than assert obj != null
.