StrutsSpringJUnit4TestCase freemarker 结果 "Template not found for name"
StrutsSpringJUnit4TestCase freemarker result "Template not found for name"
我们一直在尝试将 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.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.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.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)
发生这种情况是因为在 MockServletContext
中使用的 spring 的 org.springframework.core.io.DefaultResourceLoader
不会像您在 [=13] 中使用的那样加载 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);
}
我们一直在尝试将 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.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.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.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)
发生这种情况是因为在 MockServletContext
中使用的 spring 的 org.springframework.core.io.DefaultResourceLoader
不会像您在 [=13] 中使用的那样加载 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);
}