2012-04-11 21 views
33

看來,問題舊如舊世界,但我仍然無法找出解決方案..測試與Spring和Maven:ApplicationContext的

我試圖運行簡單的測試:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = {"/applicationContext.xml", "/PersonsPopulateTest-context.xml"}) 
@Transactional 
public class PersonsPopulateTest { 

文件所在位置:

src 
    main 
     resources 
      applicationContext.xml 

src   
    test 
     resources 
      PersonsPopulateTest-context.xml 

所以建立後,這些文件在目標/班目標/測試類

但MVN測試命令還說:無法加載的ApplicationContext

官方文件說什麼:

@RunWith(SpringJUnit4ClassRunner.class) 
// ApplicationContext will be loaded from "/applicationContext.xml" and "/applicationContext-test.xml" 
// in the root of the classpath 
@ContextConfiguration(locations={"/applicationContext.xml", "/applicationContext-test.xml"}) 
public class MyTest { 
    // class body... 
} 

我哪裏出錯了?

感謝, Vlaidimir

更新。 surefire-reports:

java.lang.IllegalStateException: Failed to load ApplicationContext 
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157) 
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109) 
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75) 
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321) 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211) 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288) 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:290) 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) 
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:236) 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) 
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53) 
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123) 
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
at java.lang.reflect.Method.invoke(Method.java:597) 
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164) 
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110) 
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175) 
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107) 
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68) 
Caused by: java.lang.IllegalArgumentException: Can not load an ApplicationContext with a NULL 'contextLoader'. Consider annotating your test class with @ContextConfiguration. 
at org.springframework.util.Assert.notNull(Assert.java:112) 
at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:117) 
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:148) 
... 30 more 
+0

你能用stacktrace顯示確切的錯誤信息嗎? – axtavt 2012-04-11 10:43:45

+3

我認爲'applicationContext.xml'必須位於你的'src/test/resources'中。 AFAIK測試不能訪問'src/main'文件夾。 – 2012-04-11 10:44:52

+5

@Maroe:不,測試可以訪問主類路徑。如果他們不能,他們如何加載被測試的類? – axtavt 2012-04-11 10:52:19

回答

10

我覺得Maven根本沒有包含main/resources中的XML文件。

您可以嘗試明確指定要包含在pom.xml中的內容。

讓我知道如果下面的配置已經工作:

<!-- Add this directly after the <build>-opening tag --> 
    <resources> 
     <resource> 
      <filtering>true</filtering> 
      <directory>src/test/resources</directory> 
      <includes> 
       <include>**/*.properties</include> 
      </includes> 
      <excludes> 
       <exclude>**/*local.properties</exclude> 
      </excludes> 
     </resource> 
     <resource> 
      <directory>src/main/resources</directory> 
      <includes> 
       <include>**/*.properties</include> 
       <include>**/*.xml</include> 
      </includes> 
     </resource> 
    </resources> 

這是我在你的情況下使用。如果你沒有包含屬性文件,你可以編輯它。

+0

默認情況下,Maven包含所有來自'src/test/resources'和'src/main/resources'的所有內容。 'package'之後你可以很容易地檢查目標文件。所以顯然問題不在於此。我認爲Prasanna Talakanti在評論中指出了錯誤的上下文聲明。 – GKislin 2016-06-21 21:22:06

9

我的測試環境文件在src \ test \ resources \ spring文件夾下。 我管理符合加載上下文

@ContextConfiguration(locations={"classpath:**/test-context.xml"}) 

但是引用(在測試context.xml中)到應用程序的context.xml其是下SRC \主\資源\彈簧夾失敗

我設法通過創建測試類的ClassPathXmlApplicationContext與

ClassPathXmlApplicationContext appContext=new ClassPathXmlApplicationContext(new String[]{"classpath:spring/application-context.xml","classpath:spring/model-context.xml"}); 

加載應用上下文讓我知道這是否有助於或可能創建的任何其他問題。

+1

我設法將test-context.xml中的application-context.xml引用到 2012-04-12 12:34:35

2
<!-- Add this directly after the <build>-opening tag in the your pom--> 
      <testResources> 
       <testResource> 
        <directory>src/test/resources</directory> 
        <filtering>true</filtering> 
        <includes> 
         <include>**/*.xml</include> 
         <include>**/*.properties</include> 
        </includes> 
       </testResource> 
      </testResources> 
