Note that there are some explanatory texts on larger screens.

plurals
  1. POClassCastException when using embedded glassfish for unit tests
    primarykey
    data
    text
    <p>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:</p> <pre><code>java.lang.ClassCastException: $Proxy81 cannot be cast to </code></pre> <p>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.</p> <p>Examples of workiing code:</p> <pre><code>@Stateful public class LayoutManagerBean implements LayoutManager { private final Log LOG = LogFactory.getLog(LayoutManagerBean.class); public List&lt;Menu&gt; getMenus(User currentUser) { ... } } @Local public interface LayoutManager { public List&lt;Menu&gt; getMenus(User user); } </code></pre> <p>And the test:</p> <pre><code>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 &gt; 1); } } </code></pre> <p>And an example of a failure:</p> <pre><code>@Singleton public class OpenChurchPortalContext implements PortalContext { private Set&lt;PortletMode&gt; portletModes = Collections.emptySet(); private Set&lt;WindowState&gt; 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); } ... } </code></pre> <p>And the test:</p> <pre><code>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&lt;PortletMode&gt; modes = (Set&lt;PortletMode&gt;) context.getSupportedPortletModes(); assertTrue(modes.size() &gt; 1); Set&lt;WindowState&gt; states = (Set&lt;WindowState&gt;) context.getSupportedWindowStates(); assertTrue(states.size() &gt; 1); } } </code></pre> <p>Any ideas as to why this may not be working?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload