Announcement Announcement Module
Collapse
No announcement yet.
testing a SimpleFormController Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • testing a SimpleFormController

    how do you test a SimpleFormController, a basic code/example will greatly help. i am using this reference http://jroller.com/page/jwboring/200..._controller_in
    but to no avail.

    i am being told that onSubmit(...) is not visible
    and/or
    Code:
    ApplicationObjectSupport instance [billing.mock.CycleForm@157aa53] does not run in an ApplicationContext
    	at org.springframework.context.support.ApplicationObjectSupport.getApplicationContext(ApplicationObjectSupport.java:130)
    my testCode is

    Code:
    public void testHttpForm(){
    
    		MockHttpServletRequest request = new MockHttpServletRequest( "POST","/cycles.htm" );
    		MockHttpSession session = (MockHttpSession) request.getSession();
    		MockHttpServletResponse response = new MockHttpServletResponse();
    
    		CycleForm classBeingTested = new CycleForm( );
    		String formView = "fail";
    		String successView = "success";
    		classBeingTested.setFormView( formView );
    		classBeingTested.setSuccessView( successView );
    
    		Object dummyObject=new Object();
    		
    		BindException errors = new BindException(dummyObject, "command" );
    		try{
    		ModelAndView mv = classBeingTested.call( (HttpServletRequest)request, (HttpServletResponse)response, dummyObject, errors );
    		}catch(Exception e){
    			e.printStackTrace(System.out);
    		}
    		
    	}
    maybe someone can point me to the right direction. thanks a lot

  • #2
    how do you test a SimpleFormController...i am being told that onSubmit(...) is not visible
    If you've extended from SimpleFormController, you should be able to test onSubmit(...). Have you got the right signature?

    Code:
    ApplicationObjectSupport instance [billing.mock.CycleForm@157aa53] does not run in an ApplicationContext
    Why do you need to get the ApplicationContext? Maybe post the code for CycleForm.

    Comment


    • #3
      yah it's working as a webapp, since im in testing i create dummy objects of my actual objects and put it in an xml and retrieves a copy via application context.

      Code:
      public class CycleForm extends BaseForm {
      	
      
      	public CycleForm() {
      		// need a session to hold the formBackingObject
      		setSessionForm(true);
      	}
      
      	private FiscalMap loadMe(FiscalMap fiscalMap) {
      
      		fiscalMap = (FiscalMap) this.getApplicationContext().getBean(
      				"fiscalMap");
      		return fiscalMap;
      	}
      
      	private LeaseGroup loadMe2(LeaseGroup leaseGroup){
      		leaseGroup =(LeaseGroup) this.getApplicationContext().getBean("leaseGroup");
      		return leaseGroup;
      	}
      	
      	private ArrayList loadPeriods(ArrayList billingPeriods, String fiscalYear, String billingType, FiscalMap fiscalMap, LeaseGroup leaseGroup){
      		
      		FiscalCycle fiscalCycle = new FiscalCycle();
      		BillingCycle billingCycle = new BillingCycle();
      		try{
      		fiscalCycle = fiscalMap.getFiscalCycle(fiscalYear);}
      		catch(Exception e){System.out.println(":::Error in getting Fiscal Cycle" + e.getMessage()); }
      		try{
      		billingCycle = fiscalCycle.getBillingCycle(billingType);}
      		catch(Exception e){System.out.println(":::Error in getting Billing Cycle" + e.getMessage());}
      		try{
      		billingPeriods = billingCycle.getBillingPeriods();}
      		catch(Exception e){System.out.println(":::Error in getting Billing Periods" + e.getMessage());}
      		
      		return billingPeriods;
      	}
      	
      	
      	protected Map referenceData(HttpServletRequest request)
      			throws ServletException {
      
      		Map refData = new HashMap();
      
      		String fiscalYear = request.getParameter("fiscalYear");
      //		if(fiscalYear == null)
      //			fiscalYear = (String) request.getSession().getAttribute("fiscalYear");
      //		else
      //			request.getSession().setAttribute("fiscalYear",fiscalYear);
      				
      		String billingType = request.getParameter("billingType");
      //		if(billingType == null)
      //			billingType = (String) request.getSession().getAttribute("leaseGroup");
      //		else
      //			request.getSession().setAttribute("leaseGroup",billingType);
      		
      		FiscalMap fiscalMap = new FiscalMap();
      		fiscalMap = loadMe(fiscalMap);
      		String[] fiscalYears = fiscalMap.getFiscalYears();
      		LeaseGroup leaseGroup = new LeaseGroup();
      		leaseGroup = loadMe2(leaseGroup);
      		refData.put("map", fiscalMap);
      		refData.put("leaseGroup", leaseGroup);
      		refData.put("selectedYear", fiscalYear);
      		refData.put("selectedType", billingType);
      		return refData;
      	}
      
      	protected Object formBackingObject(HttpServletRequest request)
      			throws ServletException {
      		
      		HashMap refData = new HashMap();
      		
      		ArrayList cycles = new ArrayList();
      		ArrayList billingPeriods = new ArrayList();
      		
      //		System.out.println("=======================");
      //		System.out.println("chosen year:: "+ request.getParameter("fiscalYear"));
      //		System.out.println("chosen type:: "+ request.getParameter("billingType"));
      		
      		String fiscalYear = request.getParameter("fiscalYear");
      		if(fiscalYear == null)
      			fiscalYear = (String) request.getSession().getAttribute("fiscalYear");
      		else
      			request.getSession().setAttribute("fiscalYear",fiscalYear);
      				
      		String billingType = request.getParameter("billingType");
      		if(billingType == null)
      			billingType = (String) request.getSession().getAttribute("leaseGroup");
      		else
      			request.getSession().setAttribute("leaseGroup",billingType);
      		
      		FiscalMap fiscalMap = new FiscalMap();
      		fiscalMap = loadMe(fiscalMap);
      		LeaseGroup leaseGroup = new LeaseGroup();
      		leaseGroup = loadMe2(leaseGroup);
      		if(fiscalYear != null && billingType != null){
      			billingPeriods = loadPeriods(billingPeriods,fiscalYear,billingType,fiscalMap,leaseGroup);
      			cycles = billingPeriods;
      			
      			HashMap sales = new HashMap();
      			sales = (HashMap) this.getApplicationContext().getBean("sales");
      			SysParams sysParams = (SysParams) this.getApplicationContext().getBean("sysParams");
      			ArrayList paramDtls= (ArrayList) this.getApplicationContext().getBean("sysParamDtls");
      			ArrayList leases = new ArrayList();
      			leases = leaseGroup.getLeases(0);
      			
      			for&#40;int i=0; i<billingPeriods.size&#40;&#41;;i++&#41;&#123;
      				for&#40;int j=0; j<leases.size&#40;&#41;;j++&#41;&#123;
      				BillingPeriod bp = new BillingPeriod&#40;&#41;;
      					bp = &#40;BillingPeriod&#41;billingPeriods.get&#40;i&#41;;
      					Lease lease = &#40;Lease&#41;leases.get&#40;j&#41;;
      					LeasableSpace lSpace = &#40;LeasableSpace&#41; lease.getLeasableSpace&#40;&#41;;
      					String stallNo = lSpace.getStallNo&#40;&#41;;
      					bp.setSales&#40;sales&#41;;
      					ArrayList s = bp.getSales&#40;stallNo&#41;;
      					
      					ParamDtls pd = &#40;ParamDtls&#41;paramDtls.get&#40;6&#41;;
      					int daysNum = Integer.parseInt&#40;pd.getRate&#40;&#41;&#41;;
      					
      					// determine sales status
      					if&#40;s.size&#40;&#41; != daysNum&#41;&#123;
      						bp.setSalesStatus&#40;"Not Available"&#41;;
      						break;
      					&#125;
      					else&#123;
      						bp.setSalesStatus&#40;"Done"&#41;;
      					&#125;
      				&#125;
      			&#125;
      			
      //			determine utility status
      			for&#40;int i=0; i<billingPeriods.size&#40;&#41;;i++&#41;&#123;
      				for&#40;int j=0; j<leases.size&#40;&#41;;j++&#41;&#123;
      				BillingPeriod bp = new BillingPeriod&#40;&#41;;
      					bp = &#40;BillingPeriod&#41;billingPeriods.get&#40;i&#41;;
      					Lease lease = &#40;Lease&#41;leases.get&#40;j&#41;;
      					LeasableSpace lSpace = &#40;LeasableSpace&#41; lease.getLeasableSpace&#40;&#41;;
      					String stallNo = lSpace.getStallNo&#40;&#41;;
      					
      					HashMap utilities = new HashMap&#40;&#41;;
      					utilities = &#40;HashMap&#41;this.getApplicationContext&#40;&#41;.getBean&#40;"utilityUsages"&#41;;
      					bp.setUtilityUsages&#40;utilities&#41;;
      					ArrayList utility = new ArrayList&#40;&#41;;
      					utility = lease.getUtility&#40;&#41;;
      					ArrayList utilityUsage = new ArrayList&#40;&#41;;
      					utilityUsage = bp.getUtilityUsages&#40;stallNo&#41;;
      
      					if &#40;utilityUsage.containsAll&#40;utility&#41;&#41;&#123;
      						bp.setUtilitiesStatus&#40;"Done"&#41;;
      					&#125;
      					else&#123;
      						bp.setUtilitiesStatus&#40;"Not Available"&#41;;
      					&#125;
      				&#125;
      			&#125;
      			
      			//determine parameter status
      			for&#40;int i=0; i<billingPeriods.size&#40;&#41;;i++&#41;&#123;
      				BillingPeriod bp = new BillingPeriod&#40;&#41;;
      				bp = &#40;BillingPeriod&#41;billingPeriods.get&#40;i&#41;;
      				if &#40;bp.getSysParams&#40;&#41; == null&#41;&#123;
      					System.out.println&#40;"Im in 1"&#41;;
      					bp.setParameterStatus&#40;"?"&#41;;
      					System.out.println&#40;bp.getParameterStatus&#40;&#41;&#41;;
      				&#125;
      				else&#123;
      					System.out.println&#40;"Im in 2"&#41;;
      					bp.setParameterStatus&#40;"Done"&#41;;
      					System.out.println&#40;bp.getParameterStatus&#40;&#41;&#41;;
      					&#125;
      			&#125;
      		&#125;
      		if &#40;&#40;fiscalYear!=null&#41;  && !fiscalYear.equals&#40;""&#41;&#41;&#123;
      			try&#123;
      				FiscalCycle fiscalCycle = fiscalMap.getFiscalCycle&#40;fiscalYear&#41;;
      				BillingPeriodHelper  bph = new BillingPeriodHelper&#40;fiscalCycle,billingType,leaseGroup,getApplicationContext&#40;&#41;&#41;;
      				BillingCycle cycle = bph.prepareCycle&#40;&#41;;
      				request.getSession&#40;&#41;.setAttribute&#40;"BillingCycle",cycle&#41;;
      				System.out.println&#40;"\n\n\t\t--->Just sessioned Cycle&#58;"+cycle.toString&#40;&#41;&#41;;
      			&#125;
      			catch&#40;IOException e&#41;&#123;throw new ServletException&#40;e.getMessage&#40;&#41;&#41;;&#125;
      		&#125;
      		return cycles;
      	&#125;
      
      	protected void onBind&#40;HttpServletRequest request, Object command&#41; &#123;
      		String selected&#91;&#93;;
      		selected = request.getParameterValues&#40;"selectedPeriod"&#41;;
      		request.getSession&#40;&#41;.setAttribute&#40;"selectedPeriod", selected&#41;;	
      		System.out.println&#40;"\n\n\t\t--->binding..."&#41;;
      	&#125;
      
      	/** Method inserts a new Pet */
      	protected ModelAndView onSubmit&#40;HttpServletRequest request, HttpServletResponse response, Object command, BindException errors&#41;  throws ServletException &#123;
      		System.out.println&#40;"\n\n\t\tRedirectView...."&#41;;
      		FiscalMap fiscalMap = &#40;FiscalMap&#41;getApplicationContext&#40;&#41;.getBean&#40;"fiscalMap"&#41;;
      		LeaseGroup lg = &#40;LeaseGroup&#41;getApplicationContext&#40;&#41;.getBean&#40;"leaseGroup"&#41;;
      		//String fYear = request.getParameter&#40;"fiscalYear"&#41;;
      		//String lGroup = request.getParameter&#40;"leaseGroup"&#41;;
      		
      		String fYear = &#40;String&#41; request.getSession&#40;&#41;.getAttribute&#40;"fiscalYear"&#41;;
      		String lGroup = &#40;String&#41; request.getSession&#40;&#41;.getAttribute&#40;"leaseGroup"&#41;;
      
      		ArrayList<BillingPeriod> incPeriods = new ArrayList<BillingPeriod>&#40;&#41;;
      		FiscalCycle fiscalCycle = null;
      		BillingCycle cycle = null;
      		String&#91;&#93; periodIds = request.getParameterValues&#40;"selectedPeriod"&#41;;
      		BillingPeriodHelper bph = null;
      		
      		System.out.println&#40;"\n\n\t\t--->period IDs&#58;"+periodIds&#41;;
      		if &#40;periodIds!=null&#41;&#123;
      			try&#123;
      				cycle = &#40;BillingCycle&#41;request.getSession&#40;&#41;.getAttribute&#40;"BillingCycle"&#41;;
      				
      				
      				bph = new BillingPeriodHelper&#40;cycle,getApplicationContext&#40;&#41;&#41;;
      				//fiscalCycle = fiscalMap.getFiscalCycle&#40;fiscalMap.getFiscalYear&#40;fYear&#41;&#41;;
      				//cycle = fiscalCycle.getBillingCycleById&#40;lGroup&#41;;
      				cycle =bph.setComputationParameters&#40;&#41;;
      				int idx  = cycle.getPeriodCount&#40;&#41;;
      				System.out.println&#40;"\n\n\t\t--->Cycle from session&#58;"+cycle.toString&#40;&#41;+"priod count&#58;"+idx&#41;;
      				for&#40;int x=0; x < periodIds.length; x++&#41;&#123;
      					
      					for &#40;int y=0;y<idx;y++&#41;&#123;
      						if &#40;cycle.getBillingPeriod&#40;y&#41;.getId&#40;&#41;.equals&#40;periodIds&#91;x&#93;&#41;&#41; &#123;
      							incPeriods.add&#40;cycle.getBillingPeriod&#40;y&#41;&#41;;
      							break;
      						&#125;
      					&#125;
      				&#125;
      			&#125;
      			catch&#40;IOException e&#41;&#123;throw new ServletException&#40;e.getMessage&#40;&#41;&#41;;&#125;
      			HashMap<String, SOA&#91;&#93;> soas = new HashMap<String, SOA&#91;&#93;>&#40;&#41;;
      			for &#40;BillingPeriod b&#58;incPeriods&#41;&#123;
      				b.generate&#40;&#41;;
      				soas.put&#40;b.getId&#40;&#41;,b.processBills&#40;&#41;&#41;;
      			&#125;
      			request.getSession&#40;&#41;.setAttribute&#40;"SOA_Array",soas&#41;;
      		&#125;else throw new ServletException&#40;"No Billing Period selected."&#41;;
      		System.out.println&#40;"\n\n\t\tRedirectView...."&#41;;
      		return new ModelAndView&#40;new RedirectView&#40;getSuccessView&#40;&#41;&#41;&#41;;
      	&#125;
      
      	public ModelAndView call&#40;HttpServletRequest request, HttpServletResponse response, Object command, BindException errors&#41;  throws ServletException &#123;
      		return onSubmit&#40;request,response,command,errors&#41;;
      	&#125;
      	protected ModelAndView handleInvalidSubmit&#40;HttpServletRequest request, HttpServletResponse response&#41;
      			throws Exception &#123;
      		return disallowDuplicateFormSubmission&#40;request, response&#41;;
      	&#125;
      
      &#125;
      it's a bit crappy because i am testing/developing directly on the application server, it would be nice if i can test it inside eclipse i.e. via junit.

      thx

      Comment


      • #4
        i am being told that onSubmit(...) is not visible
        onSubmit(...) is protected so make sure your test class is in the same package. This is a standard technique to overcome this issue.

        Comment


        • #5
          thx it is working now.

          i just including my spring servlet xml, and instead of directly instantiating my controller class, i get it from the applicationContext.

          thanks guys...

          Comment

          Working...
          X