0
votes

I am facing issue with JUnit while running unit test cases.I am using spring 4.3.2.RELEASE and JUnit 4.12 and Maven 3.3.9. I'm not sure why i'm getting this exception while running the test code.I tried all the possible ways to execute the code but i got failed to run test cases.

Exception is:

java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:308)
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:333)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:220)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:301)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:303)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
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:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [spring-beans/applicationContext-b2b-transport1.xml]; nested exception is java.io.FileNotFoundException: class path resource [spring-beans/applicationContext-b2b-transport1.xml] cannot be opened because it does not exist at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212) at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:81) at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1) at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:280) at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:304) ... 24 more Caused by: java.io.FileNotFoundException: class path resource [spring-beans/applicationContext-b2b-transport1.xml] cannot be opened because it does not exist at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328) ... 33 more

POM.Xml

<?xml version="1.0" encoding="utf-8"?>

<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.bt.b2b.gateway</groupId>
    <artifactId>b2babc</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>b2babc_ws</name>
    <url>http://maven.apache.org</url>
    <!-- Shared version number properties -->
    <properties>
        <!--env>dev</env -->
        <!-- <org.springframework.version>4.1.8.RELEASE</org.springframework.version>  commented for Spring Library Updates-->
        <org.springframework.version>4.3.2.RELEASE</org.springframework.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <timestamp>${maven.build.timestamp}</timestamp>
         <maven.build.timestamp.format>yyyy-MM-dd-HH-mm</maven.build.timestamp.format>
    </properties>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
            <!--    <version>1.3</version> commented for Spring Library Updates  --> 
                <version>1.8</version>
            </plugin>

            <plugin>
                <artifactId>maven-clean-plugin</artifactId>
            <!--    <version>2.4.1</version>commented for Spring Library Updates -->
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <id>auto-clean</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>clean</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>


        </plugins>
        <!--filters>
            <filter>${env.M2_HOME}/conf/${env}.properties</filter>
        </filters-->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <testResources>
            <testResource>
                <directory>src/test/resources</directory>
                <filtering>true</filtering>
            </testResource>
        </testResources>
        <!-- <finalName>OpenreachSSGatewayEAR-${maven.build.timestamp}</finalName> -->

    </build>

    <dependencies>

        <!-- Open Source Dependencies -->
        <dependency>
            <groupId>emw-logging-framework</groupId>
            <artifactId>logging</artifactId>
            <version>3_3_12_2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${org.springframework.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.xmlbeans</groupId>
            <artifactId>xmlbeans-xpath</artifactId>
            <version>2.4.0</version>


            <!--<scope>provided</scope>-->
        </dependency>
        <dependency>
            <groupId>net.sf.saxon</groupId>
            <artifactId>saxon</artifactId>
            <version>9</version>
        </dependency>

        <dependency>
            <groupId>net.sf.saxon</groupId>
            <artifactId>saxon9-dom</artifactId>
            <version>9.1.0.8</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.6.4</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.4</version>

        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>


        <!-- B2B Specific Dependencies -->

        <dependency>
            <groupId>actionplan</groupId>
            <artifactId>actionplan</artifactId>
            <!-- <version>1.6</version> added storeBusiness Receipt in Action Plan 1.7 -->
            <version>1.7</version>
        </dependency>
        <dependency>
            <groupId>e537DeliveryReceipt</groupId>
            <artifactId>e537DeliveryReceipt</artifactId>
            <version>1.0</version>          
        </dependency>

        <dependency>
            <groupId>com.bt</groupId>
            <artifactId>aliasnamesplan</artifactId>
            <version>1.0</version>
        </dependency>

        <dependency>
            <groupId>supplieroutboundactionplan</groupId>
            <artifactId>supplieroutboundactionplan</artifactId>
            <version>1.0</version>
        </dependency>

        <dependency>
            <groupId>com.bt</groupId>
            <artifactId>AliasNamesPlanSingleStageCompr</artifactId>
            <version>1.0</version>
        </dependency>       

        <dependency>
            <groupId>noNamespace</groupId>
            <artifactId>b2bpropertymappings</artifactId>
            <version>1.1</version>
        </dependency>

        <dependency>
            <groupId>noNamespace</groupId>
            <artifactId>b2bbesconfig</artifactId>
            <version>1.3</version>
        </dependency>
        <dependency>
            <groupId>noNamespace</groupId>
            <artifactId>b2bmisservices</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>noNamespace</groupId>
            <artifactId>btacknowledgements</artifactId>
            <version>1.1</version>
        </dependency>

        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>       



        <!-- Added these dependencies for EassyMock Testing-->

        <dependency>
            <groupId>org.easymock.EasyMock</groupId>
            <artifactId>EasyMock</artifactId>
            <version>3.1</version>

            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.objectweb.asm</groupId>
            <artifactId>ASM</artifactId>
            <version>3.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>net.sf.cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.objenesis</groupId>
            <artifactId>objenesis</artifactId>
            <version>1.2</version>
            <scope>test</scope>
        </dependency>
        <!-- EassyMock End -->
    </dependencies>


    <modules>
        <module>components/p</module>
        <module>components/b2ba</module>
        <module>components/b2bb</module>
        <module>components/b2bc</module>
        <module>components/b2bd</module>
        <module>components/b2be</module>
        <module>components/f</module>
        <module>components/g</module>
        <module>components/h</module>
        <module>components/i</module>
        <module>components/j</module>
        <module>components/k</module>
        <module>components/l</module>
        <module>components/m</module>
        <module>ears/o</module>
        <module>components/b2bn</module>
    </modules>

    <!-- profiles and their matching property filenames -->
    <profiles>

        <profile>
            <id>BTR</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>

            <properties>
                <build.profile.id>BTR</build.profile.id>
                <b2blogpath></b2blogpath>
                <earName>somexyzEAR</earName>
            </properties>
            <build>
                <filters>
                    <filter>${env.M2_HOME}/conf/${build.profile.id}.properties</filter>
                </filters>
                <resources>
                    <resource>
                        <filtering>true</filtering>
                        <directory>src/main/resources</directory>
                    </resource>
                </resources>
            </build>
        </profile>
        <profile>
            <id>BTORSS</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>

            <properties>
                <build.profile.id>BTORSS</build.profile.id>
                <b2blogpath></b2blogpath>
                <earName>somexyzEAR</earName> 

                <!-- <timestamp>${maven.build.timestamp}</timestamp> -->
                <!-- <maven.build.timestamp.format>yyyy-MM-dd HH:mm</maven.build.timestamp.format> -->
            </properties>
            <build>         
                <filters>
                    <filter>${env.M2_HOME}/conf/${build.profile.id}.properties</filter>
                </filters>
                <resources>
                    <resource>
                        <filtering>true</filtering>
                        <directory>src/main/resources</directory>
                    </resource>
                </resources>
            </build>
        </profile>
        <profile>
            <id>BTWS</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>

            <properties>
                <build.profile.id>BTWS</build.profile.id>
                <b2blogpath></b2blogpath>
                <earName>somexyzEAR</earName>
            </properties>
            <build>
                <filters>
                    <filter>${env.M2_HOME}/conf/${build.profile.id}.properties</filter>
                </filters>
                <resources>
                    <resource>
                        <filtering>true</filtering>
                        <directory>src/main/resources</directory>
                    </resource>
                </resources>
            </build>
        </profile>

        <profile>
            <id>BTGS</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>

            <properties>
                <build.profile.id>BTGS</build.profile.id>
                <b2blogpath></b2blogpath>
                <earName>somexyzEAR</earName>
            </properties>
            <build>
            <finalName>${project.artifactId}-${project.version}-${timestamp}</finalName>
                <filters>
                    <filter>${env.M2_HOME}/conf/${build.profile.id}.properties</filter>
                </filters>
                <resources>
                    <resource>
                        <filtering>true</filtering>
                        <directory>src/main/resources</directory>
                    </resource>
                </resources>
            </build>
        </profile>

        <profile>
            <id>BTOR</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>

            <properties>
                <build.profile.id>BTOR</build.profile.id>
                <b2blogpath></b2blogpath>
                <earName>OpenreachGatewayEAR</earName>
            </properties>
            <build>
                <filters>
                    <filter>${env.M2_HOME}/conf/${build.profile.id}.properties</filter>
                </filters>
                <resources>
                    <resource>
                        <filtering>true</filtering>
                        <directory>src/main/resources</directory>
                    </resource>
                </resources>
            </build>
        </profile>          

    </profiles>


</project>

Below is the code for Junit Testcase

package com.java.autowire;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ContextConfiguration(locations = {"classpath:spring-beans/applicationContext-b2b-transport1.xml"})
@RunWith(SpringJUnit4ClassRunner.class)

public class TestMainApp {
    /*@Autowired
    private Student student;*/

    @Test
    public void getname(){
              //Do something here.....
    }
}

Below is the code for Studenet.java

package com.java.autowire;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;

public class Student {

    private Integer age;

    private String name;

    public String getName() {
        return name;
    }

    //@Autowired
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }

    //@Autowired(required=false)
    public void setAge(Integer age) {
        this.age = age;
    }
}

Below is the Configuration file named as Bean.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns = "http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context = "http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <context:annotation-config/>

   <!-- Definition for student bean -->
   <bean id = "student" class = "com.java.autowire.Student">

   </bean>

</beans> 

Hint: This configuration file is under src/main/resource/spring-beans/applicationContext-b2b-transport1.xml
Tried all the possible ways. Please help me out from this issue.

1
Could you please add the code that access the file ~beans/applicationContext-b2b-transport1.xml~ so that the question can be answered?Ashok.N

1 Answers

1
votes

The error seems pretty obvious...

class path resource [spring-beans/applicationContext-b2b-transport1.xml] cannot be opened because it does not exist

...where are you referencing that file?

You are also loading...

@ContextConfiguration(locations = {"classpath:sree/Bean.xml"})

not /spring-beans/Beans.xml.