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
Karthikeya Mullunja RamakrishnaKarthikeya Mullunja Ramakrishna 

Help in writing the test class

My controller class -- 

Public Class ContactUpdateServer { 
    public Contact objContact{get;set;}
    public String currentRecordId {get;set;}
    public Contact con{get;set;}
   
    public List<contact> up = new List<Contact>();
    
    List<ContactCreationCalloutParser.Profile> lstProfilemain = new List<ContactCreationCalloutParser.Profile>() ;
    
    public ContactUpdateServer(ApexPages.StandardController controller) {
     Boolean isValid = False;
    currentRecordId  = ApexPages.CurrentPage().getparameters().get('id');
        con = [select SSO_Number__c,Name from Contact where id =: currentRecordId ];
        system.debug('----- sso --- ' +con);
        string searchstring = con.SSO_Number__c;
        if(searchString.length() == 9){
                // Check if search string is 9 digit long numeric value. i.e SSO number.
                   if(searchString.isNumeric()){
                     isValid = True;
       }}
       if(isValid){
       system.debug('----isvalid----' +isValid);
        ContactServer();}
    }
 Public Pagereference Goback(){
    Contact objContact = new Contact();
    objContact.Id = ApexPages.CurrentPage().getparameters().get('id');
    PageReference pageRef = new PageReference('/' + objContact.Id);
     pageRef.setRedirect(true);
      return pageRef;
      }
    

Test Class : 

@isTest 
private Class ContactUpdateServerTest{
static testMethod void test1()
    {
        Contact objContact = new Contact(lastname = 'test',firstname = 'firstTest', title ='titletest',type__c = 'type',SSO_Number__c = '502066194');
        insert objContact;
     
       Test.startTest();
       ContactUpdateServer obj = new ContactUpdateServer(new ApexPages.StandardController(objContact));
      ApexPages.currentPage().getParameters().put(obj.currentRecordId,objContact.id);
       
       system.assertequals(objContact.SSO_Number__c.Length(),9);
       // string searchstring = objContact.SSO_Number__c;
      //  string currentRecordId = objContact.Id;
        

     Test.stopTest();
    }
 
   Static TestMethod  void test2(){
   Contact objContact = new Contact(lastname = 'test',firstname = 'firstTest', title ='titletest',type__c = 'type',SSO_Number__c = '502066194');
        insert objContact;
   
   test.startTest();
   
            ApexPages.StandardController sc = new ApexPages.standardController(objContact);

               ContactUpdateServer obj = new ContactUpdateServer(sc);
               
                PageReference pageRef = obj.Goback();
                  Test.setCurrentPage(pageRef);
                  pageRef.getParameters().put('id',objContact.id);
            
            obj.GoBack();
            
            System.assertNotEquals(null,pageRef);

        test.stopTest();

   }
}


Please suggest where i am going wrong -- I am getting an error "List has no rows for assignment to SObject". 
Best Answer chosen by Karthikeya Mullunja Ramakrishna
Apoorv Saxena 4Apoorv Saxena 4
Ok, 

Please try this code and let me know how this works for you.
 
@isTest 
private Class ContactUpdateServerTest{
static testMethod void test1()
    {
        Contact objContact = new Contact(lastname = 'test',firstname = 'firstTest', title ='titletest',type__c = 'type',SSO_Number__c = '502066194');
        insert objContact;
     
       Test.startTest();
	   ApexPages.currentPage().getParameters().put('id',objContact.id);
       ContactUpdateServer obj = new ContactUpdateServer(new ApexPages.StandardController(objContact));
       Test.stopTest();
    }
 
static TestMethod  void test2(){
   Contact objContact = new Contact(lastname = 'test',firstname = 'firstTest', title ='titletest',type__c = 'type',SSO_Number__c = '502066194');
   insert objContact;
   
   test.startTest();
			ApexPages.currentPage().getParameters().put('id',objContact.id);
            ApexPages.StandardController sc = new ApexPages.standardController(objContact);

            ContactUpdateServer obj = new ContactUpdateServer(sc);
            
            obj.GoBack();
            

        test.stopTest();

   }
}

Please mark this as Solved if this helps you so that others can view it as a proper solution.

Thanks,
Apoorv
 

All Answers

Apoorv Saxena 4Apoorv Saxena 4
Hi Karthikeya,

Please modify the code between Test.startTest() and Test.StopTest() in your  method test1() to the following:

Test.startTest();
       ContactUpdateServer obj = new ContactUpdateServer(new ApexPages.StandardController(objContact));
       ApexPages.currentPage().getParameters().put('id',objContact.id);
       system.assertequals(objContact.SSO_Number__c.Length(),9);
 Test.stopTest();
    

Please let me know how this works for you, mark this as Solved if this helps you so that others can view it as a proper solution.

Thanks,
Apoorv
Karthikeya Mullunja RamakrishnaKarthikeya Mullunja Ramakrishna
No this does not help ! Still i am facing the same error 
Apoorv Saxena 4Apoorv Saxena 4
Ok, 

Please try this code and let me know how this works for you.
 
@isTest 
private Class ContactUpdateServerTest{
static testMethod void test1()
    {
        Contact objContact = new Contact(lastname = 'test',firstname = 'firstTest', title ='titletest',type__c = 'type',SSO_Number__c = '502066194');
        insert objContact;
     
       Test.startTest();
	   ApexPages.currentPage().getParameters().put('id',objContact.id);
       ContactUpdateServer obj = new ContactUpdateServer(new ApexPages.StandardController(objContact));
       Test.stopTest();
    }
 
static TestMethod  void test2(){
   Contact objContact = new Contact(lastname = 'test',firstname = 'firstTest', title ='titletest',type__c = 'type',SSO_Number__c = '502066194');
   insert objContact;
   
   test.startTest();
			ApexPages.currentPage().getParameters().put('id',objContact.id);
            ApexPages.StandardController sc = new ApexPages.standardController(objContact);

            ContactUpdateServer obj = new ContactUpdateServer(sc);
            
            obj.GoBack();
            

        test.stopTest();

   }
}

Please mark this as Solved if this helps you so that others can view it as a proper solution.

Thanks,
Apoorv
 
This was selected as the best answer