How to use VisibleForTesting for pure JUnit tests

后端 未结 4 791
南旧
南旧 2021-02-01 03:27

I´m running pure JUnit4 java tests over my pure java files on my project but I can\'t find a way to use @VisibleForTesting clearly without making the thing manually public.

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-01 03:35

    Make the method package-private and the test will be able to see it, if the test is in the corresponding test package (same package name as the production code).

    @VisibleForTesting
    Address getAddress() {
      return mAddress;
    }
    

    Also consider refactoring your code so you don't need to explicitly test a private method, try testing the behaviour of a public interface. Code that is hard to test can be an indication that improvements can be made to production code.

    The point of an annotation is that its convention and could be used in static code analysis, whereas a comment could not.

提交回复
热议问题