0

尝试运行 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 类

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));
}


}
4

1 回答 1

0

查看XmlBeanFactory的 API ,我看不出它是如何转换的com.me.vpc.configurations.GatewayConfigurations(这并不奇怪,因为您已经创建了该类/接口)。

getBeans()返回 a XmlBeanFactory,并将其getGatewayConfigurations()强制转换为GatewayConfigurations. 这永远不会奏效。

于 2012-12-18T10:41:50.290 回答