StrutsSpringJUnit4TestCase freemarker 结果“找不到名称的模板”

Posted

技术标签:

【中文标题】StrutsSpringJUnit4TestCase freemarker 结果“找不到名称的模板”【英文标题】:StrutsSpringJUnit4TestCase freemarker result "Template not found for name" 【发布时间】:2015-12-16 21:28:03 【问题描述】:

我们一直在尝试将StrutsSpringJunit4TestCase 用于 Struts2,但是当我们运行基本测试时,我们会收到一个我们知道存在的文件的"Template not found for name" 错误:

SEVERE:   [29:39.103] Template not found for name "/WEB-INF/content/errors/error.ftl".
The name was interpreted by this TemplateLoader: org.apache.struts2.views.freemarker.FreemarkerThemeTemplateLoader@7c060680.

调度程序#sendError:500

似乎测试用例没有在src/main/webapp/ 中找到文件,因为适当的文件确实存在于:`src/main/webapp/WEB-INF/content/errors/error.ftl

我们需要做些什么来指导 Struts2 或测试在哪里找到 freemarker 模板?

样本测试:

@ContextConfiguration(classes = TdarWebAppConfiguration.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@Transactional
public class IndexControllerITCase extends StrutsSpringJUnit4TestCase<HomepageSupportingController> 
    protected final Logger logger = LoggerFactory.getLogger(getClass());

    @Test
    public void test() throws Exception 
        ActionProxy proxy = getActionProxy("/robots");
        HomepageSupportingController myAction = (HomepageSupportingController) proxy.getAction();
        logger.debug(executeAction("/robots"));
        String execute = myAction.execute();
        finishExecution();
        logger.debug(response.getContentType());
        logger.debug(response.getContentAsString());
        assertEquals("Error", ActionSupport.SUCCESS, execute);
    

全栈跟踪:

freemarker.template.TemplateNotFoundException: Template not found for name "/WEB-INF/content/errors/error.ftl".
The name was interpreted by this TemplateLoader: org.apache.struts2.views.freemarker.FreemarkerThemeTemplateLoader@7c060680.
    at freemarker.template.Configuration.getTemplate(Configuration.java:1833)
    at freemarker.template.Configuration.getTemplate(Configuration.java:1655)
    at org.apache.struts2.views.freemarker.FreemarkerResult.doExecute(FreemarkerResult.java:181)
    at org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:191)
    at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:369)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:273)
    at org.tdar.struts.interceptor.HttpsInterceptor.doHttpIntercept(HttpsInterceptor.java:52)
    at org.tdar.struts.interceptor.HttpsInterceptor.intercept(HttpsInterceptor.java:34)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
    at org.tdar.struts.interceptor.CacheControlInterceptor.intercept(CacheControlInterceptor.java:40)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
    at org.tdar.struts.interceptor.SessionSecurityInterceptor.intercept(SessionSecurityInterceptor.java:85)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
    at org.tdar.struts.interceptor.ActivityLoggingInterceptor.intercept(ActivityLoggingInterceptor.java:69)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
    at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:189)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
    at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:564)
    at org.apache.struts2.StrutsJUnit4TestCase.executeAction(StrutsJUnit4TestCase.java:140)
    at org.tdar.struts.action.IndexControllerITCase.test(IndexControllerITCase.java:31)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:85)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:86)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:241)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

【问题讨论】:

哪个 S2 版本? IndexControllerITCase.java:31 是哪一行?显示您尝试执行的操作配置。 Struts2 版本:2.3.24;第 31 行是`HomepageSupportingController myAction = (HomepageSupportingController) proxy.getAction(); ` 我们甚至尝试编写自己的 FreemarkerConfiguration(以提供更好的供应路径,但没有进一步发展)。谢谢 【参考方案1】:

发生这种情况是因为MockServletContext 中使用的弹簧org.springframework.core.io.DefaultResourceLoader 不会像您用于freemarker 结果配置那样加载这样的url-s,例如/WEB-INF/content/errors/error.ftl.

您可以通过覆盖测试中的initServletMockObjects 方法来提供不同的资源加载器。

@Override
protected void initServletMockObjects() 
    servletContext = new MockServletContext(your_custom_resource_loader);
    response = new MockHttpServletResponse();
    request = new MockHttpServletRequest();
    pageContext = new MockPageContext(servletContext, request, response);

【讨论】:

进行以下更改似乎有效:servletContext = new MockServletContext(applicationContext); 你是否碰巧在网上有一个完整工作测试的参考?我们似乎只是在配置问题之后遇到配置问题,并且几乎没有文档。 (谢谢) 完全工作的测试...有什么依赖/配置?这个问题是关于使用freemarker 结果测试操作执行。我过去用tiles 的结果做了一些S2 测试,但它们不公开。如果您有更多问题,您可以提出一个新问题,我会看看。祝你好运。

以上是关于StrutsSpringJUnit4TestCase freemarker 结果“找不到名称的模板”的主要内容,如果未能解决你的问题,请参考以下文章