【问题标题】:Can't load messages.properties when running test on IntelliJ在 IntelliJ 上运行测试时无法加载 messages.properties
【发布时间】:2015-07-02 11:46:07
【问题描述】:

加载messages.proeprties的测试配置是

<bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource" id="messageSource" p:basenames="classpath:i18n/messages,classpath:i18n/application" p:fallbackToSystemLocale="false"/>

如果我在 Intellij(Junit 测试运行器)上运行单个测试,它将无法从 src/main/webapp/WEB-INF/i18n/messages.properties 加载 messages.properties

从控制台 (mvn -Dtest=blablatest test) 运行相同的测试有效。

为了让它在 Intellij 上运行,我必须将 messages.properties 复制到 src/test/resources/i18n/messages.properties

Update1“从控制台 (mvn -Dtest=blablatest test) 运行相同的测试有效。”
仅在先构建项目时才有效。

【问题讨论】:

  • src/main/webapp 不是类路径。你应该把它们放在src/main/resources 而不是classpath: 来接它们。
  • 如果您的测试目的是测试 messages.properties 本身(不太可能),您可以遵循 M.Deinum 建议。如果您的测试目的是测试需要 messages.properties 的 something 我宁愿添加 src/main/resources 作为我的测试的类路径,但我只有一组不同的 *.properties而是用于测试目的。
  • @medveshonok117 测试的目的是需要messages.properties。如果我把它放在src/main/resources 中,那么单元测试就可以了,但问题是默认情况下项目的消息属性是src/main/webapp/WEB-INF/i18n/messages.properties
  • 好的,所以我的意思是您希望您的测试从主类路径加载messages.properties:正确的方法是将您的“实时”和单元测试环境。
  • @medveshonok117 我不需要不同的消息来进行测试,为什么要在这种情况下使用单独的文件,放在测试资源中并维护两个文件?为什么这是正确的方法?

标签: java spring maven intellij-idea junit


【解决方案1】:

您可以添加 src/main/webapp/WEB-INF/i18n 来测试资源:文件 > 项目结构 ... > 项目设置 > 模块

【讨论】:

  • 谢谢。它以这种方式适用于 Intellij,但我意识到(参见更新)当我运行测试表单 maven 时它无法正常工作,例如mvn clean;mvn -Dtest=blablatest test
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-02
  • 2015-09-26
  • 1970-01-01
  • 1970-01-01
  • 2015-10-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多