Why do I get a ClassCastException when I use narrow() in an EJB client TestCase?
(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
(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.