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

标签 java spring junit velocity

我使用的是 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 。我在 src/main/webapp/WEB-INF/velocity.properties 中有我的 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);

最佳答案

关于:

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

关于java - 使用 Spring 配置 Velocity 以进行 JUnit 测试时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6630784/

相关文章:

java - 在 ssl (ldaps) 的支持下连接 Activity 目录

java - 通过接口(interface)公开单例类的正确方法

java - 测试驱动开发 - 如何在不存在任何实现代码之前编写测试

junit - 如何使用 junit 的 assertThat 检查列表是否仅包含某些不相关的类类型?

java - 如何模拟从抽象类继承的 protected 子类方法?

java - 使用坐标存储系统创建稀疏矩阵?

java - 你如何找到整数数组中第二大的数字?

java - 可以修改第三方 ActiveX 控件中的枚举吗?

java - 星号-Java AGI。 DefaultAgiServer 在启动方法运行时卡住

java - 启动 ApplicationContext 时出错。要显示条件报告,请在启用 'debug' 的情况下重新运行应用程序