Jahia Dev Forum > Testing Action Controller

0 (0 Good)
0 (0 Bad)

Testing Action Controller

by  rali »  2013/11/27 21:49

So I created a module and then I created a java file with  a main in it so that I can test the controller. 

Though I'm getting an exception:

 

 
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.ws.soap.saaj.SaajSoapMessag
eFactory] for bean with name 'messageFactory' defined in class path resource [rasheed-module.xml]; nested exception is ja
va.lang.ClassNotFoundException: org.springframework.ws.soap.saaj.SaajSoapMessageFactory
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1250)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactor
y.java:576)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1319)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:885)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:
562)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java
:895)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
at com.rasheed.jahia.services.AbstractServiceController.getAppContext(AbstractServiceController.java:20)
 
 
protected static ApplicationContext getAppContext() {
if(appContext == null) {
LN:20   appContext = new ClassPathXmlApplicationContext(new String[] {"rasheed-module.xml"});
}
return appContext;
}

Can you guys help me with this or is this more of a SpringFramework problem? 

  • Testing Action Controller
    2013/11/27 21:52

    rali <p> So I created a module and then I created a java file with &nbsp;a main in it so that I can test the controller.&nbsp;</p> <p> Though I&#39;m getting an exception:</p> <p> &nbsp;</p> <div> &nbsp;</div> <blockquote> <div> org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.ws.soap.saaj.SaajSoapMessag</div> <div> eFactory] for bean with name &#39;messageFactory&#39; defined in class path resource [rasheed-module.xml]; nested exception is ja</div> <div> va.lang.ClassNotFoundException: org.springframework.ws.soap.saaj.SaajSoapMessageFactory</div> <div> at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1250)</div> <div> at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactor</div> <div> y.java:576)</div> <div> at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1319)</div> <div> at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:885)</div> <div> at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:</div> <div> 562)</div> <div> at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java</div> <div> :895)</div> <div> at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)</div> <div> at org.springframework.context.support.ClassPathXmlApplicationContext.&lt;init&gt;(ClassPathXmlApplicationContext.java:139)</div> <div> at org.springframework.context.support.ClassPathXmlApplicationContext.&lt;init&gt;(ClassPathXmlApplicationContext.java:93)</div> <div> at com.rasheed.jahia.services.AbstractServiceController.getAppContext(AbstractServiceController.java:20)</div> </blockquote> <div> &nbsp;</div> <div> &nbsp;</div> <blockquote> <div> <div> protected static ApplicationContext getAppContext() {</div> <div> if(appContext == null) {</div> <div> <strong>LN:20 &nbsp;&nbsp;</strong>appContext = new ClassPathXmlApplicationContext(new String[] {&quot;rasheed-module.xml&quot;});</div> <div> }</div> <div> return appContext;</div> <div> }</div> </div> <p> Can you guys help me with this or is this more of a SpringFramework problem?&nbsp;</p> </blockquote>

  • Number of messages  8
    Registration date Nov 27, 2013
    0 (0 Good)
    0 (0 Bad)

    Re: Testing Action Controller

    by  rali »  2013/11/27 21:51

    FYI

    heres the rest of the exception 

     

    at com.rasheed.jahia.services.AbstractServiceController.getAppContext(AbstractServiceController.java:20)
    at com.rasheed.jahia.services.AbstractServiceController.getRestServiceResponse(AbstractServiceController.java:26)
    at com.rasheed.jahia.services.rasheedServiceController.getForms(rasheedServiceController.java:248)
    at com.rasheed.cms.actions.Action.doExecute(Action.java:59)
    at com.rasheed.cms.actions.TESTAction.main(TESTAction.java:87)
  • Re: Testing Action Controller
    2013/11/27 21:51

    rali <p> FYI</p> <p> heres the rest of the exception&nbsp;</p> <p> &nbsp;</p> <blockquote> <div> at com.rasheed.jahia.services.AbstractServiceController.getAppContext(AbstractServiceController.java:20)</div> <div> at com.rasheed.jahia.services.AbstractServiceController.getRestServiceResponse(AbstractServiceController.java:26)</div> <div> at com.rasheed.jahia.services.rasheedServiceController.getForms(rasheedServiceController.java:248)</div> <div> at com.rasheed.cms.actions.Action.doExecute(Action.java:59)</div> <div> at com.rasheed.cms.actions.TESTAction.main(TESTAction.java:87)</div> </blockquote>

  • Number of messages  8
    Registration date Nov 27, 2013
    0 (0 Good)
    0 (0 Bad)

    Re: Testing Action Controller

    by  shuber »  2013/11/28 08:30

    Hello, 

    This is indeed more a Spring framework question. But how are you executing your test ? It seems that you are missing some Spring JARs in the classloader executing your test. You can either build your own dynamic classloader using an URLClassLoader to reference the JARs that are in Jahia's WEB-INF/lib directory or if you are using Maven you could include the Spring framework JARs you need with :

    <scope>test</scope>

    So they would only be used for the test execution.

    Also, why don't you use JUnit or TestNG to write your tests ?

    Best regards,

      Serge Huber.

    Serge Huber (shuber)

    Number of messages  271
    Registration date
    0 (0 Good)
    0 (0 Bad)

    Re: Re: Testing Action Controller

    by  rali »  2013/11/28 14:08

    Awesome response!

    Junit is ideal, I just haven't used it enough and I wanted to do this test quickly. I'll probably go back in a week or two and try to become more expert at JUnit. If you have any great tutorials or references please share. 

     

    I like the idea of building my own ClassLoader. Seems like a few lines of code no? Maybe creating it in the AbstractController  right? 


    See the reason I don't want to do the scope idea is because when I run the testAction it doesn't build, but if I do a build first and then run it then it could pull the necessary dependencies.. 

    I like your suggestions, though havening not done either before it's hard to imagine next steps. I would like to try the ClassLoader idea first. 

  • Re: Re: Testing Action Controller
    2013/11/28 14:08

    rali <p> Awesome response!</p> <p> Junit is ideal, I just haven&#39;t used it enough and I wanted to do this test quickly. I&#39;ll probably go back in a week or two and try to become more expert at JUnit. If you have any great tutorials or references please share.&nbsp;</p> <p> &nbsp;</p> <p> I like the idea of building my own ClassLoader. Seems like a few lines of code no? Maybe creating it in the AbstractController &nbsp;right?&nbsp;</p> <p> <br /> See the reason I don&#39;t want to do the scope idea is because when I run the testAction it doesn&#39;t build, but if I do a build first and then run it then it could pull the necessary dependencies..&nbsp;</p> <p> I like your suggestions, though havening not done either before it&#39;s hard to imagine next steps. I would like to try the ClassLoader idea first.&nbsp;</p>

  • Number of messages  8
    Registration date Nov 28, 2013
    0 (0 Good)
    0 (0 Bad)

    Re: Re: Re: Testing Action Controller

    by  shuber »  2013/11/30 07:30

    Creating your own class loader is not that difficult, you can create one simply by doing the following : 

                List<URL> classLoaderURLs = new ArrayList<URL>();
                // add URLs to JARs to the list
                URLClassLoader urlClassLoader = new URLClassLoader(classLoaderURLs.toArray(new URL[classLoaderURLs.size()]), this.getClass().getClassLoader());
     
                    Class customClass = urlClassLoader.loadClass("org.jahia.server.CustomClass");
                    CustomInterface customInterface = (InContextRunner) customClass.newInstance();
                    customInterface.execute(anyParameters, urlClassLoader);
     
    Regards,
      Serge... 

     

    Serge Huber (shuber)

    Number of messages  271
    Registration date
    Contact
    Share
    Feedback

    Get in touch

    Whether you are a current user or if you are just evaluating Jahia, we are here to help.

    Contact us

    Share this page