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
kathybbkathybb 

Help with testing an embedded page Controller

I have a page with data from a custom object that I embedded in the Accounts page.  Can someone suggest a way to get more than 44% coverage in my code?  Here is may page, Controller extension, and Test code.  When I run the code through the UI (by calling up an account record), it appears to be working perfectly, which it should not if it isn't going into the "uncovered" lines.  I'd really appreciate any insight anyone has.

 

The Test Code

 

@isTest
private class TestSalesTargetsController {
    static  testMethod void TestSalesTargets() {
    Set<ID> newTargets = new Set<ID>();
    Account myAcct = new Account();
    Set<ID> ParentAccounts = new Set<ID>();
        
        
    ApexPages.StandardController con = new ApexPages.Standardcontroller(myAcct);
    SalesTargetsController ext = new SalesTargetsController(con);
   
    Account testAcctWithTargets= new Account();
      Account testAcctWithNoTargets = new Account();
      Sales_Targets__c testTargetwithData = new Sales_Targets__c();
      Sales_Targets__c testTargetNoData= new Sales_Targets__c();
      List<Account> AcctsToInsert = new List<Account>();
      List<Sales_Targets__c> TargetsToInsert= new List<Sales_Targets__c>();
   
// create Accounts for Testing
    for(integer x = 0;x<2;x++){
        system.debug('Account = ' + testAcctWithTargets);
        testAcctWithTargets = new Account(Name='TestAcctWithTargets' + x);
        AcctsToInsert.add(testAcctWithTargets);
        testAcctWithNoTargets = new Account(Name='TestAcctWithNoTargets' + x);
        AcctsToInsert.add(testAcctWithNoTargets);
    }
        try{
            Database.SaveResult[] results= Database.insert(AcctsToInsert, false);
            if(results != null)
                for(Database.SaveResult result : results){
                   
                    if(result.isSuccess()){
                        system.debug(result.getId());
                    ParentAccounts.add(result.getId());
                    }
                }
        }catch (DMLException e) {
            system.debug('Account Insert Failed with exception ' + e);
        }
    	
        
        // query Accounts for SalesTargets
        string accountQuery = 'select id, name from account where id in :ParentAccounts';
        
        Map<id, Account> MapAcc = new Map<id, Account>((List<Account>)Database.query(accountQuery));
        
        
        if(MapAcc == null) {
            system.debug('MapAcc has no records and is null');
        } else {
           
        
        system.debug('Size of Account List = ' + mapAcc.size());
        // Create SalesTargets for Testing  
            for(ID aID : ParentAccounts){
            aID = (string)aid;
        	system.debug('acct = '+ aID);
            if(MapAcc.containskey(aID)){
                Account thisAccount = MapAcc.get(aID);
               string thisname = thisAccount.name;
                if( thisname.substring(0, 18) == ('TestAcctWithTargets'))
            {
               Sales_Targets__c st1 = new Sales_Targets__c(Target_Name__c='MediaStar',Account__c =aID, Probability__c=10, value__c=1000.00,Year__c='2013');
               Sales_Targets__c st2 = new Sales_Targets__c(Target_Name__c='CME', Account__c = aID, Probability__c=20, value__c=100.00,Year__c='2013');
               Sales_Targets__c st3 = new Sales_Targets__c(Target_Name__c='Guide', Account__c = aID, Probability__c=10, value__c=1500.00,Year__c='2013');
               Sales_Targets__c st4 = new Sales_Targets__c(Target_Name__c='CMS', Account__c = aID, Probability__c=50, value__c = 2000.00, Year__c = '2013');
               Sales_Targets__c st5 = new Sales_Targets__c(Target_Name__c='PSIP', Account__c = aID,Probability__c=30, value__c = 2000.00, Year__c = '2013');
				system.debug('Finished populating the Targets for Account: '+ aID);
                system.debug(st1);
                system.debug(st2);
                system.debug(st3);
                system.debug(st4);
                system.debug(st5);
               system.debug('ready to add to TargetsToInsert');
               TargetsToInsert.add(st1);
               TargetsToInsert.add(st2);
               TargetsToInsert.add(st3);
               TargetsToInsert.add(st4);
               TargetsToInsert.add(st5);
                     
         }
  
            system.debug('Ready to insert all');
         try{
            database.insert(TargetsToInsert);
        }catch (DMLException e) {
            system.debug('Target Insert Failed with exception ' + e);
        }
       }
       }
      Database.SaveResult[] resultsTargets= Database.insert(TargetsToInsert, false);
            if(resultsTargets != null){
                for(Database.SaveResult result : resultsTargets){
                      if(result.isSuccess()){
                        system.debug(result.getId());
                    	newTargets.add(result.getId()); 
                      }else{
                          system.debug('target failed to insert');
                      }
                }
                
                string qry3 = 'Select id, account__c, account__r.name from Sales_Targets__c where account__r.id in :ParentAccounts';
                Map<ID, Sales_Targets__c> TargetsCheck =new Map<id, Sales_Targets__c>((List<Sales_Targets__c>)Database.query(qry3));
                for(id myid : TargetsCheck.keyset()){
                    if(TargetsCheck.get(myId).account__r.name.substring(0, 18) == 'TestAccountWithTargets') {
                        PageReference insertedPage = Page.SalesTargetInsert;
                     	insertedPage = ext.display();
                      system.assert(ext.numchildren > 0);
                        system.debug('numChildren = ' + ext.numChildren);
                    } else {
                        system.assert(ext.numchildren == 0);
                    }
                    PageReference myPage = new PageReference('/apex/SalesTargetInsert?id=:TargetsCheck.get(myID).account__r)');
                     
                }
            }
            for(ID AccountID : ParentAccounts){
                pageReference anAccount = new PageReference('/' + AccountID);
                system.debug('anAccount = ' + anAccount);
            }
                             
            
            
        }
        
    
    } 
}

 The Page:   Note:  The test coverage results show the entire display() method as uncovered code.

 

