java.lang.ClassCastException:$ Proxy96无法强制转换为ticket.app.DatesFacade

时间:2011-04-02 17:20:00

标签: java jsf junit hudson

我正在将一些代码输入到在Ubuntu框上运行的Hudson Server中,以便在代码上运行一些代码度量(Sonar)和Cobertura。该项目运行Glassfish3.1,使用Maven3,用Java编写,JSF 2.0并使用OracleXE数据库(不相关)。

尝试在JUnit测试中创建Facade实例时会抛出错误。当我从Netbeans运行测试时,测试运行正常,但是当Hudson自动构建它时,我得到了这个错误:

    java.lang.ClassCastException: $Proxy96 cannot be cast to ticket.app.DatesFacade
    at ticket.app.EventsControllerTest.setUp(EventsControllerTest.java:60)
    at junit.framework.TestCase.runBare(TestCase.java:128)
    at junit.framework.TestResult$1.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:120)
    at junit.framework.TestSuite.runTest(TestSuite.java:230)
    at junit.framework.TestSuite.run(TestSuite.java:225)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at org.apache.maven.surefire.junit.JUnitTestSet.execute(JUnitTestSet.java:207)
    at org.apache.maven.surefire.junit.JUnit3Provider.executeTestSet(JUnit3Provider.java:107)
    at org.apache.maven.surefire.junit.JUnit3Provider.invoke(JUnit3Provider.java:79)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at org.apache.maven.surefire.booter.ProviderFactory$ClassLoaderProxy.invoke(ProviderFactory.java:103)
    at $Proxy0.invoke(Unknown Source)
    at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:145)
    at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcess(SurefireStarter.java:87)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:69)

Hudson服务器不是从JBoss运行的,它是从Tomcat运行的。我添加了这个细节,因为当我谷歌这个错误时,除了JBoss错误之外,我什么都没发现,这个错误解释了在WAR和EAR文件中都引用了facade。

编辑:这就是我目前正在创建Facade的实例(在通过Netbeans GF3.1运行测试时工作正常):

        Map properties = new HashMap();
    properties.put(EJBContainer.MODULES, new File("target/classes"));
    properties.put("org.glassfish.ejb.embedded.glassfish.configuration.file", "server/config/domain.xml");
    properties.put("oracle.jdbc.OracleDriver", "server/lib/ojdbc14.jar");
    ejbContainer = EJBContainer.createEJBContainer(properties);
    ctx = ejbContainer.getContext();

    EventsFacade instance = (EventsFacade)ctx.lookup("java:global/classes/EventsFacade");

编辑:我的EventsFacade:

@Stateful
public class EventsFacade extends AbstractFacade<Events> {
    @PersistenceContext(unitName = "tickets_AppTicket_war_1.0-SNAPSHOTPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public EventsFacade() {
        super(Events.class);
    }
}

编辑:并且我的AbstractFacade已经完整:

public abstract class AbstractFacade<T> {
private Class<T> entityClass;

public AbstractFacade(Class<T> entityClass) {
    this.entityClass = entityClass;
}

protected abstract EntityManager getEntityManager();

public void create(T entity) {
    getEntityManager().persist(entity);
}

public void edit(T entity) {
    getEntityManager().merge(entity);
}

public void remove(T entity) {
    getEntityManager().remove(getEntityManager().merge(entity));
}

public T find(Object id) {
    return getEntityManager().find(entityClass, id);
}

public List<T> findAll() {
    javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
    cq.select(cq.from(entityClass));
    return getEntityManager().createQuery(cq).getResultList();
}

public List<T> findRange(int[] range) {
    javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
    cq.select(cq.from(entityClass));
    javax.persistence.Query q = getEntityManager().createQuery(cq);
    q.setMaxResults(range[1] - range[0]);
    q.setFirstResult(range[0]);
    return q.getResultList();
}

public int count() {
    javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
    javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
    cq.select(getEntityManager().getCriteriaBuilder().count(rt));
    javax.persistence.Query q = getEntityManager().createQuery(cq);
    return ((Long) q.getSingleResult()).intValue();
}

}

编辑:创建新界面:

    @Local
public interface EventsInterface<Events> {

    int count();

    void create(Events entity);

    void edit(Events entity);

    Events find(Object id);

    List<Events> findAll();

    List<Events> findRange(int[] range);

    void remove(Events entity);

}

已更改: EventsFacade delaration:

public class EventsFacade extends AbstractFacade<Events> implements EventsInterface<Events> {

编辑:在创建新界面后在IDE中收到代理错误后,输出开始显示:

Field Name == intfClass
Field == interface ticket.app.EventsInterface
Field Name == containerId
Field == 85313541807800321
Field Name == delegate
Field == com.sun.ejb.containers.EJBLocalObjectInvocationHandler@72b0f2b2
Field Name == isOptionalLocalBusinessView
Field == false

1 个答案:

答案 0 :(得分:5)

这通常意味着当对象周围有代理(并且代理基于接口)时,您通过其类引用对象,而不是通过其接口引用对象。

解决方案是通过其接口引用对象。我猜想EventsFacade有一个Local和/或Remote接口。尝试使用它。如果它没有界面 - 制作一个,这是一个很好的做法。定义界面中的所有公共方法。

更新:看起来,这可能是您正在使用的嵌入式玻璃鱼的问题。由于您无法使用调试器进行调试,因此您可以采取以下措施来跟踪问题:使用java.lang.reflect.Proxy.getInvocationHandler(object)。然后列出返回对象的所有字段。 (你现在必须放弃演员表并将其从上下文中简单地作为Object)。类似的东西:

Object ic = Proxy.getInvocationHandler(facade);
Field[] fields = ic.getClass().getDeclaredFields();
for (Field field : fields) {
    field.setAccessible(true);
    System.out.println(field.getName() + "=" + field.get(ic);
}