问题
all, I am puzzled about the struts2 action unit test
import org.apache.struts2.StrutsSpringTestCase;
import org.junit.Test;
import com.opensymphony.xwork2.ActionProxy;
public class TestLoginAction extends StrutsSpringTestCase {
@Test
public void testCheck() throws Exception {
ActionProxy proxy = null;
LoginAction test = null;
request.setParameter("username", "admin");
proxy = getActionProxy("/checkLogin");
test = (LoginAction) proxy.getAction();
String result = proxy.execute();
assertEquals("error", result);
assertEquals("admin", test.getUsername());
}
}
It throw the warnings and exceptions:
Couldn't get resource paths for class path resource [WEB-INF/jsp/] java.io.FileNotFoundException: class path resource [WEB-INF/jsp/] cannot be resolved to URL because it does not exist
回答1:
StrutsSpringTestCase is expecting the Spring configuration to be loaded from classpath:applicationContext.xml. You can override this behavior by adding this to your test action:
@Override
public String getContextLocations() {
return "path/to/your/TestLoginAction-context.xml";
}
回答2:
the reason of throwing the exception has been found, I use struts-convention to find my action classes,but ,this configuration is the base search path of jsp files,so of course the convention can't recognize the path as java class path,I will provide the two workaround here:
you can modify the configuration value "/WEB-INF/jsp" to the existing class path,such as "com.foo.bar" to make the convention resolve class path smoothly
rewrite the MockServletContext and swallow the throwing Exception public Set getResourcePaths(String path) { .... }
回答3:
You may add a line as follows to struts.xml to work around it.
<constant name="struts.convention.result.path" value="/your/path" />
来源:https://stackoverflow.com/questions/7734695/couldnt-get-resource-paths-for-class-path-resource