Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.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测试错误-ClassCastException_Java_Junit - Fatal编程技术网

Java junit测试错误-ClassCastException

Java junit测试错误-ClassCastException,java,junit,Java,Junit,在尝试运行junit测试时,我遇到以下错误- java.lang.ClassCastException: business.Factory cannot be cast to services.itemservice.IItemsService at business.ItemManager.get(ItemManager.java:56) at business.ItemMgrTest.testGet(ItemMgrTest.java:49) 导致故障的具体测试是 @Test public

在尝试运行junit测试时,我遇到以下错误-

java.lang.ClassCastException: business.Factory cannot be cast to services.itemservice.IItemsService
at business.ItemManager.get(ItemManager.java:56)
at business.ItemMgrTest.testGet(ItemMgrTest.java:49)
导致故障的具体测试是

@Test
public void testGet() {
        Assert.assertTrue(itemmgr.get(items));
}
它正在测试的代码是

public boolean get(Items item)  { 

        boolean gotItems = false;       

        Factory factory = Factory.getInstance();

        @SuppressWarnings("static-access")
        IItemsService getItem = (IItemsService)factory.getInstance();

        try {
            getItem.getItems("pens", 15, "red", "gel");
            gotItems = true;
        } catch (ItemNotFoundException e) {
            // catch
            e.printStackTrace();
            System.out.println("Error - Item Not Found");
        }
        return gotItems;
    }
存储项的测试几乎是相同的,效果很好

工厂班是

public class Factory {

    private Factory() {}
    private static Factory Factory = new Factory();
    public static Factory getInstance() {return Factory;}




    public static IService getService(String serviceName) throws ServiceLoadException {
        try {
            Class<?> c = Class.forName(getImplName(serviceName));
            return (IService)c.newInstance();
        } catch (Exception e) {
            throw new ServiceLoadException(serviceName + "not loaded");
        }
    }



    private static String getImplName (String serviceName) throws Exception {
        java.util.Properties props = new java.util.Properties();
            java.io.FileInputStream fis = new java.io.FileInputStream("config\\application.properties");
                props.load(fis);
                    fis.close();
                    return props.getProperty(serviceName);
}
}
公共类工厂{
私有工厂(){}
私有静态工厂=新工厂();
公共静态工厂getInstance(){return Factory;}
公共静态IService getService(字符串serviceName)引发ServiceLoadException{
试一试{
Class c=Class.forName(getImplName(serviceName));
返回(IService)c.newInstance();
}捕获(例外e){
抛出新的ServiceLoadException(serviceName+“未加载”);
}
}
私有静态字符串getImplName(字符串serviceName)引发异常{
java.util.Properties=new java.util.Properties();
java.io.FileInputStream fis=new java.io.FileInputStream(“config\\application.properties”);
道具载荷(fis);
fis.close();
返回props.getProperty(serviceName);
}
}

您的Factory.getInstance方法返回一个Factory对象,而Factory不是IItemsService。也许您需要更改以下内容:

@SuppressWarnings("static-access")
IItemsService getItem = (IItemsService)factory.getInstance();
致:


您调用了错误的方法。方法
Factory.getInstance()
返回一个实例(根据您的实现,它是单实例),因此当您将
Factory
强制转换为
IItemService
时,它将抛出一个
ClassCastException


我在您的
工厂中没有看到任何返回
IItemService
的方法。这里唯一有意义的方法是
getService
,它返回一个
IService
。但是,如果您尝试将
iDevice
强制转换为
IItemService
并且IItemService不扩展iDevice,它可能会抛出
ClassCastException

什么是方法
Factory.getInstance()
类似?它似乎返回一个
工厂
对象,但是
IItemsService
@SuppressWarnings("static-access")
IItemsService getItem = (IItemsService)factory.getService(serviceName);