Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法使用Junit对spring工厂类进行单元测试_Java_Spring_Unit Testing_Spring Mvc_Junit - Fatal编程技术网

Java 无法使用Junit对spring工厂类进行单元测试

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

我试图用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.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/”下。