Announcement Announcement Module
Collapse
No announcement yet.
SpringJUnit4ClassRunner, SpringToolSuite, Eclipse and Standalone Maven and classpath Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • SpringJUnit4ClassRunner, SpringToolSuite, Eclipse and Standalone Maven and classpath

    Hello


    I have just spent 3 hours to trying to get a unit test to run inside SpringTool Suite 2.3.2




    @RunWith(SpringJUnit4ClassRunner.class)

    @ContextConfiguration(locations = {

    "/META-INF/application-context-col-fx-ws-test.xml",

    "/META-INF/application-context-col-fx-ws.xml" })

    public class ColFxServiceEndpointTest {

    /* . . . */

    }



    The unit test works outside of the IDE on the command line, I can execute “mvn clean test” and then Maven completes successfully.

    However trying to run the same thing in Eclipse, or rather STS, produces



    Ijava.lang.IllegalStateException: Failed to load ApplicationContext

    at org.springframework.test.context.TestContext.getAp plicationContext(TestContext.java:308)

    at org.springframework.test.context.support.Dependenc yInjectionTestExecutionListener.injectDependencies (DependencyInjectionTestExecutionListener.java:109 )

    at org.springframework.test.context.support.Dependenc yInjectionTestExecutionListener.prepareTestInstanc e(DependencyInjectionTestExecutionListener.java:75 )

    at org.springframework.test.context.TestContextManage r.prepareTestInstance(TestContextManager.java:333)

    at org.springframework.test.context.junit4.SpringJUni t4ClassRunner.createTest(SpringJUnit4ClassRunner.j ava:220)

    at org.springframework.test.context.junit4.SpringJUni t4ClassRunner$1.runReflectiveCall(SpringJUnit4Clas sRunner.java:301)

    at org.junit.internal.runners.model.ReflectiveCallabl e.run(ReflectiveCallable.java:15)

    at org.springframework.test.context.junit4.SpringJUni t4ClassRunner.methodBlock(SpringJUnit4ClassRunner. java:303)

    at org.springframework.test.context.junit4.SpringJUni t4ClassRunner.runChild(SpringJUnit4ClassRunner.jav a:240)

    at org.junit.runners.BlockJUnit4ClassRunner.runChild( BlockJUnit4ClassRunner.java:50)

    ==/////==

    Caused by: org.springframework.beans.factory.parsing.BeanDefi nitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:META-INF/application-context-col-fx-ws.xml]

    Offending resource: class path resource [META-INF/application-context-col-fx-ws-test.xml]; nested exception is org.springframework.beans.factory.parsing.BeanDefi nitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:META-INF/application-context-col-fx-services.xml]

    Offending resource: class path resource [META-INF/application-context-col-fx-ws.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionSt oreException: IOException parsing XML document from class path resource [META-INF/application-context-col-fx-services.xml]; nested exception is java.io.FileNotFoundException: class path resource [META-INF/application-context-col-fx-services.xml] cannot be opened because it does not exist



    What on earth is going on between Eclipse STS and Maven class paths?


    At first, I thought was a simple problem like it was M2Eclipse plugin, check: the plugin is indeed enabled, Maven dependencies are preferred option. Next, I looked at the pom.xml, check: I see we have JUnit 4.7 artifact JAR listed. I looked at the build class path in Eclipse, check: all JARs appear to be there. Finally, I thought it was the resource path was wrong, so I tried “classpath:/META-INF/application-context-col-fs-ws.xml” and achieved the same failure. And finally, I checked that bean factory files the resources are in fact in the directories target\classes\META-INF and target\test-classes\META-INF respectively. To be sure I executed "mvn resources:resources resources:testResources" and then ran the unit test from the IDE.

    Can you explain please where the configuration settings for STS so that the unit test runs as it does with Maven command line?


    Thank you very much indeed.

  • #2
    It was caused by inconsistent JUnit versions 4.4 (parent col-fx) versus 4.7 (col-fx-ws)

    Code:
    	<dependencies>
    		<dependency>
    			<groupId>junit</groupId>
    			<artifactId>junit</artifactId>
    			<version>4.7</version>
    			<scope>test</scope>
    		</dependency>
    		<dependency>
    			<groupId>org.codehaus.mojo</groupId>
    			<artifactId>findbugs-maven-plugin</artifactId>
    			<version>1.2</version>
    			<scope>provided</scope>
    		</dependency>
    	</dependencies>

    So we have a Maven master parent Project ``Col-fx'' and a sub module called ``col-fx-ws'', one declared JUnit 4.4 and the other JUnit 4.7. I apologise humble Eclipse STS it was not your fault at all, it was ours. We have to get better at organising Maven dependencies here ...(sigh)

    Also we fixed in the meantime class path references to make sure if we put resources in src/main/resources etc then the Maven commandline and the Maven embedder (Eclipse STS) will not ever get confused by denoting the protocol to read from the classpath and from the root package/folder i.e "classpath:/foo-app-context.xml" replace the relative resource path name "foo-app-context.xml"

    Comment

    Working...
    X