2011-10-25 36 views
2

情况:我有一个MyController类,它与一些外部WebServices一起工作。如何从jUnit测试中启动maven过滤?

public class MyController { 
    private String integrationWebServiceURL; 
} 

这个类的web服务URL在描述符配置控制器豆(的applicationContext.xml)期间传递

<bean id="myController" class="com.mypath.MyController"> 
    <property name="integrationWebServiceURL" value="${integration.web.service.url}"/> 
</bean> 

值是动态的,实际值被存储在属性文件中应用。性能

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

但它不是终点 - 真正的VA lue存储在maven项目文件中(pom.xml),其中filtering = true。

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

所以,当我们用MVN从pom.xml的安装测试值复制到适当的application.properties占位符,然后测试我的类的工作就好了。

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

} 

问题:我需要从IDE启动我的测试,能够与不同的设置,以发挥和使用IDE的调试功能。但是如果我简单地从IDE开始这个测试,而没有初步的Maven构建 - 比我的web服务地址将简单地从application.properties获取并且等于“$ {pom.integration.web.service.url}”(例如,Maven过滤的过程不会测试前不工作)。如何调整Maven,Spring或jUnit以从pom.xml中提取我的值?

注意:我知道我可以简单地在由test-class使用的application.properties或applicationContext.xml文件中明确设置此值,但我需要从pom.xml中提取此值。

+0

如果您使用Eclipse,你尝试过M2Eclipse或M2E插件吗? – Ralph

+0

为什么你需要从POM中获得价值?或者说,我的意思是,为什么POM是一个适合存储这些信息的地方?我建议将这些数据保存在可交付工件外侧的属性文件中,并仅在运行时访问它。然后,您可以获得属性文件的一个版本,仅在测试类路径中提供,该文件特定于测试。 –

+0

@Ralph IntelliJ IDEA是我的IDE。 – dim1902

回答

0

只需使用Maven的运行,如:

mvn test 

则应全部用POM变量过滤去。 您可以拥有特定属性文件的testResources。或者是一个applicationContext-test.xml。

+0

我认为OP的需求是让IDE在从IDE进行clean + build(即非Maven构建)之后进行测试。 –

0

最好的解决方案是使用Maven感知的IDE,并且在源文件必须被创建时运行mvn copy-resources。对于Eclipse,请尝试m2e,对于IDEA,Maven插件也应该这样做。

如果不是出于某种原因的选项,您可以手动在通用测试代码的静态代码块运行目标,例如(所以它总是执行一次):

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