There are different opinions on the importance of testing private methods, for example here and here . I personally think that this makes sense, the question is how to do it right. In C ++ you can use #define to hack or make a test friend class, in C # there is an InternalsVisibleToAttribute , but in Java we either need to use reflection , or make them "visible for testing" and annotate them as such to make it clear. The flaws of both should be clear enough.
I think there should be something better. Beginning with
public class Something { private int internalSecret() { return 43; } }
it would be nice to be able to call private methods in test code, for example
@MakeVisibleForTesting Something something = new Something(); Assert.assertEquals(43, something.internalSecret());
Here, the annotation tacitly converts all calls to private something methods using reflection. I wonder if Lombok can (and ask the authors).
It is possible that the execution of this magic turned out to be too complicated, and in any case it will take some time, so I am looking for an alternative. Perhaps annotating the test class with something like @Decapsulate and using the annotation handler to create the Decapsulated_Something class, similar to
public class Decapsulated_Something { public Decapsulated_Something(Something delegate) { this.delegate = delegate } public boolean internalSecret() {
which would allow to use
Decapsulated_Something something = new Decapsulated_Something(new Something()); Assert.assertEquals(43, something.internalSecret());
I don't have much experience working with annotation processing, so I ask first:
- How hard is it to implement this?
- What did I forget?
- What do you think of this in general?
maaartinus
source share