<apex:page standardController="Account" extensions="SalesTargetsController" action="{!display}">
    <style type="text/css">
        body {background: #F3F3EC; padding-top: 15px}
    </style>
    <apex:form >
        <apex:pageBlock id="pageBl" title="Sales Targets for the Year 2013" rendered="{!NOT(ISNULL(targets))}">
            <apex:pageBlockTable id="theTable" value="{!targets}" var="t" >
                
                <apex:column headerValue="Line" value="{!t.Name}" />
                <apex:column headerValue="Target Name" value="{!t.Target_Name__c}" />
                <apex:column value="{!t.Probability__c}"/>
                <apex:column value="{!t.Value__c}"/>
                <apex:column value="{!t.Contacted__c}"/>
                <apex:column value="{!t.No_Sale_Reason__c}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 

The Controller

 

public class SalesTargetsController {
    private ApexPages.StandardController con {get;set;}
    private Account a;
    
    String myId = ApexPages.currentPage().getParameters().get('id');
    public integer numChildren = 0;
    public List<Sales_Targets__c> targets {get; set;}
    
    public SalesTargetsController(ApexPages.StandardController con){
        this.con = con;
        this.a =(Account)con.getRecord();
    }
    public PageReference display() {
    
        if(targets == null){
           targets = new List<Sales_Targets__c>();
        } else {
            targets.clear();
        }
 
        string qry = 'Select t.id, t.name, t.target_name__c, t.probability__c, t.value__c, t.Contacted__c, t.no_sale_reason__c from Sales_Targets__c t where Account__r.ID in :ParentAccounts Order by t.id';
     	targets=database.query(qry);
        system.debug('in display targets.size()=' + targets.size());
        if(targets.size() < 1 || targets == null){
            numChildren = 0;
            } else {
        numChildren = targets.size();
                system.debug('numChildres = ' + numChildren);
        }
        return null;
    }
    
}

 Thank you in advance for your help.