在Junit测试中使用来自WEB-INF的i18n资源

chz*_*gla 5 junit spring classpath

我想我的类路径设置遇到了错误.

我想测试一个国际化的网络应用程序,其中的messagesource定义如下:

<bean id="messageSource"
  class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basenames">
        <list>
            <value>/WEB-INF/i18n/errors</value>
            <value>/WEB-INF/i18n/messages</value>
            <value>/WEB-INF/i18n/links</value>
            <value>/WEB-INF/i18n/forms</value>
            <value>/WEB-INF/i18n/communication</value>
        </list>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

加载这些值在生产环境中完美运行.但是,在运行Junit Test时,它无法解析这些属性文件,因为它们不在类路径中.

但是,我希望它们不在类路径上,因为我可以利用我可以在属性文件中更改内容的功能,并立即反映在网站上: Since application servers typically cache all files loaded from the classpath, it is necessary to store resources somewhere else (for example, in the "WEB-INF" directory of a web app). Otherwise changes of files in the classpath will not be reflected in the application.

spring applicationContext位于那里:/src/main/resources/spring/applicationContext.xml 并使用这些注释加载到Junit测试中:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/spring/applicationContext.xml"})
Run Code Online (Sandbox Code Playgroud)

如何让Junit获取那些非类路径资源呢?属性文件已打开/src/main/webapp/WEB-INF/i18n/*

Junit:4.7.

春天:3.0.5.

chz*_*gla 6

我在此期间解决了我最初的问题.

马修的解决方案对我没有帮助 - 仍然是非常好的投入.因为我从未说过,他不知道我在我的项目中使用了maven.

但是,在maven中,我找到了解决问题的方法:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <skipTests>true</skipTests>
                <additionalClasspathElements>
                    <additionalClasspathElement>src\main\webapp\</additionalClasspathElement>
                </additionalClasspathElements>
            </configuration>
        </plugin>
Run Code Online (Sandbox Code Playgroud)

我配置了surefire-plugin以获取类路径的其他元素.