开发者

Couldn't get resource paths for class path resource

开发者 https://www.devze.com 2023-04-12 22:10 出处:网络
all, I am puzzled about the struts2 action unit test import org.apache.struts2.StrutsSpringTestCase; import org.juni开发者_开发百科t.Test;

all, I am puzzled about the struts2 action unit test

import org.apache.struts2.StrutsSpringTestCase;
import org.juni开发者_开发百科t.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


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";
}


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) { .... }


You may add a line as follows to struts.xml to work around it.

<constant name="struts.convention.result.path" value="/your/path" /> 
0

精彩评论

暂无评论...
验证码 换一张
取 消