我在运行单元测试用例时遇到JUnit问题。我使用的是spring 4.3.2.RELEASE和JUnit 4.12以及Maven 3.3.9。我不确定为什么我在运行测试代码时遇到此异常。我尝试了所有可能的方法来执行代码,但是我无法运行测试用例。
例外情况是:
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)
引起:org.springframework.beans.factory.BeanDefinitionStoreException:IOException从类路径资源解析XML文档[spring-beans / applicationContext-b2b-transport1.xml];嵌套异常是java.io.FileNotFoundException:类路径资源[spring-beans / applicationContext-b2b-transport1.xml]无法打开,因为它不存在 在org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341) 在org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302) 在org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143) 在org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178) 在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个 引起:java.io.FileNotFoundException:无法打开类路径资源[spring-beans / applicationContext-b2b-transport1.xml],因为它不存在 在org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158) 在org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328) ......还有33个
的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>
以下是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.....
}
}
以下是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;
}
}
以下是名为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>
提示:此配置文件位于src / main / resource / spring-beans / applicationContext-b2b-transport1.xml下 尝试了所有可能的方法。请帮我解决这个问题。
答案 0 :(得分:1)
错误似乎很明显......
class path resource [spring-beans/applicationContext-b2b-transport1.xml] cannot be opened because it does not exist
...你在哪里引用那个文件?
您还在加载......
@ContextConfiguration(locations = {"classpath:sree/Bean.xml"})
不是/spring-beans/Beans.xml
。