Make Eclipse use src/test/resources instead of src/main/resources

心不动则不痛 提交于 2019-12-10 03:19:32

问题


I'm writing a little Maven application in Eclipse. I store some property files and my application context in the directory src/main/resources.

I now want to make Eclipse use properties in the directory src/test/resources. So when I run and debug the program in Eclipse, these test properties should be used.

Do you know how I could make that happen?


回答1:


Whether you use the Maven Eclipse Plugin or m2eclipse, src/test/resources precedes src/main/resources on the classpath (more precisely, their output directories). In other words, there is nothing to do, things just works as on the command line.




回答2:


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
  6. This folder will appear under "User entries", then you should move it up to make it the first on your classpath



回答3:


Use a test override (e.g. 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/.

You have to also make sure that the main placeholder configurer doesn't ever see testproperties, e.g. here's mine:

<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.



来源:https://stackoverflow.com/questions/2912517/make-eclipse-use-src-test-resources-instead-of-src-main-resources

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!