Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

Why do I get a ClassCastException when I use narrow() in an EJB client TestCase?

0
Posted

Why do I get a ClassCastException when I use narrow() in an EJB client TestCase?

0

(Submitted by: Scott Stirling) The solution is to prevent your EJB’s interface classes from being loaded by the JUnit custom class loader by adding them to excluded.properties. This is another problem inherent to JUnit’s dynamically reloading TestCaseClassLoader. Similar to the LinkageErrors with JAXP and the org.xml.sax and org.w3c.dom classes, but with a different result. Here’s some example code: Point point; PointHome pointHome; // The next line works in textui, but throws // ClassCastException in swingui pointHome = (PointHome)PortableRemoteObject. narrow(ctx.lookup(“base/PointHome”), PointHome.class); When you call InitialContext.lookup(), it returns an object that was loaded and defined by the JVM’s system classloader (sun.misc.Launcher$AppClassLoader), but the PointEJBHome.class type is loaded by JUnit’s TestCaseClassLoader. In the narrow(), the two fully qualified class names are the same, but the defining classloaders for the two are different so you get the exception during

0

(Submitted by: Scott Stirling) The solution is to prevent your EJB’s interface classes from being loaded by the JUnit custom class loader by adding them to excluded.properties. This is another problem inherent to JUnit’s dynamically reloading TestCaseClassLoader. Similar to the LinkageErrors with JAXP and the org.xml.sax and org.w3c.dom classes, but with a different result. Here’s some example code: Point point; PointHome pointHome; // The next line works in textui, but throws // ClassCastException in swingui pointHome = (PointHome)PortableRemoteObject. narrow(ctx.lookup(“base/PointHome”), PointHome.class); When you call InitialContext.lookup(), it returns an object that was loaded and defined by the JVM’s system classloader (sun.misc.Launcher$AppClassLoader), but the PointEJBHome.class type is loaded by JUnit’s TestCaseClassLoader.

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.