0

i am neophyte to test class. here's my issue. i use custom controller and i use public string to get the all value from my vf. how do i pass all the value on my test class ?

--Apex Class--

public class step1Form{

public String firstName{get;set;}
public String lastName{get;set;}
public String phone{get;set;}
public String email{get;set;}
public String city{get;set;}
public String company{get;set;}
public String web2leadId{get;set;}
Public Web2Lead_From_Site__c web2lead{get;set;}

public step1Form(){

    web2lead = new Web2Lead_From_Site__c();

}

public PageReference Next(){

        web2lead.Last_Name__c = firstName+' '+lastName;
        web2lead.Phone__c = phone;
        web2lead.City__c = city;
        web2lead.Company__c =firstName+' '+lastName;
        web2lead.Email__c = email;
        insert web2lead;

        web2leadId = web2lead.Id;

        PageReference step2Form = new PageReference('http://lp.amalan.id/step2');
        step2Form.getParameters().put('id',web2lead.Id);
        step2Form.setRedirect(true);
        return step2Form;
}
}

--Visualforce--

<apex:page controller="step1Form" showHeader="false" standardStylesheets="false" sidebar="false">
       <div class="form-group"> 
              <apex:inputtext value="{!firstName}"/>
       </div> 
       <div class="form-group"> 
              <apex:inputtext value="{!lastName}" />
       </div> 
       <div class="form-group"> 
              <apex:inputtext value="{!phone}" />
       </div> 
       <div class="form-group"> 
              <apex:inputtext value="{!email}"/>
       </div>
       <div class="form-group">
            <apex:inputHidden value="{!city}" id="cityId"/> 
            <select class="form-control" form="contact" onchange="document.getElementById('{!$Component.cityId}').value = this.value;"> 
            <option value="">Kota</option> 
           <option value="DKI Jakarta">DKI Jakarta</option> 
           <option value="Lainnya">Lainnya</option> 
           </select>
      </div>

--Test Class-- (coverage 38%)

@isTest
private class step1TestClass{
     static testMethod void insertTest() 
     {
         step1Form step1 =new step1Form();
         step1.firstName='first';
         step1.lastName='last';
         step1.email='test@gmail.com';
         step1.phone='012912';
         step1.city='DKI Jakarta';

         Web2Lead_From_Site__c w2l = new Web2Lead_From_Site__c();
         w2l.Last_Name__c = step1.firstName+' '+step1.lastName;
         w2l.Email__c= step1.email;
         w2l.Company__c = step1.firstName+' '+step1.lastName;
         w2l.Phone__c = step1.phone;
         w2l.City__c = step1.city;
         insert w2l;


     }

}

--Coverage-- Coverage

and i need to pass the web2leadId via pageReference to my next controller

Thanks

4
  • 1
    when you initialize the controller in test class there you need to set the controller string using controller object.. It will be very useful If you share us the test class code so we can check with that.. developer.salesforce.com/trailhead/module/apex_testing – Ratan Paul Jan 12 '16 at 4:42
  • @Ratan already edit my post. please check :) – Febrian Rizky Jan 12 '16 at 4:59
  • Can you pls add a screenshot of which part is not covered ? – Ratan Paul Jan 12 '16 at 5:00
  • Febrian check @Tushar ans.. That will work for you.. – Ratan Paul Jan 12 '16 at 5:06
1

I think what you need to here is

@isTest
private class step1TestClass{
     static testMethod void insertTest() 
     {
         step1Form step1 =new step1Form();
         step1.firstName='first';
         step1.lastName='last';
         step1.email='test@gmail.com';
         step1.phone='012912';
         step1.city='DKI Jakarta';

         web2lead.Last_Name__c = step1.firstName+' '+step1.lastName;
         web2lead.Email__c= step1.email;
         web2lead.Company__c = step1.firstName+' '+step1.lastName;
         web2lead.Phone__c = step1.phone;
         web2lead.City__c = step1.city;
         step1.Next();


     }

}

Now it will call your method , Assign the value , Insert the record and pass this to parameters and it will increase the code coverage for you.

6
  • Tushar we can assign step1.Next(); value to page reference and we can put a systemassertEquals to check correct pagereference . – Ratan Paul Jan 12 '16 at 5:10
  • already try it but it get error like System.DmlException: Insert failed. First exception on row 0 with id a3RO00000002yoWMAQ; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id] – Febrian Rizky Jan 12 '16 at 5:12
  • @FebrianTarigan you need to remove insert w2l; line from test class because insert operation already happening in your controller – Ratan Paul Jan 12 '16 at 5:17
  • now it works ! after i remove the insert w2l; thanks @Ratan and @TusharSharma – Febrian Rizky Jan 12 '16 at 5:28
  • @FebrianTarigan nice it worked... – Ratan Paul Jan 12 '16 at 5:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.