2012-05-08 32 views
0

我使用Maven與神火插件來運行TestNG的測試,它出現的上下文不加載,即使它是在類路徑中,試驗工作正常我的IDE中,但不是在maven中。TestNG的+彈簧+的Maven =無法通過延長AbstractTestNGSpringContextTests加載上下文

@ContextConfiguration(位置= 陣列( 「類路徑*:/ COM/gottex/gottware /服務器/ offlinepuwithdummydatafeed.xml」)) 我的XML是在src /測試/ COM/gottex/gottware /服務器

我父POM包含:

  <build> 
    <defaultGoal>package</defaultGoal> 
    <testResources> 
     <testResource> 
      <directory>src/test/java</directory> 
      <includes> 
       <include>**/*.xml</include> 
       <include>**/*.properties</include> 
      </includes> 
     </testResource> 
    </testResources> 

    <plugins> 

      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-surefire-plugin</artifactId> 
       <version>2.9</version> 
       <configuration> 
        <verbose>true</verbose> 
        <forkMode>never</forkMode> 

       </configuration> 

      </plugin> ... 

和我的孩子POM如果沒有依賴關係不包含任何..

+0

哪個Maven的版本?你怎麼樣? – khmarbaise

回答

0

長時間調試會話後,我終於能找到問題所在。它位於ClassLoader的水平,和調試

org.springframework.core.io.support.PathMatchingResourcePatternResolver 裏面的方法findAllClassPathResources

protected Resource[] findAllClassPathResources(String location) throws IOException { 
     String path = location; 
     if (path.startsWith("/")) { 
      path = path.substring(1); 
     } 
     Enumeration resourceUrls = getClassLoader().getResources(path); 
     Set<Resource> result = new LinkedHashSet<Resource>(16); 
     while (resourceUrls.hasMoreElements()) { 
      URL url = (URL) resourceUrls.nextElement(); 
      result.add(convertClassLoaderURL(url)); 
     } 
     return result.toArray(new Resource[result.size()]); 
    } 

這當上下文是完成時,它表現出自身加載併產生以下結果:

  • 當你運行裏面的想法時,ClassLoader是java.net.URLClassLoa der並且這個工作正常
  • 當通過Maven-Surefire插件進行runnigng測試時,使用了不同的類加載器:ClassRealm [plugin> org.apache.maven.plugins:maven-surefire-plugin:2.9, parent:sun.misc。啓動$ AppClassLoader @ 61ba34f2]

第二類加載器返回一個枚舉resourceUrls沒有「moreElements」,因此沒有加載的背景下,但沒有失敗是由框架拋出。