Tesing an Action using this.getServlet
().getServletContext().getRealPath("/WEB-
INF/myfile.xml"); with a MockStrutsTestCase, I have
found out that the context directory of the
ServletContextSimulator was null by default. I had to
edite the source code (thanks open source !) to
understand it.
Here is the snippet I have used to initialize it in my
setUp() method :
// default configuration resource for the module.
String config = "/WEB-INF/struts-config.xml";
// get the absolute path of the struts-config.xml,
example : /C:/Projects/Web-App/WEB-INF/struts-
config.xml
String pathConfig = this.getClass().getResource
(config).getPath();
// extract the context directory, get /C:/Projects/Web-
App/
String pathContextDirectory = pathConfig.substring(0,
pathConfig.indexOf(config));
// Affect it to the MockStrutsTestCase
this.setContextDirectory(new File
(pathContextDirectory));
I think this code could be useful for others so I have
created a patch which includes it in the setUp() of
MockStrutsTestCase.
Important :
- I have not compiled the updated
MockStrutsTestCase.java If necessary, I can do it ! I
can also provide a war with a unit Test to proove that
everything works properly.
- I am not sure that it works with sub-applications.
Logged In: YES
user_id=88575
Probally not a good idea. This won't work with modules. It
also will ba a bad default for user's who don't have the
struts-config.xml in the context path for mock testing.