我在测试中使用spring加载mock' categoryDAO'在我的testApplicationContext.xml文件中进行了delcared,但运行测试会引发错误:
java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.mockito.org/spring/mockito]
Offending resource: file [/Users/sebastien/dev/categorydao/target/test-classes/testApplicationContext.xml]
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:70)
这是我运行的测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:**/testApplicationContext.xml"})
public class CategoryDAORetryTest {
@Autowired
CategoryDAO categoryDAO;
@Test
public void try_several_times() {
when(categoryDAO.list(anyInt()))
.thenThrow(new RuntimeException("Remote Exception 1"))
.thenThrow(new RuntimeException("Remote Exception 2"))
.thenReturn(Arrays.asList(new CategoryDTO()));
assertThat(categoryDAO.list(SITE_EN_TGU_US, 10).size(), equalTo(1));
}
@Configuration
@EnableRetry
public static class SpringConfig {
}
}
以下是与我使用的依赖关系相关的其他一些信息:
<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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:mockito="http://www.mockito.org/spring/mockito"
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
http://www.mockito.org/spring/mockito classpath:META-INF/mockito.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com" />
<mockito:mock id="categoryDAO" class="com.purch.category.dao.CategoryDAO" />
<mvc:annotation-driven />
</beans>
我还提交了mockito.xsd来使用模拟anotation:
.
└── src
├── main
│ ├── java
...
│ └── resources
│ └── hibernate.cfg.xml
└── test
├── java
│ └── com
│ ├── purch
│ │ └── category
│ │ └── dao
│ │ ├── CategoryDAORetryTest.java
└── resources
├── META-INF
│ ├── mockito.xsd
│ ├── spring.handlers
│ ├── spring.schemas
│ └── spring.tooling
└── testApplicationContext.xml
以下是我在pom.xml中使用的弹簧库列表,其中包含&#34; dep.version.spring&#34;到&#34; 4.3.1.RELEASE&#34;:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${dep.version.spring}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${dep.version.spring}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${dep.version.spring}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${dep.version.spring}</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils-core</artifactId>
<version>${dep.version.commons.beanutils}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${dep.version.hibernate-core}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.9.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>5.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${dep.version.spring}</version>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>1.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.8</version>
</dependency>
我的项目中存储的文件mockito.xsd是:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.mockito.org/spring/mockito"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
targetNamespace="http://www.mockito.org/spring/mockito"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:import namespace="http://www.springframework.org/schema/beans"
schemaLocation="http://www.springframework.org/schema/beans/spring-beans.xsd"/>
<xsd:element name="mock">
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="beans:identifiedType">
<xsd:attribute name="class" type="xsd:string" use="required"/>
<xsd:attribute name="useStaticMap" type="xsd:boolean" default="false" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="spy">
<xsd:complexType>
<xsd:attribute name="beanName" type="xsd:string" use="required"/>
</xsd:complexType>
</xsd:element>
</xsd:schema>
答案 0 :(得分:0)
导入&#34; springockito&#34;在我的pom.xml中丢失了!
<dependency>
<groupId>org.kubek2k</groupId>
<artifactId>springockito</artifactId>
<version>1.0.4</version>
<scope>test</scope>
</dependency>