Announcement Announcement Module
Collapse
No announcement yet.
Failed to load ApplicationContext at TestContext.getApplicationContext Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Failed to load ApplicationContext at TestContext.getApplicationContext

    Hi,

    I'm having a problem loading my test application context (same result with either STS m2eclipse or 'mvn clean test' from a shell). Does anyone see what's wrong in my setup? Thanks in advance for your suggestions!

    Here's my test (it looks odd because it just waits 1s before doing an assert - that's because it attempts to test the effect of a @Scheduled annotated processor which is instantiated by the application context):

    Code:
    package nl.rivm.vubis2warp.task.impl;
    
    import org.junit.Test;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
    import org.springframework.util.Assert;
    
    @ContextConfiguration(locations={"classpath:/nl/rivm/vubis2warp/task/impl/test-context.xml"})
    public class SchedulerImplTest extends AbstractJUnit4SpringContextTests {
    
    	@Test
    	public void test() throws InterruptedException {
    		Thread.sleep(1000);
    		Assert.notNull(this.getClass().getResource("/work/sent/210231001.xml"));
    	}
    
    }
    I verified that Maven's resource plugin copies my test-context.xml from /src/test/resources/nl/rivm/vubis2warp/task/impl to /target/test-classes/nl/rivm/vubis2warp/task/impl.

    However, Spring's TestContext fails to load the ApplicationContext, because class path resource [nl/rivm/vubis2warp/task/impl/test-context.xml] does not exist. Also tried moving test-context.xml to /src/test/resources/ and changing @ContextConfiguration as follows, but to no avail:

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

    Below is the stack trace plus some preliminary log output (org.springframework.test at debug level):

    Code:
    Running nl.rivm.vubis2warp.task.impl.SchedulerImplTest
    DEBUG: org.springframework.test.context.junit4.SpringJUnit4ClassRunner - SpringJUnit4ClassRunner constructor called with [class nl.rivm.vubis2warp.task.impl.SchedulerImplTest].
    DEBUG: org.springframework.test.context.support.DelegatingSmartContextLoader - Delegating to GenericXmlContextLoader to process context configuration [ContextConfigurationAttributes@1593ce6 declaringClass = 'nl.rivm.vubis2warp.task.impl.SchedulerImplTest', locations = '{classpath:/nl/rivm/vubis2warp/task/impl/test-context.xml}', classes = '{}', inheritLocations = true, contextLoaderClass = 'org.springframework.test.context.ContextLoader'].
    DEBUG: org.springframework.test.context.ContextLoaderUtils - Could not find an 'annotation declaring class' for annotation type [interface org.springframework.test.context.ActiveProfiles] and class [class nl.rivm.vubis2warp.task.impl.SchedulerImplTest]
    DEBUG: org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [nl.rivm.vubis2warp.task.impl.SchedulerImplTest]
    DEBUG: org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [nl.rivm.vubis2warp.task.impl.SchedulerImplTest]
    DEBUG: org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [nl.rivm.vubis2warp.task.impl.SchedulerImplTest]
    DEBUG: org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [nl.rivm.vubis2warp.task.impl.SchedulerImplTest]
    DEBUG: org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [nl.rivm.vubis2warp.task.impl.SchedulerImplTest]
    DEBUG: org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [nl.rivm.vubis2warp.task.impl.SchedulerImplTest]
    DEBUG: org.springframework.test.context.support.DependencyInjectionTestExecutionListener - Performing dependency injection for test context [[TestContext@13bedc4 testClass = SchedulerImplTest, testInstance = nl.rivm.vubis2warp.task.impl.SchedulerImplTest@176e9c0, testMethod = [null], testException = [null], mergedContextConfiguration = [MergedContextConfiguration@1ec4535 testClass = SchedulerImplTest, locations = '{classpath:/nl/rivm/vubis2warp/task/impl/test-context.xml}', classes = '{}', activeProfiles = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader']]].
    DEBUG: org.springframework.test.context.support.DelegatingSmartContextLoader - Delegating to GenericXmlContextLoader to load context from [MergedContextConfiguration@1ec4535 testClass = SchedulerImplTest, locations = '{classpath:/nl/rivm/vubis2warp/task/impl/test-context.xml}', classes = '{}', activeProfiles = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader'].
    DEBUG: org.springframework.test.context.support.AbstractGenericContextLoader - Loading ApplicationContext for merged context configuration [[MergedContextConfiguration@1ec4535 testClass = SchedulerImplTest, locations = '{classpath:/nl/rivm/vubis2warp/task/impl/test-context.xml}', classes = '{}', activeProfiles = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader']].
    ERROR: org.springframework.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@5f634c] to prepare test instance [nl.rivm.vubis2warp.task.impl.SchedulerImplTest@176e9c0]
    java.lang.IllegalStateException: Failed to load ApplicationContext
    	at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157)
    
    [...]
    
    	at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
    Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [nl/rivm/vubis2warp/task/impl/test-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [nl/rivm/vubis2warp/task/impl/test-context.xml] cannot be opened because it does not exist
    
    [...]
    
    Caused by: java.io.FileNotFoundException: class path resource [nl/rivm/vubis2warp/task/impl/test-context.xml] cannot be opened because it does not exist
    
    [...]
    TIA,

    Ger-Jan

  • #2
    In general if the file cannot be loaded it either isn't there or isn't readable...

    Sometimes it is quite a battle, at least that is my personal experience, combining Eclipse and Maven commandline, I found a lot of cases where directories get cleaned, binaries removed etc. so it could be both are competing on a directory. In general I try to clean the project, force a recompile and start everything again (all from within Eclipse, whilst nog using maven).

    Comment


    • #3
      Hi Marten,

      Thanks for your quick response. The approach you suggested works: clean + build, then run as JUnit test (either project or test class).

      I also tried the Maven only option: copied working folder outside the reach of Eclipse/STS, then it ran 'mvn clean test' followed by 'mvn test', but test-context.xml isn't found in either of the runs. Even if for some reason (TestContext tries to load the application context before Maven finishes copying resources?) the file isn't there in the first run, shouldn't TestContext find the missing file the second time round (skipping clean, so leaving the target subtree in place)?

      Any thoughts?

      Thanks again,

      Ger-Jan

      Comment


      • #4
        When you run mvn test clean I would check if the file is there. Could be that there is some problem with a maven plugin (maven-resources-plugin) or configuration of a plugin (skipping the copying of xml files for instance).

        When the files are copied you should see something like below in your maven output...

        Code:
        [INFO] --- maven-resources-plugin:2.5:resources (default-testResources) @ myproject ---
        [INFO] Using 'UTF-8' encoding to copy filtered resources.
        [INFO] Copying 1 resource

        Comment


        • #5
          The file is there allright, I checked again and again. And the copy action itself shows up in the output of mvn -X clean test:

          Code:
          [INFO] --- maven-resources-plugin:2.3:testResources (default-testResources) @ vubis2warp ---
          [DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-resources-plugin:2.3:testResources' with basic configurator -->
          [DEBUG]   (f) filters = []
          [DEBUG]   (s) includeEmptyDirs = false
          [DEBUG]   (s) outputDirectory = /home/gerjan/Desktop/vubis2warp/target/test-classes
          [DEBUG]   (s) overwrite = false
          [DEBUG]   (f) project = MavenProject: nl.rivm.vubis2warp:vubis2warp:0.4-SNAPSHOT @ /home/gerjan/Desktop/vubis2warp/pom.xml
          [DEBUG]   (s) resources = [Resource {targetPath: null, filtering: false, FileSet {directory: /home/gerjan/Desktop/vubis2warp/src/test/resources, PatternSet [includes: {}, excludes: {}]}}]
          [DEBUG]   (f) session = org.apache.maven.execution.MavenSession@c18e99
          [DEBUG] -- end configuration --
          [DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-resources-plugin:2.3:testResources' with basic configurator -->
          [DEBUG]   (f) filters = []
          [DEBUG]   (s) includeEmptyDirs = false
          [DEBUG]   (s) outputDirectory = /home/gerjan/Desktop/vubis2warp/target/test-classes
          [DEBUG]   (s) overwrite = false
          [DEBUG]   (f) project = MavenProject: nl.rivm.vubis2warp:vubis2warp:0.4-SNAPSHOT @ /home/gerjan/Desktop/vubis2warp/pom.xml
          [DEBUG]   (s) resources = [Resource {targetPath: null, filtering: false, FileSet {directory: /home/gerjan/Desktop/vubis2warp/src/test/resources, PatternSet [includes: {}, excludes: {}]}}]
          [DEBUG]   (f) session = org.apache.maven.execution.MavenSession@c18e99
          [DEBUG] -- end configuration --
          [DEBUG] properties used {
          
          [...]
          
          }
          [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
          [DEBUG] resource with targetPath null
          directory /home/gerjan/Desktop/vubis2warp/src/test/resources
          excludes []
          includes []
          [INFO] Copying 15 resources
          
          [...]
          
          [DEBUG] file test-context.xml has a filtered file extension
          [DEBUG] copy /home/gerjan/Desktop/vubis2warp/src/test/resources/nl/rivm/vubis2warp/task/impl/test-context.xml to /home/gerjan/Desktop/vubis2warp/target/test-classes/nl/rivm/vubis2warp/task/impl/test-context.xml

          Comment


          • #6
            Hmm strange... Also, judging from the path, you are on a Unix based system is the file readable (I would expect so because you can also copy the file)... Another thing to check is the configuration/classpath when executing the surefire plugin (might be that it isn't on the classpath)...

            Comment


            • #7
              Bingo. The file is readable, but your suggestion to check the surefire plugin config paid off.

              In order to be able to debug the test in STS I inserted this forkmode config element for surefire in the pom:
              Code:
              <configuration>
              	<forkMode>none</forkMode>
              </configuration>
              If I comment it out again, the app context file not found problem disappears. I would think the classpath for the non-forked surefire differs from the default one. This should show in Maven's debug output, but no, the different debug outputs even claim to use the same classpaths (screenshot attached).

              Anyway; I guess I have to do without debugging tests if I wish to run my tests in a Maven build.

              Thanks once again for your help!

              GJ

              Comment


              • #8
                OK, uploading screenshots doesn't seem to work for me. I've included relevant outputs instead (some longer lines removed):

                forkmode none:

                Code:
                [DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-surefire-plugin:2.10:test' with basic configurator -->
                [DEBUG]   (s) basedir = /home/gerjan/Desktop/vubis2warp
                [DEBUG]   (s) childDelegation = false
                [DEBUG]   (s) classesDirectory = /home/gerjan/Desktop/vubis2warp/target/classes
                [DEBUG]   (s) disableXmlReport = false
                [DEBUG]   (s) enableAssertions = true
                [DEBUG]   (s) forkMode = once
                [DEBUG]   (s) junitArtifactName = junit:junit
                [DEBUG]   (s) localRepository =        id: local
                      url: file:///home/gerjan/.m2/repository/
                   layout: none
                
                [DEBUG]   (f) parallelMavenExecution = false
                [DEBUG]   (s) perCoreThreadCount = true
                [DEBUG]   (s) pluginArtifactMap = {org.apache.maven.plugins:maven-surefire-plugin=org.apache.maven.plugins:maven-surefire-plugin:maven-plugin:2.10:, org.apache.maven.surefire:surefire-booter=org.apache.maven.surefire:surefire-booter:jar:2.10:compile, org.apache.maven.surefire:surefire-api=org.apache.maven.surefire:surefire-api:jar:2.10:compile, org.apache.maven.surefire:maven-surefire-common=org.apache.maven.surefire:maven-surefire-common:jar:2.10:compile, org.apache.maven.shared:maven-common-artifact-filters=org.apache.maven.shared:maven-common-artifact-filters:jar:1.3:compile, org.codehaus.plexus:plexus-utils=org.codehaus.plexus:plexus-utils:jar:2.1:compile, org.apache.maven.reporting:maven-reporting-api=org.apache.maven.reporting:maven-reporting-api:jar:2.0.9:compile}
                [DEBUG]   (s) printSummary = true
                [DEBUG]   (s) project = MavenProject: nl.rivm.vubis2warp:vubis2warp:0.4-SNAPSHOT @ /home/gerjan/Desktop/vubis2warp/pom.xml
                [DEBUG]   (s) projectArtifactMap = { [...] }
                [DEBUG]   (s) redirectTestOutputToFile = false
                [DEBUG]   (s) remoteRepositories = [       id: central
                      url: http://repo.maven.apache.org/maven2
                   layout: default
                snapshots: [enabled => false, update => daily]
                 releases: [enabled => true, update => never]
                ]
                [DEBUG]   (s) reportFormat = brief
                [DEBUG]   (s) reportsDirectory = /home/gerjan/Desktop/vubis2warp/target/surefire-reports
                [DEBUG]   (s) runOrder = filesystem
                [DEBUG]   (s) session = org.apache.maven.execution.MavenSession@211b37
                [DEBUG]   (s) skip = false
                [DEBUG]   (s) skipTests = false
                [DEBUG]   (s) testClassesDirectory = /home/gerjan/Desktop/vubis2warp/target/test-classes
                [DEBUG]   (s) testFailureIgnore = false
                [DEBUG]   (s) testNGArtifactName = org.testng:testng
                [DEBUG]   (s) testSourceDirectory = /home/gerjan/Desktop/vubis2warp/src/test/java
                [DEBUG]   (s) trimStackTrace = true
                [DEBUG]   (s) useFile = true
                [DEBUG]   (s) useManifestOnlyJar = true
                [DEBUG]   (s) useSystemClassLoader = true
                [DEBUG]   (s) useUnlimitedThreads = false
                [DEBUG]   (s) workingDirectory = /home/gerjan/Desktop/vubis2warp
                [DEBUG] -- end configuration --
                [INFO] Surefire report directory: /home/gerjan/Desktop/vubis2warp/target/surefire-reports
                [DEBUG] dummy:dummy:jar:1.0 (selected for null)
                [DEBUG]   org.apache.maven.surefire:surefire-booter:jar:2.10:compile (selected for compile)
                [DEBUG]     org.apache.maven.surefire:surefire-api:jar:2.10:compile (selected for compile)
                [DEBUG] Adding to surefire booter test classpath: /home/gerjan/.m2/repository/org/apache/maven/surefire/surefire-booter/2.10/surefire-booter-2.10.jar Scope: compile
                [DEBUG] Adding to surefire booter test classpath: /home/gerjan/.m2/repository/org/apache/maven/surefire/surefire-api/2.10/surefire-api-2.10.jar Scope: compile
                [DEBUG] Setting system property [user.dir]=[/home/gerjan/Desktop/vubis2warp]
                [DEBUG] Setting system property [localRepository]=[/home/gerjan/.m2/repository]
                [DEBUG] Setting system property [basedir]=[/home/gerjan/Desktop/vubis2warp]
                [DEBUG] Using JVM: /usr/lib/jvm/java-6-openjdk-i386/jre/bin/java
                [DEBUG] dummy:dummy:jar:1.0 (selected for null)
                [DEBUG]   org.apache.maven.surefire:surefire-junit4:jar:2.10:test (selected for test)
                [DEBUG]     org.apache.maven.surefire:surefire-api:jar:2.10:test (selected for test)
                [DEBUG] Adding to surefire test classpath: /home/gerjan/.m2/repository/org/apache/maven/surefire/surefire-junit4/2.10/surefire-junit4-2.10.jar Scope: test
                [DEBUG] Adding to surefire test classpath: /home/gerjan/.m2/repository/org/apache/maven/surefire/surefire-api/2.10/surefire-api-2.10.jar Scope: test
                [DEBUG] test classpath classpath:
                [DEBUG]   /home/gerjan/Desktop/vubis2warp/target/test-classes
                [DEBUG]   /home/gerjan/Desktop/vubis2warp/target/classes
                forkmode default:

                Code:
                [DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-surefire-plugin:2.10:test' with basic configurator -->
                [DEBUG]   (s) basedir = /home/gerjan/Desktop/vubis2warp
                [DEBUG]   (s) childDelegation = false
                [DEBUG]   (s) classesDirectory = /home/gerjan/Desktop/vubis2warp/target/classes
                [DEBUG]   (s) disableXmlReport = false
                [DEBUG]   (s) enableAssertions = true
                [DEBUG]   (s) forkMode = none
                [DEBUG]   (s) junitArtifactName = junit:junit
                [DEBUG]   (s) localRepository =        id: local
                      url: file:///home/gerjan/.m2/repository/
                   layout: none
                
                [DEBUG]   (f) parallelMavenExecution = false
                [DEBUG]   (s) perCoreThreadCount = true
                [DEBUG]   (s) pluginArtifactMap = {org.apache.maven.plugins:maven-surefire-plugin=org.apache.maven.plugins:maven-surefire-plugin:maven-plugin:2.10:, org.apache.maven.surefire:surefire-booter=org.apache.maven.surefire:surefire-booter:jar:2.10:compile, org.apache.maven.surefire:surefire-api=org.apache.maven.surefire:surefire-api:jar:2.10:compile, org.apache.maven.surefire:maven-surefire-common=org.apache.maven.surefire:maven-surefire-common:jar:2.10:compile, org.apache.maven.shared:maven-common-artifact-filters=org.apache.maven.shared:maven-common-artifact-filters:jar:1.3:compile, org.codehaus.plexus:plexus-utils=org.codehaus.plexus:plexus-utils:jar:2.1:compile, org.apache.maven.reporting:maven-reporting-api=org.apache.maven.reporting:maven-reporting-api:jar:2.0.9:compile}
                [DEBUG]   (s) printSummary = true
                [DEBUG]   (s) project = MavenProject: nl.rivm.vubis2warp:vubis2warp:0.4-SNAPSHOT @ /home/gerjan/Desktop/vubis2warp/pom.xml
                [DEBUG]   (s) projectArtifactMap = { [...] }
                [DEBUG]   (s) redirectTestOutputToFile = false
                [DEBUG]   (s) remoteRepositories = [       id: central
                      url: http://repo.maven.apache.org/maven2
                   layout: default
                snapshots: [enabled => false, update => daily]
                 releases: [enabled => true, update => never]
                ]
                [DEBUG]   (s) reportFormat = brief
                [DEBUG]   (s) reportsDirectory = /home/gerjan/Desktop/vubis2warp/target/surefire-reports
                [DEBUG]   (s) runOrder = filesystem
                [DEBUG]   (s) session = org.apache.maven.execution.MavenSession@c18e99
                [DEBUG]   (s) skip = false
                [DEBUG]   (s) skipTests = false
                [DEBUG]   (s) testClassesDirectory = /home/gerjan/Desktop/vubis2warp/target/test-classes
                [DEBUG]   (s) testFailureIgnore = false
                [DEBUG]   (s) testNGArtifactName = org.testng:testng
                [DEBUG]   (s) testSourceDirectory = /home/gerjan/Desktop/vubis2warp/src/test/java
                [DEBUG]   (s) trimStackTrace = true
                [DEBUG]   (s) useFile = true
                [DEBUG]   (s) useManifestOnlyJar = true
                [DEBUG]   (s) useSystemClassLoader = true
                [DEBUG]   (s) useUnlimitedThreads = false
                [DEBUG]   (s) workingDirectory = /home/gerjan/Desktop/vubis2warp
                [DEBUG] -- end configuration --
                [INFO] Surefire report directory: /home/gerjan/Desktop/vubis2warp/target/surefire-reports
                [DEBUG] dummy:dummy:jar:1.0 (selected for null)
                [DEBUG]   org.apache.maven.surefire:surefire-booter:jar:2.10:compile (selected for compile)
                [DEBUG]     org.apache.maven.surefire:surefire-api:jar:2.10:compile (selected for compile)
                [DEBUG] Adding to surefire booter test classpath: /home/gerjan/.m2/repository/org/apache/maven/surefire/surefire-booter/2.10/surefire-booter-2.10.jar Scope: compile
                [DEBUG] Adding to surefire booter test classpath: /home/gerjan/.m2/repository/org/apache/maven/surefire/surefire-api/2.10/surefire-api-2.10.jar Scope: compile
                [DEBUG] Setting system property [user.dir]=[/home/gerjan/Desktop/vubis2warp]
                [DEBUG] Setting system property [localRepository]=[/home/gerjan/.m2/repository]
                [DEBUG] Setting system property [basedir]=[/home/gerjan/Desktop/vubis2warp]
                [DEBUG] dummy:dummy:jar:1.0 (selected for null)
                [DEBUG]   org.apache.maven.surefire:surefire-junit4:jar:2.10:test (selected for test)
                [DEBUG]     org.apache.maven.surefire:surefire-api:jar:2.10:test (selected for test)
                [DEBUG] Adding to surefire test classpath: /home/gerjan/.m2/repository/org/apache/maven/surefire/surefire-junit4/2.10/surefire-junit4-2.10.jar Scope: test
                [DEBUG] Adding to surefire test classpath: /home/gerjan/.m2/repository/org/apache/maven/surefire/surefire-api/2.10/surefire-api-2.10.jar Scope: test
                [DEBUG] test classpath classpath:
                [DEBUG]   /home/gerjan/Desktop/vubis2warp/target/test-classes
                [DEBUG]   /home/gerjan/Desktop/vubis2warp/target/classes

                Comment

                Working...
                X