You need to sign in to do that
Don't have an account?
cl0s3r
Test coverage of Classes
I have a class that I have 69% tes coverage. The lines that are refernced as not tested are some of the if statements or Apexpage references. How has anyone else resolved this testing?
public class ProdCase { //private final Opportunity o; private Opportunity o; public ProdCase(ApexPages.StandardController stdController) { this.o = (Opportunity)stdController.getRecord(); this.o = [Select Implementation_Contac__c from Opportunity where id =: this.o.id]; } //For testability public ProdCase(Opportunity o) { this.o = o; } //capture the opportunity id from the button public PageReference prodDetails(){ String theID = ApexPages.currentPage().getParameters().get('id'); if (theID == null){ return null; } return prodDetails(theID); } public PageReference prodDetails(String theID){ //Build Oportunity Line Item List object with results from the matching records of the query List<OpportunityLineItem> lin = [Select o.UnitPrice, o.TotalPrice, o.ListPrice, o.Id, o.Opportunity.Pricebook2Id, o.PricebookEntryId, o.ServiceDate, o.Quantity, o.Description, o.Transmission_Mode__c, o.Opportunity.Name, o.Opportunity.StageName, o.Opportunity.Type, o.Opportunity.Estimated_Implementation_Date__c, o.Opportunity.Opportunity_States__c, o.Opportunity.Implementation_Contac__c, o.Opportunity.Implementation_Program__c From OpportunityLineItem o where o.OpportunityId =:theID and o.Implementation_Validation__c != true]; //List<OpportunityLineItem> lin = [Select(Select UnitPrice, TotalPrice, //Quantity, ListPrice, Id From OpportunityLineItem WHERE Implementation_Validation__c != true)o.Name from Opportunity o where o.id =:theID] ; if (o.Implementation_Contac__c == null){ ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.CONFIRM, 'The Implementation Contact has not been assigned, please correct and resubmit!'); ApexPages.addMessage(myMsg); return ApexPages.currentPage(); } //Build list<Case> ToInsert = new list<Case>(); for(Integer I = 0; I < lin.size(); I++){ //create the case object Case cas = new Case(); /* Determine if the Opportuntiy is a Submitter, Vendor, or Receiver Opportuntiy and create the correct case record type. */ String oliID = lin[I].PricebookEntryId; List<PriceBookEntry> prd = [Select p.Product2Id from PricebookEntry p where Id =:oliID ]; for(Integer ic = 0; ic < prd.size(); ic++){ cas.Related_Products__c=prd[ic].Product2Id; } if(lin[I].Opportunity.Type == ('Submitter')){ cas.RecordTypeId='012600000004vNEAAY'; //Owner equals Ann Shelton cas.OwnerId='00560000000lh2zAAA'; }else if(lin[I].Opportunity.Type ==('Vendor')){ cas.RecordTypeId='012600000004vacAAA'; //Owner equals Ann Shelton cas.OwnerId='00560000000lh2zAAA'; }else { cas.RecordTypeId='012600000004vN9AAI'; //Owner equals Cyn Criss cas.OwnerId='00560000000lfXlAAI'; } cas.Implementation_Name__c=lin[I].Opportunity.Name; cas.Status='Business Development'; cas.Projected_Transactions__c=lin[I].Quantity; cas.States__c=lin[I].Opportunity.Opportunity_States__c; cas.ContactId=lin[I].Opportunity.Implementation_Contac__c; cas.Description=lin[I].Description; cas.Subject=lin[I].Opportunity.Name; cas.Implementation_Program__c=lin[I].Opportunity.Implementation_Program__c; //Sandbox //cas.Opportunity_Link__c='tapp0.salesforce.com/'+lin[I].OpportunityId; //Prd cas.Opportunity_Link__c='na4.salesforce.com/'+lin[I].OpportunityId; cas.Estimated_Implementation_Date__c=lin[I].Opportunity.Estimated_Implementation_Date__c; cas.Estimated_Live_Date__c=lin[I].ServiceDate; // cas.Related_Products__c=prd[ic].Product2Id; //Sets the validation field true so that you can not submit a line item case more than once. lin[I].Implementation_Validation__c = true; //Insert the cas object into the ToInsert list. ToInsert.add(cas); } // Inserts all the objects within the ToInsert list insert ToInsert; update lin; PageReference pageRef = new PageReference('/' + theId); pageRef.setRedirect(true); return pageRef; } }
/** * This class contains unit tests for validating the behavior of Apex classes * and triggers. * * Unit tests are class methods that verify whether a particular piece * of code is working properly. Unit test methods take no arguments, * commit no data to the database, and are flagged with the testMethod * keyword in the method definition. * * All test methods in an organization are executed whenever Apex code is deployed * to a production organization to confirm correctness, ensure code * coverage, and prevent regressions. All Apex classes are * required to have at least 75% code coverage in order to be deployed * to a production organization. In addition, all triggers must have some code coverage. * * The @isTest class annotation indicates this class only contains test * methods. Classes defined with the @isTest annotation do not count against * the organization size limit for all Apex scripts. * * See the Apex Language Reference for more information about Testing and Code Coverage. */ @isTest private class TestProdCase { static testMethod void myUnitTest() { test.startTest(); /** creating a list to iterate through both states of an Opportunity Case (Submitter, Vendor) **/ List<Integer> run = new List<Integer>(); run.add(1); run.add(2); run.add(3); // for(Integer it=0;it <run.size(); it++){ System.debug('Alerting on ----> run List '+run); // List<RecordType> rec = [Select id from RecordType where name in ('Implementation Vendor','Implementation Submitter')]; // for(Integer it = 0;it<rec.size();it++){ /** Create Account Record **/ Account a = new Account(Name='TestAccount'); insert a; /** Create Contact Record **/ String acId = null; List<Account> ac = [Select id from Account where name = 'TestAccount']; Contact c = new Contact(FirstName='Johnny', LastName='Tester', AccountId=ac[0].Id); acId = ac[0].id; insert c; System.debug('Alerting on ----> Contact List'+c); /** Create Opportunity Record **/ System.debug('Alerting on ----> '+'Going into the Opportuntiy creation'); Opportunity o = new Opportunity(); // for(Integer it=0;it <run.size(); it++){ //Opportunity o = new Opportunity(); list<Contact> co = [Select Id from Contact where firstName='Johnny' and lastName = 'Tester']; //Integer ct = co.size(); //System.debug('Alerting on ----> '+ct); o.Name='TestOpportunity'; o.CloseDate=Date.today(); o.StageName='Prospecting'; o.Opportunity_States__c='AL'; o.Status__c='Business Development'; o.AccountId=a.Id; o.Name='TestOpportunity'; o.CloseDate=Date.today(); system.debug('Alerting on ----> Contact List'+co); o.Implementation_Contac__c=co[0].Id; o.Type='Submitter'; //Sets the Opportunity type to vendor or submitter // if(it == 1){ // o.Type='Submitter'; // }else if (it == 2){ // o.Type='Vendor'; // }else{ // o.Type='Receiver'; // } // insert o; // System.debug('Alerting on ----> Opportuntiy List '+ o); // } insert o; System.debug('Alerting on ----> Opportuntiy List '+ o); /* Set the Product */ List<PricebookEntry> numb = [Select Id, Pricebook2Id from PricebookEntry where isActive = TRUE LIMIT 5]; //OpportunityLineItem opl = new OpportunityLineItem(OpportunityId=o.Id); for(Integer i = 0;i<numb.size(); i++){ OpportunityLineItem opl = new OpportunityLineItem(OpportunityId=o.Id); // opl.PricebookEntryId=numb[i].Id; opl.PricebookEntryId=numb[i].Id; opl.Quantity=25; opl.ServiceDate=Date.today(); opl.UnitPrice=125.50; insert opl; System.debug('Alerting on ----> Opportunity Line Item '+ opl); } String opp = o.Id; /* Pass in the Opportunity Object */ ProdCase testing = new ProdCase(o); /* Pass in the Opportunity ID */ testing.prodDetails(opp); // System.debug('Alerting on ----> Iteration '+it); test.stopTest(); } // test.stopTest(); // } }
Could you please provide a runtest result that shows uncovered lines as re and covered as blue. Would be very hekpful for tracing the issue and finding a solution.
The code that is clipped isnt covered.
public ProdCase(ApexPages.StandardController stdController) {
this.o = (Opportunity)stdController.getRecord();
this.o = [Select Implementation_Contac__c from Opportunity where id =: this.o.id];
}
public PageReference prodDetails(){
String theID = ApexPages.currentPage().getParameters().get('id');
if (theID == null){
return null;
}
return prodDetails(theID);
}
if (o.Implementation_Contac__c == null){
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.CONFIRM, 'The Implementation Contact has not been assigned, please correct and resubmit!');
ApexPages.addMessage(myMsg);
return ApexPages.currentPage();
}
if(lin[I].Opportunity.Type == ('Submitter')){
cas.RecordTypeId='012600000004vNEAAY';
//Owner equals Ann Shelton
cas.OwnerId='00560000000lh2zAAA';
}else if(lin[I].Opportunity.Type ==('Vendor')){
cas.RecordTypeId='012600000004vacAAA';
//Owner equals Ann Shelton
cas.OwnerId='00560000000lh2zAAA';
}else {
cas.RecordTypeId='012600000004vN9AAI';
//Owner equals Cyn Criss
cas.OwnerId='00560000000lfXlAAI';
}
How is everyone else testing PageReference as well as multiple logic branches when you can only have one object type per class call?
the basic flow of my code is this:
User escalates an Opportunity to a case
OpportuntiyType = Case.RecordType
There is a case created per Opportunity line item
Implementation_Contact__c = Contact of case
Case.Owner = Logic