如何从jUnit测试中启动maven过滤



情况:我有一个类MyController与一些外部WebServices工作。

public class MyController {
    private String integrationWebServiceURL;
}

在描述符(applicationContext.xml)

配置控制器bean期间传递该类web服务URL
<bean id="myController"  class="com.mypath.MyController">
    <property name="integrationWebServiceURL" value="${integration.web.service.url}"/>
</bean>

该值是动态的,实际值存储在属性文件application.properties

integration.web.service.url=${pom.integration.web.service.url}

但这不是结束-实际值存储在maven项目文件(pom.xml)中,filter =true。

<pom.integration.web.service.url>http://mywebservices.com</pom.integration.web.service.url>

所以,当我们使用mvn安装测试值从pom.xml复制到适当的占位符在应用程序中。属性,然后测试我的类工作得很好。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/applicationContext.xml"})
public class MyControllerTest {
}

问题:我需要从IDE启动我的测试,以便能够使用不同的设置并使用IDE的调试功能。但是,如果我只是从IDE中启动这个测试,而没有初步的Maven构建,那么我的web服务地址将简单地从应用程序中获取。属性和等于"${pom.integration.web.service.url}"(例如,在测试之前,Maven的过滤过程不工作)。我如何调整Maven、Spring或jUnit来从pom.xml中提取我的值?

注意:我知道我可以简单地在应用程序中显式设置这个值。property或applicationContext.xml文件,由Test-class使用,但我需要从pom.xml中提取这些值。

直接运行maven,如:

mvn test

那么所有的变量都应该进行过滤。您可以为特定的属性文件使用testResources。或者applicationContext-test.xml

最好的解决方案是使用支持maven的IDE,并且在必须构建源代码时运行mvn复制资源。对于Eclipse,请尝试m2e,对于IDEA, Maven插件也应该这样做。

如果由于某些原因不能选择,您可以手动运行目标,例如在普通测试代码中的静态代码块中(因此它总是只执行一次):

static {
    Process p = Runtime.getRuntime().exec( "mvn copy-resources" );
    IOUtils.copy( p.getInputStream(), System.out );
    p.waitFor();
}

最新更新