Reputation: 453
I have an outer class, Outer, and an inner class, Inner. For testing a public method, outerMethod(), in Outer I need to create an instance of an Inner, which will be stored in a list called inners. The inner class is a modified version of another class, Other.
To overview:
class Other{
public Other(){
//doesnt matter actual implementation
}
}
class Outer{
private List<Inner> inners = new ArrayList<Inner>();
public Outer(){}
public void outerMethod(){}
private class Inner{
public Inner(Other other){}
}
}
My question is should I create a method like the following just for testing purposes.
public void createInnerInstance(Other other)
{
Inner inner = new Inner(other);
inners.add(inner);
}
Is there any other workaround for this? Now I can make Inner public but Outer is the only class that really uses Inner.
Regards
Upvotes: 0
Views: 134
Reputation: 17444
Changing code specifically for testing purposes is probably not a very good idea.
You've got two options - either to redesign your class as @bot suggests in comments, or to apply workarounds.
You could change field/inner class access modifiers and use them from your test instead. Take a look here, and here for implementation details.
Upvotes: 1