简体   繁体   English

JUnit中的Freemarker和Spring测试

[英]Freemarker and Spring in JUnit tests

As in many Java apps, we're using Freemarker in our app to render emails. 与许多Java应用程序一样,我们在应用程序中使用Freemarker来呈现电子邮件。 We've found that some of our templates weren't rendering exactly as we thought they would, and so we realized that we ought to write some unit tests for our template rendering. 我们发现我们的一些模板没有完全按照我们的想法进行渲染,因此我们意识到我们应该为模板渲染编写一些单元测试。 Well I set up the tests, and immediately received a FileNotFoundException: Template "my/template.ftl" not found. 好吧,我设置了测试,并立即收到FileNotFoundException:找不到模板“my / template.ftl”。

I figured that this must be both a solved problem with an easy solution. 我认为这必须是一个解决问题的简单解决方案。 That was many work hours ago, and I realized that I was wrong; 那是几个小时前的工作,我意识到我错了; from what I can tell, this is neither solved nor easy! 从我所知,这既不是解决也不容易!

Our app's setup is, I think, pretty standard. 我认为,我们的应用程序设置非常标准。 We have a Maven project, and store our Freemarker templates in src/main/webapp/WEB-INF/freemarker. 我们有一个Maven项目,并将我们的Freemarker模板存储在src / main / webapp / WEB-INF / freemarker中。 Although most of our app's functionality is initiated by Quartz jobs, we deploy it as a webapp. 虽然我们的大多数应用程序的功能都是由Quartz作业启动的,但我们将其部署为webapp。 Therefore the following configuration in our spring XML file is all that we need: 因此,我们的spring XML文件中的以下配置就是我们所需要的:

<bean id="freemarkerConfiguration"
    class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">
    <property name="templateLoaderPath" value="/WEB-INF/freemarker/" />
</bean>

Then in our code, we Autowire the bean in like so: 然后在我们的代码中,我们像这样自动装配bean:

@Autowired
protected Configuration freemarkerConfiguration;

and use it like so: 并像这样使用它:

Template template = freemarkerConfiguration.getTemplate("mail/job/detail.ftl");

That works great in the context of our deployed app, executing within a Quartz job. 这在我们部署的应用程序的上下文中非常有用,可以在Quartz作业中执行。 But we always get the template not found when running from within unit tests. 但是我们总是在单元测试中运行时找不到模板。

I tried many different approaches, none of which worked. 我尝试了很多不同的方法,但都没有。 The last involved creating my own Freemarker TemplateLoader implementation, and trying to get it to load the Freemarker template files form the classpath. 最后一个涉及创建我自己的Freemarker TemplateLoader实现,并试图让它从类路径加载Freemarker模板文件。 The problems with that approach are that a) the same classloading code that I successfully use elsewhere in tests would not find the template, b) I'm not sure all that is involved with correctly implementing a TemplateLoader, and c) this all just seems overly-complicated in general. 这种方法的问题是:a)我在测试中成功使用的相同类加载代码将找不到模板,b)我不确定正确实现TemplateLoader所涉及的所有内容,以及c)这一切似乎都是过于复杂一般。

I may continue this route, but first... am I missing something obvious? 我可以继续这条路线,但首先......我错过了一些明显的东西吗? One of Freemarker's benefits is that it doesn't require an HTTP context, so why would it be so difficult to use Freemarker outside of a web app context? Freemarker的一个好处是它不需要HTTP上下文,那么为什么在Web应用程序上下文之外使用Freemarker会如此困难呢?

Dave, 戴夫,

ftl files being used from location src/main/webapp/... are not in classpath. 从位置src/main/webapp/...使用的ftl文件不在类路径中。 And i beleive your testing class is on location src/test/... which will not have visibility into the ftl files (which are actually in src/main/webapp//WEB-INF/freemarker/mail/job/*.ftl ) and so you get template not found exception. 我相信你的测试类位于src/test/...这将无法查看ftl文件(实际上是在src/main/webapp//WEB-INF/freemarker/mail/job/*.ftl )所以你得到模板未找到异常。 If you want them to be on classpath, put the files in src/test/resources. 如果您希望它们位于类路径上,请将文件放在src / test / resources中。

I've found that using the MockMvc allows my unit tests to find the ftl templates in src/main/resources [target/classes]. 我发现使用MockMvc允许我的单元测试在src / main / resources [target / classes]中找到ftl模板。 But when I use the TestRestTemplate, it only sees the ftl templates in src/test/resources [target/test-classes]. 但是当我使用TestRestTemplate时,它只能看到src / test / resources [target / test-classes]中的ftl模板。

In my case I'm using @SpringBootTest. 在我的情况下,我正在使用@SpringBootTest。 Im not sure if that is relevant. 我不确定这是否相关。

Also, you can see better debug output if you turn up the level on these namespaces. 此外,如果调高这些命名空间的级别,您可以看到更好的调试输出。

logging.level.freemarker=TRACE
logging.level.org.springframework.ui.freemarker=DEBUG
logging.level.org.springframework.web.servlet.view.freemarker=DEBUG

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM