5 Replies Latest reply on Dec 1, 2008 6:09 PM by kragoth

    java.lang.NoClassDefFoundError - Problem with inheritance

      I'm using JSFUnit and now I'm facing the following problem:

      I have a sample test class called SampleTest, and it works fine:

      public class SampleTest extends ServletTestCase
      {
       public static Test suite()
       {
       return new TestSuite( SampleTest.class );
       }
      
       public void testInitialPage() throws IOException
       {
       JSFSession jsfSession = new JSFSession("/sample.jsp");
      
       JSFClientSession client = jsfSession.getJSFClientSession();
      
       client.setValue("j_username", "admin");
       client.setValue("j_password", "segredo");
       client.click("loginButton");
      
       JSFServerSession server = jsfSession.getJSFServerSession();
      
       Object list = server.getManagedBeanValue("#{tripPlanBean.quarters}");
      
       assertTrue("#{tripPlanBean.quarters} should be a java.util.List", list instanceof List);
      
       }
      
      }


      Then I created an abstract class that extends ServletTestCase, called BaseWebTest, so all my test classes should extend this one. The problem is that, now, when working with inheritance, I'm having a java.lang.NoClassDefFoundError: javax/servlet/http/HttpSessionBindingListener when I try to get the JSFSession in the BaseWebTest:

      public abstract class BaseWebTest extends ServletTestCase
      {
      
      ....
      
       private void initializeSession(String initialPage) throws IOException
       {
       JSFSession jsfSession = new JSFSession(initialPage); //here is where the problem happens
      
       .....
      
       }
      
      ....
      }
      


      So, if I have a class that extends BaseWebTest, is doesn't work... for example:

      public class MyPageTest extends BaseWebTest
      {
      
      ....
      
      }
      


      It doesn't work due to the problem that happens in the BaseWebTest class. But as I mentioned before, the SampleTest works perfeclty. All classes are in the same war file and context, so I suppose that my library configuration is right. It seems there's a problem about the inheritance, I don't know exaclty how JSFUnit handles the classloader context during the test execution, may be this is the cause, but I can't figure out a solution...

      Any help would be very appreciated.

      Thanks!

        • 1. Re: java.lang.NoClassDefFoundError - Problem with inheritanc
          kragoth

          I have a test heirachy and it works just fine. So maybe you have bumped into some classloading weirdness.

          This is my setup:

          public class AbstractGekkoWebTest extends ServletTestCase {
          ...
           @Override
           protected final void setUp() throws Exception {
           super.setUp();
           ...//my own code to do crazy stuff with spring injection :P
           this.afterSetUp();
           }
          
           //SETUP THE SESSION HERE AND LOGIN TO THE SYSTEM
           public void afterSetUp() throws Exception {
           log.debug("GekkoWebTest.afterSetUp(): Request page: " + HomePageDriver.PAGE);
          
           // Send an HTTP request for the initial page
           // There is currently a bug with running as IE 6 so, leave it on Firefox for now
           //WebClientSpec wcSpec = new WebClientSpec(HomePageDriver.PAGE, BrowserVersion.FIREFOX_2);
           WebClientSpec wcSpec = new WebClientSpec(HomePageDriver.PAGE, BrowserVersion.INTERNET_EXPLORER_6);
           testContext.setSession(new JSFSession(wcSpec));
          
           //Logging in should probably be done differently but...this will do for now
           log.debug("GekkoWebTest.afterSetUp(): Logging in");
           HomePageDriver homePageDriver = getPageDriver(HomePageDriver.class);
           homePageDriver.setUsername("xxxxxxxx");
           assertTrue(homePageDriver.isDisplayed());
           homePageDriver.clickSubmit();
           log.debug("After login the page is:" + testContext.getServer().getCurrentViewID());
           }
          
           ...
          
          }
          


          And now each of my tests are declared like this:
          public class TenureMaintenanceTest extends AbstractGekkoWebTest {
          


          • 2. Re: java.lang.NoClassDefFoundError - Problem with inheritanc
            ssilvert

            The only way I can think of that HttpSessionBindingListener would not be found is if you are outside the container. So this could happen when ant or maven calls JUnit to create an instance of your test class, but before the execution call is proxied to the ServletRedirector inside the container.

            So I suspect that maybe you are calling initializeSession() from a constructor instead of from the setUp() method.

            You should be fine if you do it like Kragoth showed. If you are still having trouble please try to post more of the source code and more of the stack trace.

            Stan

            • 3. Re: java.lang.NoClassDefFoundError - Problem with inheritanc

              Thank you, Kragoth and Stan.


              Stan, you are absolutely.

              I moved the initializeSession() method to the setUp method of BaseWebTest and now it works.

              Thank you very much.

              Regards,

              Vitor Isaia.

              • 4. Re: java.lang.NoClassDefFoundError - Problem with inheritanc

                 

                "Vitor Isaia" wrote:

                Stan, you are absolutely.


                I meant: Stan, you are absolutely right.



                • 5. Re: java.lang.NoClassDefFoundError - Problem with inheritanc
                  kragoth

                  Nice pickup Stan :) That didn't even occur to me! I need to get a little more familiar with exactly how JSFUnit works at the core level. :S