How to use assert in android?

后端 未结 6 2263
日久生厌
日久生厌 2020-12-08 09:35

I want to use assert obj != null : \"object cannot be null\" on Android device. The assert doesn\'t seem to work, so I searched online and I found this local so

6条回答
  •  一个人的身影
    2020-12-08 10:12

    Sharing my class which I use for assertions on Android, its simpler, has nice naming and very elegant because it allows you to write asserts like this:

    Assert.that(obj!=null, "Object should not be null");
    

    Here's the code for the class:

    public class Assert {
        public static void that(boolean condition, String message) {
            if (!condition) {
                throw new AssertionError(message);
            }
        }
    }
    

    Hope it helps!

提交回复
热议问题