If I Have:
public abstract class A {
private Object target; // <- **IMPORTANT**
protected Blah blah // ... Not directly relevant to the question,
protected Blah blah2 // they are just a potential reason to extend
// test class ...?
public int func (){
int hello = Integer.parseInt(target.Fields.want2Change);
return hello;
}
}
I want to test Class A. Because It is abstract, I extend it with lets say Class ATest. There are other Fields but they are protected and this is reasonable.
My question is FROM THE CHILD CLASS, how can I access a parent PRIVATE field like target.
I am using a mix of mocking (Mockito), and Reflection.
I have also tried just using reflection and NOT extending the class.
This doesn't directly answer your question but is it possible to test that private fields state by exercising the public api of the parent class some how?
If you can do that then no need for reflection and this may be more maintainable long term.
If not then you must extend the class and use r election to get a handle on the private property.
See this thread: