function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
arun rupesharun rupesh 

i want test class for standard coontroller extesion class

public class Standardextensionclass {
    public contact con;
    public blob picture{get;set;}
    private ApexPages.StandardController st;
    public standardextensionclass(Apexpages.StandardController st){
        this.con=(contact)st.getrecord();
    }
    public pagereference savewithImage(){
        pagereference pr;
        try{
            insert con;
            if(picture !=null){
                Attachment at=new Attachment();
                at.Body=picture;
                at.name='Contact_' + con.id + '.jpg';
                at.ParentId=con.id;
                 at.ContentType = 'application/jpg';
        insert at;
     con.Picture_path__c = '/servlet/servlet.FileDownload?file='+ at.id;
                update con;
       Pr = new PageReference('/'+con.id);
       pr.setRedirect(true);
    
            } 
  
  } catch(Exception  e){
   system.debug('Error Message==>'+e);
  }
 
  return pr;
 } 

}
Best Answer chosen by arun rupesh
Suraj TripathiSuraj Tripathi

Hi arun rupesh,

Please try this for your test class it covers your 94% code coverage.

@isTest
public class Standardextensionclass_Test {
    static testmethod void Test(){
        contact con = new contact();
        con.LastName = 'Test';
        Test.startTest();
        ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(con);
        Standardextensionclass stdExt = new Standardextensionclass(sc);
        stdExt.con = con;
        stdExt.picture = Blob.valueOf('sample.png');
        stdExt.savewithImage();
        Test.stopTest();
    }
}

Regards,

Suraj

All Answers

sfdcMonkey.comsfdcMonkey.com
hi arun, use below test class :
@isTest

public class testSample {
static testMethod void testCheck() {
  contact testcontact = new contact();
 testcontact.lastName = 'Test contact';
    
 Test.StartTest(); 
  ApexPages.StandardController sc = new ApexPages.StandardController(testcontact);
  Standardextensionclass testAccPlan = new Standardextensionclass(sc);

  PageReference pageRef = Page.yourVFPageName; // ***change your VF page Name here**
  pageRef.getParameters().put('id', String.valueOf(testcontact.Id));
  
    Test.setCurrentPage(pageRef);
    
    testAccPlan.picture = Blob.valueOf('test');
    testAccPlan.savewithImage();
 Test.StopTest();
}
}
i hope it helps you.
      Let me inform if it helps you and kindly mark it best answer if it helps you so it make proper solution for others
 thanks
    sfdcmonkey.com
 
Suraj TripathiSuraj Tripathi

Hi arun rupesh,

Please try this for your test class it covers your 94% code coverage.

@isTest
public class Standardextensionclass_Test {
    static testmethod void Test(){
        contact con = new contact();
        con.LastName = 'Test';
        Test.startTest();
        ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(con);
        Standardextensionclass stdExt = new Standardextensionclass(sc);
        stdExt.con = con;
        stdExt.picture = Blob.valueOf('sample.png');
        stdExt.savewithImage();
        Test.stopTest();
    }
}

Regards,

Suraj

This was selected as the best answer
arun rupesharun rupesh
Hi Suraj,

thank for your reply .your code run properly and it reaches 100% code coverage
 
Suraj TripathiSuraj Tripathi
Hi arun, Thanks for choosing my answer as a best.