Announcement Announcement Module
Collapse
No announcement yet.
Testing troubles with Struts / Spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Testing troubles with Struts / Spring

    Hello,

    I am testing a Spring / Struts application. I have Struts Actions defined within my servlet xml configuration as spring beans.

    -----
    <beans>
    <!-- Action BEANS for SPRING -->
    <bean name="/user" class="com.iit.ert.web.UserAction" singleton="false">
    <property name="userManager"><ref bean="userManager"/></property>
    <property name="roleManager"><ref bean="roleManager"/></property>
    </bean>
    <bean name="/role" class="com.iit.ert.web.RoleAction" singleton="false">
    <property name="roleManager"><ref bean="roleManager"/></property>
    </bean>
    <bean name="/vehicle" class="com.iit.ert.web.VehicleAction" singleton="false">
    <property name="vehicleManager"><ref bean="vehicleManager"/></property>
    <property name="vehicleTypeManager"><ref bean="vehicleTypeManager"/></property>
    </bean>
    <bean name="/vehicleType" class="com.iit.ert.web.VehicleTypeAction" singleton="false">
    <property name="vehicleTypeManager"><ref bean="vehicleTypeManager"/></property>
    </bean>
    </beans>
    -----

    These Struts Action beans have this type of setup in the class.

    -----
    public class UserAction extends DispatchAction {
    private static Log _log = LogFactory.getLog(com.iit.ert.web.UserAction.class );
    private UserManager userManager = null;
    private RoleManager roleManager = null;

    public void setUserManager(UserManager userManager) {
    this.userManager = userManager;
    }

    public void setRoleManager(RoleManager roleManager) {
    this.roleManager = roleManager;
    }
    .....
    .....
    -----

    As you can see I have a userManager and roleManager property which are also spring beans defined in xml configuration applicationContext.xml. The userManager and roleManager beans are defined as follows:

    -----
    <!-- USER MANAGER-->
    <bean id="userManager" class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
    <property name="transactionManager">
    <ref local="transactionManager"/>
    </property>
    <property name="target">
    <ref local="userManagerTarget"/>
    </property>
    <property name="transactionAttributes">
    <props>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    <prop key="remove*">PROPAGATION_REQUIRED</prop>
    <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
    </props>
    </property>
    </bean>

    <bean id="userManagerTarget"
    class="com.iit.ert.service.impl.UserManagerImpl">
    <property name="userDAO"><ref local="userDAO"/></property>
    </bean>


    <!-- ROLE MANAGER-->
    <bean id="roleManager" class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
    <property name="transactionManager">
    <ref local="transactionManager"/>
    </property>
    <property name="target">
    <ref local="roleManagerTarget"/>
    </property>
    <property name="transactionAttributes">
    <props>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    <prop key="remove*">PROPAGATION_REQUIRED</prop>
    <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
    </props>
    </property>
    </bean>

    <bean id="roleManagerTarget"
    class="com.iit.ert.service.impl.RoleManagerImpl">
    <property name="roleDAO"><ref local="roleDAO"/></property>
    </bean>
    -----

    When I run my MockStrutsTestCase tests all beans are loaded but for some reason my Struts Action beans, which are prototypes, do not have their properties wired. Both managers are null when they are used, which leads me to believe its a wiring issue on my part. In addition to a NullPointerException when attempting to use userManager, I also get this debug message on context load:
    <No XML 'id' specified - using '/user' as ID and [] as aliases>

    It seems like it deals with this fine by setting /user to the bean's id. But I am not sure if this will work as you can not set the bean's id to '/user' in the xml configuration because of an invalid format for xml name.

    I am just trying to make sense of this. IMO, it seems like a problem in wiring the prototype bean, but I am new to spring.

    If anyone can help me, or need additional info, let me know.

    Thanks in advance,

    Anthony Bargnesi
    Software Developer
    Institute of Information Technology

  • #2
    MockStrutsTestCase loads/instanciates Struts Actions on its own from struts-config.xml. The Actions it uses are not aware/managed by Spring IoC.
    When using Struts with Spring, I usually extends Spring's XxxActionSupport. Testing such Actions is very easy. All you need is to create a org.springframework.web.context.support.StaticWebA pplicationContext, populate it with required beans (mainly Mocks) and store it in MockStrutsTestCase servletContext:
    Code:
        ServletContext sc = getActionServlet&#40;&#41;.getServletContext&#40;&#41;;
        StaticWebApplicationContext wac = new StaticWebApplicationContext&#40;&#41;;
        //configure some beans for IoC
        wac.refresh&#40;&#41;;
        sc.setAttribute&#40;StaticWebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac&#41;;
    HTH

    Comment


    • #3
      Thanks for the help...

      Hey thanks for the reply. It seems I left out the setting of the action type to org.springframework.web.struts.DelegatingActionPro xy. When I did that all was well. Since I wanted the Action tests to be easy to write without much setup I declare
      <plug-in className="org.springframework.web.struts.ContextL oaderPlugIn">
      <set-property property="contextConfigLocation"
      value="/WEB-INF/applicationContext.xml,
      /WEB-INF/action-servlet.xml"/>
      </plug-in>

      in the struts-config.xml file so that it loads all my beans and the action beans for me. Then no configuration is need within your action tests.

      Thanks again for the solution...

      Anthony Bargnesi
      Software Developer
      Institute of Information Technology

      Comment


      • #4
        same issues with testing struts actions with spring

        I have the plugin configuration all setup like I am supposed to. I even setup the <controller> instead of using the DelegatingActionProxy (as stated here).

        But when I run my test I get this error:

        --------------------------------------------------------
        No WebApplicationContext found: no ContextLoaderListener registered?
        --------------------------------------------------------

        I think I understand. The MockStrutsTestCase is not really looking at web.xml so the spring ContextLoaderListener is not getting loaded. So how do I make my test case see the web.xml properly?


        Also, if I put the code above (ServletContext sc = getActionServlet().....) into my test case, I get a different error:

        --------------------------------------------------------
        Error creating bean with name '/groups/search' defined in ServletContext resource [/WEB-INF/action-servlet.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No bean named 'groupTarget' is defined: org.springframework.beans.factory.support.DefaultL istableBeanFactory defining beans []; root of BeanFactory hierarchy
        org.springframework.beans.factory.NoSuchBeanDefini tionException: No bean named 'groupTarget' is defined: org.springframework.beans.factory.support.DefaultL istableBeanFactory defining beans []; root of BeanFactory hierarchy
        --------------------------------------------------------

        But I clearly have a bean with that name. I have tried with <bean id="" and <bean name="". I have had the bean definitions in the applicationContext.xml and in the action-servlet.xml. Nothing seems to work.

        Any idea on how this might be resolved would be very helpful. Thanks.


        Charlie
        [/url]

        Comment


        • #5
          This thread on the user mailing list might help.

          Matt

          Comment

          Working...
          X