Java 无法使用Junit对spring工厂类进行单元测试
我试图用SpringJUnit对一个类进行单元测试,并得到以下异常 我的ProductDaoFactory类是一个单例类,其中只有一个非静态工厂方法 错误日志:Java 无法使用Junit对spring工厂类进行单元测试,java,spring,unit-testing,spring-mvc,junit,Java,Spring,Unit Testing,Spring Mvc,Junit,我试图用SpringJUnit对一个类进行单元测试,并得到以下异常 我的ProductDaoFactory类是一个单例类,其中只有一个非静态工厂方法 错误日志: Caused by: java.io.FileNotFoundException: class path resource [application-context.xml] cannot be opened because it does not exist at org.springframework.core.io.Cla
Caused by: java.io.FileNotFoundException: class path resource [application-context.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:171)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:329)
... 43 more
ProductDaoFactoryTest.java
@ContextConfiguration(locations = "classpath:application-context.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class ProductDaoFactoryTest {
@Test
public void testProductDaoFactory() {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application-context.xml");
ProductDaoFactory productDaoFactory = (ProductDaoFactory) applicationContext.getBean("productDaoFactory");
assertNotNull(productDaoFactory);
applicationContext.close();
}
}
public final class ProductDaoFactory {
private static final ProductDaoFactory INSTANCE = new ProductDaoFactory();
private String daoType;
//so cannot new an intance
private ProductDaoFactory() {
}
//singleton
public static ProductDaoFactory getInstance() {
return INSTANCE;
}
public void setDaoType(String daoType) {
this.daoType = daoType;
}
//factory method
public ProductDao getProductDao() {
ProductDao productDao = null;
if ("jdbc".equalsIgnoreCase(daoType)) {
productDao = new ProductDaoJdbcImpl();
} else if ("ibatis".equalsIgnoreCase(daoType)) {
productDao = new ProductDaoIBatisImpl();
} else if ("hibernate".equalsIgnoreCase(daoType)) {
productDao = new ProductDaoHibernateImpl();
}
return productDao;
}
}
ProductDaoFactory.java
@ContextConfiguration(locations = "classpath:application-context.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class ProductDaoFactoryTest {
@Test
public void testProductDaoFactory() {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application-context.xml");
ProductDaoFactory productDaoFactory = (ProductDaoFactory) applicationContext.getBean("productDaoFactory");
assertNotNull(productDaoFactory);
applicationContext.close();
}
}
public final class ProductDaoFactory {
private static final ProductDaoFactory INSTANCE = new ProductDaoFactory();
private String daoType;
//so cannot new an intance
private ProductDaoFactory() {
}
//singleton
public static ProductDaoFactory getInstance() {
return INSTANCE;
}
public void setDaoType(String daoType) {
this.daoType = daoType;
}
//factory method
public ProductDao getProductDao() {
ProductDao productDao = null;
if ("jdbc".equalsIgnoreCase(daoType)) {
productDao = new ProductDaoJdbcImpl();
} else if ("ibatis".equalsIgnoreCase(daoType)) {
productDao = new ProductDaoIBatisImpl();
} else if ("hibernate".equalsIgnoreCase(daoType)) {
productDao = new ProductDaoHibernateImpl();
}
return productDao;
}
}
WEB-INF/application context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
.....
">
<bean id="productDaoFactory" class="com.ministore.dao.factory.ProductDaoFactory" factory-method="getInstance"></bean>
<bean id="productDao" factory-bean="productDaoFactory" factory-method="getProductDao">
<property name="daoType" value="jdbc" />
</bean>
<!-- DATASOURCES AND DAO LAYER -->
<!-- using JEE namespace for lookup -->
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/ministore-db" expected-type="javax.sql.DataSource" />
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
WEB-INF/application context.xml
在测试过程中不在类路径上。请您指导如何解决此问题?将文件移动到可见的位置。如果您使用的是Maven,它将位于“src/main/resources/”下。