0

我有同樣的問題,所有的文件被成功複製到目標/班目標/測試類,仍然春天無法找到他們。 其實在我的情況下,問題是在啓用Compile on Save選項:當我明確指定版本Maven的萬無一失,插件Maven的資源 - 插件POM

+0

你設置了哪些版本?我嘗試設置這個插件的所有版本,但它仍然不起作用 – herau 2014-06-13 08:23:13

0

修訂

問題消失。我使用Netbeans,並且該選項設置爲For test execution only。該值編譯已更改的文件並用新文件替換資源。但是,由於另外使用應用程序資源來測試資源,For test execution onlytarget文件夾中產生錯誤生成的資源。

更改Compile on Save=For test execution only

Compile on Save=Disable解決問題。

下面的文本也是正確的,但有時不起作用。它只在我重新啓動Netbeans IDE之前工作。但是,問題原因的詳細信息是正確的,因此我寧願離開測試。


OLD: 在我的情況我在src/main/resourcesappContext-test.xml。 當我更改任何代碼並啓動一個單元測試(不是全部)時,它會重新編譯並正確執行。但是如果我再次啓動相同的單元測試,則會失敗,並顯示java.lang.IllegalStateException: Failed to load ApplicationContext

我已經改變pom.xml

<build> 
    <resources> 
     <resource> 
      <directory>${project.basedir}/src/main/resources</directory> 
      <filtering>true</filtering> 
     </resource> 
    </resources> 
    <testResources> 
     <testResource> 
      <directory>${project.basedir}/src/test/java/resources</directory> 
      <filtering>true</filtering> 
     </testResource> 
    </testResources> 
</build> 

<build> 
    <resources> 
     <resource> 
      <directory>${project.basedir}/src/main/resources</directory> 
      <filtering>true</filtering> 
     </resource> 
    </resources> 
    <testResources> 
     <testResource> 
      <directory>${project.basedir}/src/main/resources</directory> 
      <filtering>true</filtering> 
     </testResource> 
     <testResource> 
      <directory>${project.basedir}/src/test/java/resources</directory> 
      <filtering>true</filtering> 
     </testResource> 
    </testResources> 
</build> 

,現在一切工作的罰款。

錯誤是由於appContext-test.xml使用src/main/resources/my.properties文件,像

database.url = ${database.url} 
database.username = ${database.username} 
database.password = ${database.password} 

很多變量都在構建過程中填補。但是,如果跳過testResource中的src/main/resources,則將my.properties按原樣添加到target/classes/my.properties,即,沒有替代。這個文件當然會打破上下文。

PS:你可以刪除${project.basedir}/ - 這是我的習慣。

2

您的應用程序上下文必須包含在類路徑中,並把*:

@ContextConfiguration(locations = { "classpath:*/application-context.xml" }) 
+0

找到你的xml的最佳方法 – swapyonubuntu 2016-04-05 08:45:50

3

我認爲最好的做法是把你的應用程序上下文文件在src /測試/資源測試PersonsPopulateTest-context.xml中。該文件將被複制到目標/測試類,你可以參考它在您的測試類,如下:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = {"classpath:**/PersonsPopulateTest-context.xml"}) 
@Transactional 
public class PersonsPopulateTest { 

} 

如果你仍然想參考的applicationContext。xml下的src /主要/資源,那麼你必須包括它如下:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = {"file:src/main/resources/applicationContext.xml"}) 
@Transactional 
public class PersonsPopulateTest { 

} 

它爲我工作。

1

出於某種原因,我有同樣的問題,它的工作,當我跑行家測試與JDK6,而不是JDK8(對我來說這是一個遺留應用程序)

如果它可以幫助任何人。

0

我面臨同樣的問題。對我來說,測試通過eclipse成功運行。然而,當我跑mvn test,這是給我的錯誤:無法加載的ApplicationContext

Fix: Added the src/test/resources directory to classpath of surefire plugin as-

<plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <version>2.10</version> 
      <configuration> 
       <additionalClasspathElements> 
        <additionalClasspathElement>${project.basedir}/src/test/resources</additionalClasspathElement> 
       </additionalClasspathElements>  
      </configuration> 

Adding classpath to SureFire

希望它能幫助。