2012-12-18 39 views
0

試圖運行Junit測試時出現此錯誤。此行:與XmlBeanFactory相關的Casting錯誤

 AppConfig.getGatewayConfigurations().getURL(); 

在某些地方工作正常。另外我沒有任何與XML文件路徑有關的問題。而類的GatewayConfigurations構建得很好,它的屬性在bean配置文件中分配得很好!

這裏是跟蹤

java.lang.ClassCastException: org.springframework.beans.factory.xml.XmlBeanFactory cannot be cast to com.me.vpc.configurations.GatewayConfigurations 
at com.me.vpc.configurations.AppConfig.getGatewayConfigurations(AppConfig.java:26) 
at com.me.vpc.test.PaymentQueryStringBuilderThirdPartyTest.buildQueryStringTest(PaymentQueryStringBuilderThirdPartyTest.java:34) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
at java.lang.reflect.Method.invoke(Unknown Source) 
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59) 
at org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:98) 
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:79) 
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:87) 
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:77) 
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42) 
at org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:88) 
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51) 
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44) 
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27) 
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37) 
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42) 
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 

PaymentQueryStringBuilderThirdPartyTest類

package com.me.vpc.test; 

import static org.junit.Assert.*; 

import java.io.UnsupportedEncodingException; 
import java.util.HashMap; 
import java.util.Map; 

import org.junit.Before; 
import org.junit.Test; 
import com.me.vpc.common.PaymentQueryStringBuilderThirdParty; 
import com.me.vpc.configurations.AppConfig; 


public class PaymentQueryStringBuilderThirdPartyTest { 

@SuppressWarnings("rawtypes") 
private Map parameters; 

@SuppressWarnings({ "unchecked", "rawtypes" }) 
@Before 
public void setUp() throws Exception { 

    parameters = new HashMap(); 

    parameters.put("vpc_Merchant", "MER123"); 
    parameters.put("vpc_OrderInfo", "A48cvE28"); 
    parameters.put("vpc_Amount", "2995"); 
} 

@Test 
public void buildQueryStringTest() throws UnsupportedEncodingException { 

    String URL = AppConfig.getGatewayConfigurations().getURL(); 

    String expectedQueryString = URL + "?vpc_Merchant=MER123&vpc_OrderInfo=A48cvE28&vpc_Amount=2995"; 

    assertEquals(expectedQueryString, PaymentQueryStringBuilderThirdParty.buildQueryString(parameters)); 
} 

} 

的AppConfig類

public class AppConfig { 

/** 
* Default xml configurations file 
*/ 
private final static String APP_CONFIG_FILE = "vpc-configurations.xml"; 

/** 
* Load gateway configurations 
* @return GatwayConfigurations object 
*/ 
public static GatewayConfigurations getGatewayConfigurations(){ 

    return (GatewayConfigurations)getBeans(); 
} 

/** 
* Get the desired configuration bean 
* @param beanName 
* @return BeanFactory object 
*/ 
private static BeanFactory getBeans() 
{ 
    return new XmlBeanFactory(new FileSystemResource(APP_CONFIG_FILE)); 
} 


} 

回答

0

看着爲XmlBeanFactory的API,我看不出它如何能投到com.me.vpc.configurations.GatewayConfigurations(毫不奇怪,因爲你有cr吃了那個類/接口)。

getBeans()返回XmlBeanFactorygetGatewayConfigurations()轉換爲GatewayConfigurations。這將無法工作。