簡體   English   中英

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

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

我正在 Eclipse 中編寫一個小的 Maven 應用程序。 我將一些屬性文件和我的應用程序上下文存儲在目錄 src/main/resources 中。

我現在想讓 Eclipse 使用目錄 src/test/resources 中的屬性。 所以當我在 Eclipse 中運行和調試程序時,應該使用這些測試屬性。

你知道我怎么能做到這一點嗎?

嘗試這個:

  1. 轉到“運行->運行配置...”(在調試“運行->調試配置...”的情況下)
  2. 您使用的打開運行(調試)配置
  3. 打開“類路徑”選項卡
  4. 選擇“用戶條目”,然后單擊右側的“高級...”
  5. 在打開的窗口中選擇“添加文件夾”,指向你的 src/test/resources
  6. 此文件夾將出現在“用戶條目”下,然后您應該將其向上移動以使其成為類路徑中的第一個

無論您使用Maven Eclipse Plugin還是m2eclipsesrc/test/resources在類路徑(更准確地說,它們的輸出目錄)上都位於src/main/resources之前。 換句話說,沒有什么可做的,事情就像在命令行上一樣。

使用測試覆蓋(例如 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>

在您的測試中,請確保首先導入它:

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

現在將所有測試屬性放在src/test/resources/META-INF/spring/testproperties/

您還必須確保main占位符配置器永遠不會看到testproperties ,例如這是我的:

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

它不使用雙 * 通配符,因此只會查看該目錄。

我使用上述方法取得了巨大成功。

在 eclipse 2021 中測試。轉到 Run As Configuration(或調試)並轉到 Classpath 選項卡。 現在您可以選中或取消選中,這取決於您是否要使用 /src/main/resources 或 /src/test/resources,如下所示:

在此處輸入圖片說明

最后,應用並運行你的程序

暫無
暫無

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

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