Couldn't get resource paths for class path resource

自作多情 提交于 2021-02-08 04:41:26

问题


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:

  1. 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

  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!