为什么ContextConfiguration位置在想法和日食方面有所不同

jak*_*kob 2 java unit-testing intellij-idea

在我的团队中,我们在Eclipse和Idea中都工作.这项工作非常好,除了一个我无法弄清楚如何解决的小问题.在我们的测试中设置ContextConfiguration位置并在Eclipse中运行它们时,一切都像魅力:

@Test(groups = { "database" })
@ContextConfiguration(locations = {" file:src/main/webapp/WEB-INF/applicationContext.xml" })
Run Code Online (Sandbox Code Playgroud)

但在我的想法环境中,我得到"找不到applicationContext"错误.我需要设置这样的位置(项目名称是服务):

@Test(groups = { "database" })
@ContextConfiguration(locations = {" file:services/src/main/webapp/WEB-INF/applicationContext.xml" })
Run Code Online (Sandbox Code Playgroud)

项目结构如下:parent.pom有两个子poms:services.pom和other.pom.从服务项目在终端中运行测试时,如下所示:

mvn -Dtest=com.mytest.service.somepackage.TheTest test 
Run Code Online (Sandbox Code Playgroud)

没有问题.我想,因为我的项目结构是父子双子,所以需要/ service(项目是通过指出父pom创建的).有没有办法来解决这个问题?你能帮我解决一下吗?谢谢

Arn*_*ter 5

您使用相对于当前工作目录的路径.Eclipse和Idea使用不同的目录.尝试使用类路径位置:

@ContextConfiguration(locations = {" classpath:/WEB-INF/applicationContext.xml" })
Run Code Online (Sandbox Code Playgroud)

但我不确定你的类路径配置.通常,src/main/webapp将复制到目标webapp目录.可能是您需要将其配置为包含目标webapp目录.

  • 很酷,这有一个修改,不得不添加以下我的pom找到它的上下文:<testResources> <testResource> <directory> src/main/webapp/WEB-INF </ directory> <includes> <include>*.xml </ include> </ includes> <excludes> <exclude> web.xml </ exclude> </ excludes> </ testResource> </ testResources> (2认同)