How to use VisibleForTesting for pure JUnit tests

后端 未结 4 788
南旧
南旧 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条回答
  •  星月不相逢
    2021-02-01 03:49

    The Tag itself helps with the linter to identify unwanted access.

    To lower the risk of use it directly, add this methods as internal in Kotlin or protected in Java instead of public and with that only the tests or classes that are in the same package will be able to access that method.

    Java:

    @VisibleForTesting
    protected Address address() {
      return mAddress;
    }
    

    Kotlin:

    @VisibleForTesting
    internal fun address(): Address {
      return address;
    }
    

提交回复
热议问题