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
GaneeeshGaneeesh 

How to get code coverage for this class..

Any body help me on this class code coverage..? this is my actual class

public with sharing class AddInvoiceCreditToEducation {

    public String pgmsg { get; set; }
    public AddInvoiceCreditToEducation(){
    string myDate;
    Account accRec=[select id,name,Current_Ed_Amount__c,Expiration__c from Account where id=:ApexPages.CurrentPage().getParameters().get('id')];
    myDate= string.valueof(accRec.Expiration__c.year())+'-'+string.valueof(accRec.Expiration__c.Month())+'-'+string.valueof(accRec.Expiration__c.Day());
    pgmsg='Education Update Successfull.....'+' '+ +'$'+ accRec.Current_Ed_Amount__c + ' ' + + 'Will Expire on'+ +' '+ myDate;
   
    }
    public PageReference cancel() {
    pagereference ref=new pagereference('/'+Apexpages.currentpage().getparameters().get('invId'));
    return ref;
   
        //return null;
    }

}


when i am running test class for the above test class i am getting error like list has no rows to assignment (test class failed)
and following is my test class.

@isTest
private class AddInvoiceCreditToEducation_UT{
  static testMethod void My_method(){ 
      // setup a ship to account
        Account shipTo = new Account();
        shipTo.Name = 'PSAV 6FOO';
        shipTo.Type = 'Supplier';
        shipTo.Expiration__c = system.today()+1;
        shipto.Current_Ed_Amount__c = 100;
        insert shipTo;
        Contract ocon = new Contract(name= 'test');
        ocon.AccountId=shipTo.id;
        ocon.StartDate = date.newInstance(2014, 10, 7);
        insert ocon;
        Contact ocont = new Contact(LastName= 'test');
        ocont.AccountId=shipTo.id;
        insert ocont;
        License_Invoice__c o1 = new License_Invoice__c();
        o1.Account__c = shipTo.id;
        o1.Contract__c = ocon.id;
        o1.Bill_To__c = ocont.id;        
        insert o1;
        List<Invoice_Product__c> invoicelist=new list<Invoice_Product__c>();
        Invoice_Product__c oip = new Invoice_Product__c(Contract__c = ocon.id,License_Invoice__c=o1.Id);
        Account accRec=[select id,name,Current_Ed_Amount__c,Expiration__c from Account where id=:shipTo.id];
  AddInvoiceCreditToEducation obj=new AddInvoiceCreditToEducation();
  obj.cancel();
     
   
  }
}

any body help me how to get code coverage for this
logontokartiklogontokartik

The error is because your code Account accRec query is not able to find any records. Please make sure you put that in try .. catch block. 

In your test class. 

You need to pass the Page Parameters in your code to query the Account table, for that you need set the pagereference, something like below.

@isTest
private class AddInvoiceCreditToEducation_UT{
  static testMethod void My_method(){ 
      // setup a ship to account
        Account shipTo = new Account();
        shipTo.Name = 'PSAV 6FOO';
        shipTo.Type = 'Supplier';
        shipTo.Expiration__c = system.today()+1;
        shipto.Current_Ed_Amount__c = 100;
        insert shipTo;
        Contract ocon = new Contract(name= 'test');
        ocon.AccountId=shipTo.id;
        ocon.StartDate = date.newInstance(2014, 10, 7);
        insert ocon;
        Contact ocont = new Contact(LastName= 'test');
        ocont.AccountId=shipTo.id;
        insert ocont;
        License_Invoice__c o1 = new License_Invoice__c();
        o1.Account__c = shipTo.id;
        o1.Contract__c = ocon.id;
        o1.Bill_To__c = ocont.id;        
        insert o1;
        List<Invoice_Product__c> invoicelist=new list<Invoice_Product__c>();
        Invoice_Product__c oip = new Invoice_Product__c(Contract__c = ocon.id,License_Invoice__c=o1.Id);
        Account accRec=[select id,name,Current_Ed_Amount__c,Expiration__c from Account where id=:shipTo.id];

   Test.startTest(); 
    
     PageReference testPage = Page.<your Visualforce Page Name>;
     Test.setCurrentPageReference(testPage);
     testPage.currentPage().getParameters().put('id',shipTo.Id);
     testPage.currentPage().getParameters().put('invId',o1.Id); // Assuming its the invoice record, no reference in code though 
   
     AddInvoiceCreditToEducation obj=new AddInvoiceCreditToEducation();
     obj.cancel(); // This one will also have 

   Test.stopTest();
     
   
  }
}