尝试运行Junit测试时出现此错误。这一行:
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 Class
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 Class
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 :(得分:0)
查看XmlBeanFactory的API,我无法看到如何将其转换为com.me.vpc.configurations.GatewayConfigurations
(因为您创建了该类/接口,这并不奇怪)。
getBeans()
会返回XmlBeanFactory
,getGatewayConfigurations()
会将其转换为GatewayConfigurations
。这将永远不会奏效。