better way to do Debug only assert code

后端 未结 3 1494
说谎
说谎 2020-12-09 20:53

I am writing my first Android application and I am liberally using asserts() from junit.framework.Assert

I would like to find a way to ensure that the asserts are on

3条回答
  •  悲&欢浪女
    2020-12-09 21:38

    I mean if you were using a language feature, like assert(), the compiler should be able to strip that out. But this is an actual class and if a class is referenced by executable code it will be included or assumed included in the final product by the compiler.

    However there is nothing stopping you from creating a script that removes all the references to the Assert class in all of your code base before compilation.

    Another approach would be to make a test project that targets your application and within JUnit tests actually calls the Assert on the areas which you want to make sure work. I personally like this approach because it is a nice and clean separation of test and application.

    If you are just worried about the having an if-statement everywhere, then just wrap Assert with your own class, DebuggableAssert which does that check before each call to Assert.X. It will be sort of less performant because of the method entry/exit and the conditionals but if you can maintain your code better then it might be worth it.

提交回复
热议问题