0

I have been trying to push my deployment that consist of a Apex class and the test class for it (test class - 95% code coverage). When I attempt to push this I get "Code Coverage Failure - Your code coverage is 0%. You need at least 75% coverage to complete this deployment." I am very confused as for components I am at 21/22 which gives me 95% code coverage. I have removed any kind of duplication errors and my overall code coverage is at 80%.

Any thoughts on what I might be doing wrong or missing?

Apex Class:

public class mpr_copy
{
    public Marketing_Project_Request__c objmpr{get;set;}
    public Attachment myAttachment{get;set;}
    public string fileName{get;set;}
    public Blob fileBody{get;set;}

    public mpr_copy(Apexpages.standardcontroller controller)
    {
        objmpr = (Marketing_Project_Request__c)controller.getRecord();
        myAttachment = new Attachment();
    }
    public pagereference save()
    {
        if(myAttachment.Name == null)
        {
            insert objmpr;
        }
        if(myAttachment.Name != null)
        {
            insert objmpr;
            System.debug('@@@@@fileBody'+fileBody);    
            myAttachment = new Attachment();
            Integer i=0;
            myAttachment .clear();
            myAttachment.Body = fileBody;
            myAttachment.Name = fileName ;
            myAttachment.ParentId = objmpr.id;            
            insert myAttachment;   
        }             
        pagereference pr = Page.Thank_You;                          
        pr.setRedirect(true);
        return pr;
    }
}

Test Class:

@isTest
private class Test_mpr_copy {

public static testMethod void validateMPR_Copy() {    
Marketing_Project_Request__c myMPR = new 
Marketing_Project_Request__c(Project_Name__c= 'Test MPR',Description_of_Project__c= 'Tester for Apex',Estimated_Budget__c= 4500,Content_Type__c = 'Blog;Email',Department__c = 'Client Success',Language__c = 'US',Industry__c = 'Biotechnology',Level__c = 'C-Level',Resource_Allocation__c = 'Copywriting',Requested_Publish_Launch_Date__c = date.today());   
insert myMPR;

//create pageReference
PageReference pageRef = Page.Marketing_Project_Request_Form;
Test.setCurrentPage(Page.Marketing_Project_Request_Form);

//pass parameter to the page
ApexPages.currentPage().getParameters().put('id',myMPR.Id);

//create an instance of controller.
ApexPages.StandardController standrdcontroller = new 
ApexPages.StandardController(myMPR);
mpr_copy myController = new mpr_copy(standrdcontroller);
myController.myAttachment.Name = 'foo';
myController.fileBody=Blob.valueOf('Body Test');
myController.fileName = 'Test';
myController.save();

}

public static testMethod void validateMPR_Copy1() {        
Marketing_Project_Request__c myMPR = new Marketing_Project_Request__c();
mpr_Copy pageController = new mpr_copy(new ApexPages.StandardController(myMPR));

pageController.myAttachment.Name = 'foo';
pageController.fileBody=Blob.valueOf('Body Test');
pageController.fileName = 'Test';
pageController.save();

}

}
  • 3
    Did you actually include the test class in your deployment? Can you perhaps edit your post to include more detail about what you tried? – Adrian Larson May 22 '17 at 16:23
  • I included both my apex class as well as the test class in the change set. I only had those two items in there. I selected default for the validate type and that is the error that I received. – Joshua Anderson May 22 '17 at 19:43
  • @JoshuaAnderson Was that the only error or was there information listed below it? – Eric May 22 '17 at 21:44
0

You should prepare your own data and try to test all positive and negative scenarios based on that.

Secondly, you have written your test class just to increase the coverage so that it can be deployed in production.

Thirdly, 0% coverage might be the situation if you forget to include test class during deployment.

You have not tested anything at your test method

You should take this approach for your test cases

//create Marketing_Project_Request__c and related records
Marketing_Project_Request__c myMPR = new Marketing_Project_Request__c(Name='test',......);
insert myMPR;

//create pageReference
PageReference page = Page.MyVFP;
Test.setCurrentPage(page);

//pass parameter to the page
ApexPages.currentPage().getParameters().put('id',myMPR.Id);

//create an instance of controller.
ApexPages.StandardController standrdcontroller = new ApexPages.StandardController(myMPR);
mpr_copy myController = new mpr_copy(standrdcontroller);
System.assertEquals(.....);
myController.myAttachment.Name = 'foo';
myController.fileBody=Blob.valueOf('Body Test');
myController.fileName = 'Test';
myController.save();
System.assertEquals(.....);

Also, follow Testing best practices

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.