Thursday, 13 August 2015


I recently encountered the following annotation in the source code at work:
It is one I had not seen before. Below is an example of what it looked like.
void updateTaxation(TaxationData data) 
The annotation is part of the package.

It turns out that it is a Marker annotation, simply to communicate to (other) software designers that the access level modifier has been relaxed, to make the code more easy to test.

In the example above, the access level modifier is "default", while it should/could have been "private".

I am still not convinced this is a good thing, because you changed production code to fix a testing problem.

We could work around this issue by using Reflection to access private methods/members, but Reflection is very brittle, especially when dealing with living code. So, that doesn't appeal to me either.

The best way, is to either:
  • test the private method, by using the public api that uses it
  • if this not enough, you can move the private method into a Strategy object1, and test the Strategy object in isolation


[1] Wikipedia - Strategy Pattern
[2] StackOverflow -annotation to make a private method public only for test classes


  1. Do I understand @VisibleForTesting is just an indicator for other devs and we will have to manually change the access level in production code. I

    1. I agree, it is an abomination! Well-thought-out code is always verifiable without the need for such a thing. Thanks for this post :D

  2. Nice it seems to be good post... It will get readers engagement on the article since readers engagement plays an vital role in every blog.. i am expecting more updated posts from your hands.
    ios App Development Company
    Mobile App Development Company
    Best Mobile App Development Company