+ Start a Discussion
cl0s3rcl0s3r 

ApexPages.Mesages Test Coverage

How can I cover the following code via test data?

 

 

ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.CONFIRM, 'The case status is not in a valid state');
ApexPages.addMessage(myMsg);
return ApexPages.currentPage();

 

Shashikant SharmaShashikant Sharma

You don't need to write any thing extra for this this will get covered when the actual method that contains it will be called. Is that code  in a Catch block?

cl0s3rcl0s3r

It isnt in a catch block it is in an if statement.  The code isnt getting covered via the test though.

 

cl0s3rcl0s3r

Maybe the best question then should be, How do you test both logical branches?

Shashikant SharmaShashikant Sharma

In that case you need to create data in such way that your logical branch which has this code gets executed. If you can provide me your class and test class code, then  I can help you in creating such test case to cover it.

cl0s3rcl0s3r

Class

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++){
                                	if(!lin[I].Opportunity.StageName.equals('Closed-Requires Implementation')){
                               			ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.CONFIRM, 'This Opportunity does not require an Implementation Case!');
                                		ApexPages.addMessage(myMsg);
                                		return ApexPages.currentPage();
                                	}
        
                                        //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, p.Name from PricebookEntry p where Id =:oliID ];
        
        for(Integer ic = 0; ic < prd.size(); ic++){
                cas.Related_Products__c=prd[ic].Product2Id;
                cas.Implementation_Name__c=lin[I].Opportunity.Name+' - '+prd[ic].Name;
        
    }
        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;
        }
}

 

@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', TrackerOrgType__c='Ambulatory Surgical Center', Organization_Type__c = 'Ambulatory Surgical Center');
        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;
        Apexpages.Standardcontroller sc = new Apexpages.Standardcontroller(o);
      //  Pagereference pr = new Pagereference('/'+ o.Id);
      //ApexPages.StandardController con = new ApexPages.StandardController(sc);

      ProdCase pc = new ProdCase(sc);
     Pagereference pr= pc.prodDetails();
  
   //  Pagereference pr= pc.prodDetails(o.id);
        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();
//    }
}