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
force_rocksforce_rocks 

Code Coverage Issue with Apex Test Class

I created a project in Force IDE using Prod credentials. Then I created an Apex Class(Class A) in IDE. The Apex Class compiles fine, but can't be copied onto the server, due to code coverage issues. So I then created another Apex Class(Class B) to test Class A. What's weird is that the IDE is trying to compile Class B on the server and not locally. This would obviously fail because Class A is not on the server yet.

 

Now I created a Sandbox for Development purpose. Here Class A and Class B compile fine, but can't be deployed onto the server, again because of the code coverage. In Class B(Test Class) I have created a object of Class A and called all the methods of it. Even then it complains Class A is 0% covered.

 

I think there is an issue with the way I have implemented the Test class.

 

Pls help.

 

Error: IncomeExt(ApexClass) -- 13 lines not tested, 0% covered.

 

Class A:

public class IncomeExt {
public Income__c q {get;set;}
public IncomeExt (ApexPages.StandardController c)
{
    controller = c;
    q = (Income__c) c.getRecord();
}

public void save()
{}

public String attachIncome()
{
    PageReference pdfPage = Page.AD672PDF;
     pdfPage.getParameters().put('id',q.id);
     Blob pdfBlob = pdfPage.getContent();
     Attachment a = new Attachment(parentId = q.id, name=q.id + '.pdf', body = pdfBlob);
    insert a;
    return('test');
}
      
      
private ApexPages.StandardController controller;      
   
}

 

 

Class B: (tester class)

@IsTest private with sharing class IncomeExtTest {
static testMethod void myPage_Test()
{
//PageReference pageRef = Page.AD672;
//Test.setCurrentPageReference(pageRef);
ApexPages.StandardController con = new ApexPages.StandardController(new Income__c());
// create an instance of the controller
//test.startTest();
IncomeExt temp1 = new IncomeExt(con);
temp1.attachIncome();
temp1.save();
//test.stopTest();
}
}



chandra2ravichandra2ravi

Hi ,

 

First you have to create Income__c() object and send that object to standard controller.

 

Income__c obj =new Income__c();

obj .Name='test';

otherfileds.....

ApexPages.StandardController con = new ApexPages.StandardController(obj );

Shashikant SharmaShashikant Sharma

I hope you are talking about this part not covered

 



Attachment a = new Attachment(parentId = q.id, name=q.id + '.pdf', body = pdfBlob);
    insert a;
    return('test');

 

In test context : TestMethod do not support getContent call, test skipped , a VisualforceException is given and test is skipped after this.

Shashikant SharmaShashikant Sharma

As I Explained earlier 

In test context : TestMethod do not support getContent call, test skipped , a VisualforceException is given and test is skipped after this.

So this part reamin uncovered

Attachment a = new Attachment(parentId = q.id, name=q.id + '.pdf', body = pdfBlob);
    insert a;
    return('test');

 

 

So Solution is

 

Change Your class to this

 

public class IncomeExt {
public Income__c q {get;set;}
public IncomeExt (ApexPages.StandardController c)
{
    controller = c;
    q = (Income__c) c.getRecord();
}

public void save()
{}

public String attachIncome()
{
    PageReference pdfPage = Page.AD672PDF;
    pdfPage.getParameters().put('id',q.id);
    Blob pdfBlob; 
    if(!Test.isRunningTest())
        {
            pdfBlob = pdfPage.getContent();
        }
    else
        { 
            pdfBlob = Blob.valueOf('Unit Test Attachment Body');
        }
    Attachment a = new Attachment(parentId = q.id, name=q.id + '.pdf', body = pdfBlob);
    insert a;
    return('test');
}
      
      
private ApexPages.StandardController controller;      
   
}

 And Test Class to this

 

@IsTest private with sharing class IncomeExtTest {
static testMethod void myPage_Test()
{
//PageReference pageRef = Page.AD672;
//Test.setCurrentPageReference(pageRef);
Income__c iT =  new Income__c(Name = 'TestIncomeTax');
insert iT;
ApexPages.StandardController con = new ApexPages.StandardController(iT);
// create an instance of the controller
//test.startTest();
IncomeExt temp1 = new IncomeExt(con);
temp1.attachIncome();
temp1.save();
//test.stopTest();
}
}