簡體   English   中英

使用 Spring 為 JUnit 測試配置 Velocity 時遇到問題

[英]Trouble configuring Velocity with Spring for a JUnit test

我正在使用 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);

您可以使用 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);

關於什么:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM