简体   繁体   English

让 Eclipse 使用 src/test/resources 而不是 src/main/resources

[英]Make Eclipse use src/test/resources instead of src/main/resources

I'm writing a little Maven application in Eclipse.我正在 Eclipse 中编写一个小的 Maven 应用程序。 I store some property files and my application context in the directory src/main/resources.我将一些属性文件和我的应用程序上下文存储在目录 src/main/resources 中。

I now want to make Eclipse use properties in the directory src/test/resources.我现在想让 Eclipse 使用目录 src/test/resources 中的属性。 So when I run and debug the program in Eclipse, these test properties should be used.所以当我在 Eclipse 中运行和调试程序时,应该使用这些测试属性。

Do you know how I could make that happen?你知道我怎么能做到这一点吗?

Try this:尝试这个:

  1. Go to "Run->Run configurations..." (in case of debug "Run->Debug configurations...")转到“运行->运行配置...”(在调试“运行->调试配置...”的情况下)
  2. Open Run (Debug) configuration which you use您使用的打开运行(调试)配置
  3. Open "Classpath" tab打开“类路径”选项卡
  4. Select "User Entries" and click "Advanced..." on the right选择“用户条目”,然后单击右侧的“高级...”
  5. In the opened window select "Add folder", point to your src/test/resources在打开的窗口中选择“添加文件夹”,指向你的 src/test/resources
  6. This folder will appear under "User entries", then you should move it up to make it the first on your classpath此文件夹将出现在“用户条目”下,然后您应该将其向上移动以使其成为类路径中的第一个

Whether you use the Maven Eclipse Plugin or m2eclipse , src/test/resources precedes src/main/resources on the classpath (more precisely, their output directories).无论您使用Maven Eclipse Plugin还是m2eclipsesrc/test/resources在类路径(更准确地说,它们的输出目录)上都位于src/main/resources之前。 In other words, there is nothing to do, things just works as on the command line.换句话说,没有什么可做的,事情就像在命令行上一样。

Use a test override (eg testOverrides.xml):使用测试覆盖(例如 testOverrides.xml):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!--  this file need to be imported before other contexts to ensure the test properties take effect -->

    <context:property-placeholder location="classpath*:META-INF/spring/testproperties/*.properties"/>

</beans>

In your tests, make sure it's imported first:在您的测试中,请确保首先导入它:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:META-INF/spring/testOverrides.xml","classpath*:META-INF/spring/applicationContext.xml"})
public class SomeTest { ... }

Now put all your test properties in src/test/resources/META-INF/spring/testproperties/ .现在将所有测试属性放在src/test/resources/META-INF/spring/testproperties/

You have to also make sure that the main placeholder configurer doesn't ever see testproperties , eg here's mine:您还必须确保main占位符配置器永远不会看到testproperties ,例如这是我的:

<context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>

It doesn't use the double-* wildcard, so will only look at that one directory.它不使用双 * 通配符,因此只会查看该目录。

I use the above method with great success.我使用上述方法取得了巨大成功。

Tested in eclipse 2021. Go to Run As Configuration (or debug) and go to Classpath tab.在 eclipse 2021 中测试。转到 Run As Configuration(或调试)并转到 Classpath 选项卡。 Now you could check or uncheck, depends if you want to use /src/main/resources or /src/test/resources, like so:现在您可以选中或取消选中,这取决于您是否要使用 /src/main/resources 或 /src/test/resources,如下所示:

在此处输入图片说明

Finaly, apply and run your program最后,应用并运行你的程序

暂无
暂无

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

相关问题 告诉gradle从src / test / resources而不是src / main / resources中选择persistence.xml - Tell gradle to pick persistence.xml from src/test/resources instead of src/main/resources Gradle和src / main / resources - Gradle and src/main/resources src / main / resources中的FileNotFoundException - FileNotFoundException in src/main/resources 应用程序(src / main)运行时,类路径中的src / test / resources中的文件是? - Are files in src/test/resources on classpath while application (src/main) is running? 如何从src / main / java中的src / test / resources中读取道具 - How to read props from src/test/resources in src/main/java 使用 src/test/resources 作为 gradle java 的目录 - Use src/test/resources as directory with gradle java 在 Eclipse 中为 spring 运行单元测试时将 src/main/resources 添加到类路径 - Add src/main/resources to classpath when running unit test in eclipse for spring 测试时将使用src / main / resources和src / test / resources目录中的哪个application.yml - Which application.yml that're in src/main/resources and src/test/resources directories will be used when testing Spring xml + JUnit-xml上下文文件在src / test / resources中不起作用,但是在src / main / resources中起作用 - Spring xml + JUnit - xml context file not working in src/test/resources but works in src/main/resources Springboot - application-integration.properties 在 src/main/resources 下工作,但不在 src/test/resources 下工作 - Springboot - application-integration.properties working under src/main/resources, but not under src/test/resources
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM