使用 Spring 配置 Velocity 进行 JUnit 测试时遇到问题

发布于 2024-11-19 07:31:28 字数 2532 浏览 2 评论 0原文

我正在使用 Spring 3.0.5、Maven 3.0.3、JUnit 4 和 Velocity 1.6.2。当我运行 JUnit 测试时,它无法找到我创建的速度模板。我得到的错误是:

2011-07-08 16:16:19,575 [main] ERROR test.com.myco.systems.leadsmonitor.quartz.TestReportQuartzJob  - JobExecutionException
org.quartz.JobExecutionException: Exception in execute [See nested exception: org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'email_template.vm']
at com.myco.systems.leadsmonitor.quartz.ReportQuartzJob.executeInternal(ReportQuartzJob.java:55)
at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:86)
at test.com.myco.systems.leadsmonitor.quartz.TestReportQuartzJob.testJob(TestReportQuartzJob.java:82)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)

我将有问题的模板复制到 src/main/resources 和 src/test/resources 。我的velocity.properties 文件位于src/main/webapp/WEB-INF/velocity.properties 中。其内容是:

# uncomment the next two lines to load templates from the
# classpath (WEB-INF/classes)
resource.loader=class
class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader

# comment the next two lines to stop loading templates from the
# file system
#resource.loader=file
#file.resource.loader.class=org.apache.velocity.runtime.resource.loader.FileResourceLoader


# additional config for file system loader only.. tell Velocity where the root
# directory is for template loading.  You can define multiple root directories
# if you wish, I just use the one here.  See the text below for a note about
# the ${webapp.root}
#file.resource.loader.path=${webapp.root}/WEB-INF/velocity


# caching should be 'true' in production systems, 'false' is a development
# setting only.  Change to 'class.resource.loader.cache=false' for classpath
# loading
file.resource.loader.cache=false

这是我尝试填充模板的方式。 JUnit 测试调用以下代码:

    /*
     * first, get and initialize an engine
     */
    VelocityEngine ve = new VelocityEngine();
    ve.init();

    /*
     * Build the template
     */
    final String emailBody = VelocityEngineUtils.mergeTemplateIntoString(ve, "email_template.vm", map);

    sendMessage(subject, emailBody);

I'm using Spring 3.0.5, Maven 3.0.3, JUnit 4 and Velocity 1.6.2. When I run my JUnit test, it is not able to locate a velocity template I created. The error I get is:

2011-07-08 16:16:19,575 [main] ERROR test.com.myco.systems.leadsmonitor.quartz.TestReportQuartzJob  - JobExecutionException
org.quartz.JobExecutionException: Exception in execute [See nested exception: org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'email_template.vm']
at com.myco.systems.leadsmonitor.quartz.ReportQuartzJob.executeInternal(ReportQuartzJob.java:55)
at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:86)
at test.com.myco.systems.leadsmonitor.quartz.TestReportQuartzJob.testJob(TestReportQuartzJob.java:82)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)

I copied the template in question to both src/main/resources and src/test/resources . I have my velocity.properties file in src/main/webapp/WEB-INF/velocity.properties. Its contents are:

# uncomment the next two lines to load templates from the
# classpath (WEB-INF/classes)
resource.loader=class
class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader

# comment the next two lines to stop loading templates from the
# file system
#resource.loader=file
#file.resource.loader.class=org.apache.velocity.runtime.resource.loader.FileResourceLoader


# additional config for file system loader only.. tell Velocity where the root
# directory is for template loading.  You can define multiple root directories
# if you wish, I just use the one here.  See the text below for a note about
# the ${webapp.root}
#file.resource.loader.path=${webapp.root}/WEB-INF/velocity


# caching should be 'true' in production systems, 'false' is a development
# setting only.  Change to 'class.resource.loader.cache=false' for classpath
# loading
file.resource.loader.cache=false

Here is how I'm trying to populate my template. The JUnit test invokes this code:

    /*
     * first, get and initialize an engine
     */
    VelocityEngine ve = new VelocityEngine();
    ve.init();

    /*
     * Build the template
     */
    final String emailBody = VelocityEngineUtils.mergeTemplateIntoString(ve, "email_template.vm", map);

    sendMessage(subject, emailBody);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃不饱 2024-11-26 07:31:28

怎么样:

VelocityEngine ve = new VelocityEngine();
ve.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, "./src/test/resources/");
ve.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_CACHE, false);
ve.init();

What about:

VelocityEngine ve = new VelocityEngine();
ve.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, "./src/test/resources/");
ve.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_CACHE, false);
ve.init();
云柯 2024-11-26 07:31:28

您可以使用 VelocityEngineFactoryBean 创建 VelocityEngine,如下所示:

VelocityEngineFactoryBean vefb = new VelocityEngineFactoryBean();
    Map<String, Object> velocityPropertiesMap = new HashMap<String, Object>();
    velocityPropertiesMap.put("resource.loader", "class");
    velocityPropertiesMap.put("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
    vefb.setVelocityPropertiesMap(velocityPropertiesMap);
    velocityEngine = vefb.createVelocityEngine();

那么以下语句应该有效

VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, template, model);

You can create a VelocityEngine using the VelocityEngineFactoryBean as follows:

VelocityEngineFactoryBean vefb = new VelocityEngineFactoryBean();
    Map<String, Object> velocityPropertiesMap = new HashMap<String, Object>();
    velocityPropertiesMap.put("resource.loader", "class");
    velocityPropertiesMap.put("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
    vefb.setVelocityPropertiesMap(velocityPropertiesMap);
    velocityEngine = vefb.createVelocityEngine();

Then the following statement should work

VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, template, model);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文