Java – how to forge initialcontext using default constructor
All,
I try to do some unit tests in some old java code (no interface, no abstraction, etc.)
This is a servlet using ServletContext (I assume it is set by Tomcat), which is on the web xml / context. The database information is set in the XML file Now, I've figured out how to make a fake ServletContext, but the code has
InitialContext _ic = new InitialContext();
In all places (so you can't replace it) I need to find a default initialcontext () that can execute_ ic. Lookup (VAL) without throwing an exception
I suppose there are some methods, context XML is loaded, but how does this magic work? I draw a blank What does anyone think?
Solution
You can use powermock to simulate the construction of initialcontext and control its behavior The constructor simulation record is here
Powermock testing can be messy and complex, and refactoring is usually a better choice