+ Start a Discussion
a!a! 

Test Class

Hi to All,

please write test class for this, its urgernt.

 

 

public with sharing class controlll {  
public cont__c co;
public string country;
public   string transactionid;
public string cardtype;
public controlllApexPages.StandardController controller)
{
    pc = (cont__c)controller.getrecord();
       
}
    public pagereference p1(){
    if(pc.country__c == 'United States')
    {
    country ='US';
      
     asdf3 c = new asdf3();
    p.city = pc.city__c;
    p.state = pc.state__c;

insert pc;
     }
    else
    {
     if(pc.country__c == 'Aus')
    {
    country ='AU';
      
 
    asdf4 p = new asdf4();
    p.city = pc.city__c;
    p.state = pc.state__c;

insert pc;
     }
   
   }   
          pagereference p4 = page.successpay ;
             p4.getParameters().put('transid', transactionid);
             return p4.setredirect(true);    
                }
            public cont__c  getp(){
            return pc;

}

 

 

 @IsTest(SeeAllData=true) public static void testcontrolll() {
        // Instantiate a new controller with all parameters in the page
        controlll obj = new controll();
        obj.country = 'INDIA';
        obj.transactionid = '4455660018567896';
        obj.cardtype = 'qwerty';
        obj.p1();
        System.assertEquals(obj.p1(),null);

 

}

 

 

i written test class like this, but it covers only 12%

 

am new to test class, please help me hou to  write the remaining testmethod, with code

SamuelDeRyckeSamuelDeRycke

Have you read  the documentation or and introduction to apex testing  ? 

 

Your test method:

 

@IsTest(SeeAllData = true) public static void testcontrolll() {
    // Instantiate a new controller with all parameters in the page
    controlll obj = new controll();
    obj.country = 'INDIA';
    obj.transactionid = '4455660018567896';
    obj.cardtype = 'qwerty';
    obj.p1();
    System.assertEquals(obj.p1(), null);
} 

 

public pagereference p1() {
    if (pc.country__c == 'United States') {
        country = 'US';
        asdf3 c = new asdf3();
        p.city = pc.city__c;
        p.state = pc.state__c;
        insert pc;
    } else {
        if (pc.country__c == 'Aus') {
            country = 'AU';
            asdf4 p = new asdf4();
            p.city = pc.city__c;
            p.state = pc.state__c;
            insert pc;
        }
    }
    pagereference p4 = page.successpay;
    p4.getParameters().put('transid', transactionid);
    return p4.setredirect(true);
}
public cont__c getp() {
    return pc;
}

Did you look at the test result, and the line that got covered ? That gives you a good identitfication of how your test method should look like (in the test results, scroll down to the class and click the "coverage%").

Your test runs  with a obj.Country = 'India', but your logic is based around 'US' and 'Aus', run it once with an object having country 'US' and once with country = 'Aus' for full coverage of the method. 

Test all methods, by running them all in your test test method, and run them as many different times as you have possible paths of execution within your method itself. Making sure every path of execution will be tested.

 

 

 

a!a!

Hi,

 

 

public with sharing class controlll {  
public cont__c co;
public string country;
public   string transactionid;
public string cardtype;
public controlllApexPages.StandardController controller)
{
    pc = (cont__c)controller.getrecord();
       
}
    public pagereference p1(){
    if(pc.country__c == 'United States')
    {
    country ='US';
      
     asdf3 c = new asdf3();
    p.city = pc.city__c;
    p.state = pc.state__c;

insert pc;
     }
    else
    {
     if(pc.country__c == 'Aus')
    {
    country ='AU';
      
 
    asdf4 p = new asdf4();
    p.city = pc.city__c;
    p.state = pc.state__c;

insert pc;
     }
   
   }   
          pagereference p4 = page.successpay ;
             p4.getParameters().put('transid', transactionid);
             return p4.setredirect(true);    
                }
            public cont__c  getp(){
            return pc;

}

 

 

 these lines are not covered in the test coverage, asam new for writing testcalsses, please hepl hoe to write test class for this.

 i change the country value to US and Aus, but no change in the code coverage,

 

Thanks

SamuelDeRyckeSamuelDeRycke

Yes ... you're setting the controller country in your rest.  The country that needs to change is the country of the record your controller is based around.  a cont__c object, its that country field that needs to change.

 

You're testing a controller extention, which relies on the controller, this implies you also need to include that in your test code. Have a look at the following code snippet as referende

 

Contact c = new Contact();       
ApexPages.StandardController stdController = new ApexPages.StandardController( c );             
Contactedit controller = new Contactedit ( stdController );

 

Please read and study (= understand) those links I posted.