ClassCastException当使用嵌入式glassfish进行单元测试时 [英] ClassCastException when using embedded glassfish for unit tests

查看:99
本文介绍了ClassCastException当使用嵌入式glassfish进行单元测试时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一些EJBS上通过maven和一个嵌入式的glassfish容器运行一些单元测试。我的一个测试工作,但所有后来尝试测试不同的EJB导致相同的错误:

  java.lang.ClassCastException :$ Proxy81不能被转换为

随后我尝试测试的任何bean。我确信我的设置是好的,因为我说,我的一个bean可以正常测试。



工作代码的示例:

  @Stateful 
public class LayoutManagerBean实现LayoutManager {

private final Log LOG = LogFactory.getLog(LayoutManagerBean.class) ;



public List< Menu> getMenus(User currentUser){
...
}

}

@Local
public interface LayoutManager {

public List< Menu> getMenus(用户用户);

}

而测试:

  public class LayoutManagerTest {

private static EJBContainer ejbContainer;
private static Context ctx;

@BeforeClass
public static void setUp(){
ejbContainer = EJBContainer.createEJBContainer();
ctx = ejbContainer.getContext();
}

@AfterClass
public static void tearDown(){
ejbContainer.close();
}

@Test
public void getMenus(){
LayoutManager manager = null;
try {
manager =(LayoutManager)ctx.lookup(java:global / classes / LayoutManagerBean!uk.co.monkeypower.openchurch.core.layout.beans.LayoutManager);
} catch(NamingException e){
System.out.println(无法查找gosh darned bean!);
}
assertNotNull(manager);
// Menu [] menus = manager.getMenus();
//assertTrue(menus.length> 1);
}

}

还有一个失败的例子:

  @Singleton 
public class OpenChurchPortalContext implements PortalContext {

private Set< PortletMode> portletModes = Collections.emptySet();
private Set< WindowState> windowStates = Collections.emptySet();

private属性portalProperties = new Properties();

public OpenChurchPortalContext(){
portletModes.add(PortletMode.VIEW);
portletModes.add(PortletMode.HELP);
portletModes.add(PortletMode.EDIT);
portletModes.add(new PortletMode(ABOUT));

windowStates.add(WindowState.MAXIMIZED);
windowStates.add(WindowState.MINIMIZED);
windowStates.add(WindowState.NORMAL);
}
...
}

而测试:

  public class OpenChurchPortalContextTest {

private static EJBContainer ejbContainer;
private static Context ctx;

@BeforeClass
public static void setUp(){
ejbContainer = EJBContainer.createEJBContainer();
ctx = ejbContainer.getContext();
}

@AfterClass
public static void tearDown(){
ejbContainer.close();
}

@Test
public void test(){
OpenChurchPortalContext context = null;
try {
context =(OpenChurchPortalContext)ctx.lookup(java:global / classes / OpenChurchPortalContext);
} catch(NamingException e){
System.out.println(找不到在emebedded jobber中的bean);
}
assertNotNull(context);
设置< PortletMode> modes =(Set< PortletMode>)context.getSupportedPortletModes();
assertTrue(modes.size()> 1);
设置< WindowState> states =(Set< WindowState>)context.getSupportedWindowStates();
assertTrue(states.size()> 1);
}

}

任何想法,为什么这可能不工作?

解决方案

您的单例EJB通过实现PortalContext接口具有默认的本地业务界面。测试客户端只能通过其业务界面知道,实际的bean类(OpenChurchPortalContext)不应该被客户端直接引用。所以修复是通过其业务接口PortalContext查找。


I'm running some unit tests on some EJBS via maven and an embedded glassfish container. One of my tests works, but all subsequent attempts to test a different EJB result in the same error:

java.lang.ClassCastException: $Proxy81 cannot be cast to 

Followed by whatever bean I'm attempting to test. I'm confident my setup is good since, as I say, one of my beans can be tested properly.

Examples of workiing code:

@Stateful
public class LayoutManagerBean implements LayoutManager {

    private final Log LOG = LogFactory.getLog(LayoutManagerBean.class);



    public List<Menu> getMenus(User currentUser) {
        ...
    }

}

@Local
public interface LayoutManager {

    public List<Menu> getMenus(User user);

}

And the test:

public class LayoutManagerTest {

    private static EJBContainer ejbContainer;
    private static Context ctx;

    @BeforeClass
    public static void setUp() {
        ejbContainer = EJBContainer.createEJBContainer();
        ctx = ejbContainer.getContext();
    }

    @AfterClass
    public static void tearDown() {
        ejbContainer.close();
    }

    @Test
    public void getMenus() {
        LayoutManager manager = null;
        try {
            manager = (LayoutManager) ctx.lookup("java:global/classes/LayoutManagerBean!uk.co.monkeypower.openchurch.core.layout.beans.LayoutManager");
        } catch (NamingException e) {
            System.out.println("Failed to lookup the gosh darned bean!");
        }
        assertNotNull(manager);
        //Menu[] menus = manager.getMenus();
        //assertTrue(menus.length > 1);
    }

}

And an example of a failure:

@Singleton
public class OpenChurchPortalContext implements PortalContext {

    private Set<PortletMode> portletModes = Collections.emptySet();
    private Set<WindowState> windowStates = Collections.emptySet();

    private Properties portalProperties = new Properties();

    public OpenChurchPortalContext() {
        portletModes.add(PortletMode.VIEW);
        portletModes.add(PortletMode.HELP);
        portletModes.add(PortletMode.EDIT);
        portletModes.add(new PortletMode("ABOUT"));

        windowStates.add(WindowState.MAXIMIZED);
        windowStates.add(WindowState.MINIMIZED);
        windowStates.add(WindowState.NORMAL);
    }
...
}

And the test:

public class OpenChurchPortalContextTest {

    private static EJBContainer ejbContainer;
    private static Context ctx;

    @BeforeClass
    public static void setUp() {
        ejbContainer = EJBContainer.createEJBContainer();
        ctx = ejbContainer.getContext();
    }

    @AfterClass
    public static void tearDown() {
        ejbContainer.close();
    }

    @Test
    public void test() {
        OpenChurchPortalContext context = null;
        try {
            context = (OpenChurchPortalContext) ctx.lookup("java:global/classes/OpenChurchPortalContext");
        } catch (NamingException e) {
            System.out.println("Failed to find the bean in the emebedded jobber");
        }
        assertNotNull(context);
        Set<PortletMode> modes = (Set<PortletMode>) context.getSupportedPortletModes();
        assertTrue(modes.size() > 1);
        Set<WindowState> states = (Set<WindowState>) context.getSupportedWindowStates();
        assertTrue(states.size() > 1);
    }

}

Any ideas as to why this may not be working?

解决方案

Your singleton EJB has a default local business interface by means of implementing PortalContext interface. The test client should know it only by its business interface, and the actual bean class (OpenChurchPortalContext) should not be referenced directly by the client. So the fix is to look it up by its business interface PortalContext.

这篇关于ClassCastException当使用嵌入式glassfish进行单元测试时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