Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
220 views
in Technique[技术] by (71.8m points)

salesforce - test class for inserting contacts through the following class

I have one apex class which inserts the contacts.i wrote one test class for that where its is passing but the code coverage is zero.can somebody suggest what i missed? test class: @isTest public class TestReferalAccessclass { static testMethod void ReferalAccessclassMethod() { Test.StartTest(); Contact c=new Contact(FirstName='fname',LastName = 'lname',Email = '[email protected]',Phone = '9743800309'); insert c; System.AssertNotEquals(Null, c.Id); Test.StopTest();

 }    

}
apex class:
    public without sharing class ReferalAccessclass {
    public String inputID{get; set;}
    public String firstName{get; set;}
    public String lastName{get; set;}
    public String email{get; set;}
    public String phone{get; set;}
    public Decimal exp{get; set;}
    public String location{get; set;}
    public contact con{get;set;}

    Public attachment objAttachment{get; set;}

    public ReferalAccessclass(ApexPages.StandardController controller) 
      { 

    objAttachment = new Attachment();

    }

  public void saveInformation()
{
try{
    IF(inputID != 'NULL'){
    con = [SELECT ID,Name,FirstName,LastName,Email,Phone,Years_of_Experience__c,Location__c FROM Contact where ID =: inputID ];

    con.FirstName = firstName;
    con.LastName = lastName;
    con.Email = email;
    con.Phone = phone; 
    }
    update con;
    objAttachment.ParentId = con.id;
    Insert objAttachment;

   }
  catch(exception e){} 
  ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Thank you for your valuable response');

//return null;

}

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You didn't invoke your actual class from test class. That's why its not giving code coverage. Try this test class.

@isTest public class TestReferalAccessclass { 
    static testMethod void ReferalAccessclassMethod() { 

        Contact c=new Contact(
            FirstName='fname',
            LastName = 'lname',
            Email = '[email protected]',
            Phone = '9743800309'); 
        insert c; 
        Test.StartTest(); 
        System.AssertNotEquals(Null, c.Id); 

        ApexPages.StandardController sc = new ApexPages.StandardController(c);
        ReferalAccessclass refClass = new ReferalAccessclass(sc);
        refClass.inputID = c.id;
        refClass.firstName = c.id;
        refClass.lastName = c.id;
        refClass.email = c.id;
        refClass.phone = c.id;
        refClass.con = c;
        refClass.saveInformation();

        Test.StopTest();
    }    
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...