+ Start a Discussion
SFDC n12SFDC n12 

Test class help needed

Hi,

Please help me to write the test class for the below class,

/****   Create competitiveinfo records from the corresponding opportunity.
**/

public class AF_OpportunityHandlerController{

   public void createCIRecords(List<Opportunity> oppList)
   {
        Competitive_Info__c ci;
        List<Competitive_Info__c> ciList = new List<Competitive_Info__c>();
        for(Opportunity op: oppList)
        {
            ci = new Competitive_Info__c(Account__c = op.AccountId,Line_Of_Business__c = op.Type,OwnerId = op.ownerId,Competing_Lender__c = op.Competing_Lender__c,Competitions_Rate_Index__c = op.Competing_Rate_Type__c,Competitive_Spread__c = op.Competing_Spread__c,Competitions_Net_Rate__c = op.Competition_Net_Rate__c,Date_Competitive_Info_Obtained__c = op.CloseDate,Loan_Type__c = op.Opportunity_Type__c);
            ciList.add(ci);
        }
       
        if(ciList != null && ciList.size() >0)
        {
            try
            {
                insert ciList;
            }
            catch(system.DMLException e)
            {
                system.debug('DML Exception'+e.getDMLMessage(0));
            }
            finally
            {
                AF_DealerCRM_Utility.IS_CI_CREATED = true; 
            }
        }
   }
 
 
 
 
}

Thanks in advance
Best Answer chosen by SFDC n12
Vinit_KumarVinit_Kumar
I don't see you calling your method createCIRecords() inside your Test Class.

You need to call your method something like below :-

afcominfoUpd.createCIRecords(<pass a list Opportunity>);

All Answers

bob_buzzardbob_buzzard
As you have asked for help, I'd expect to see your attempt at writing a test class and details of problems you are encountering.  As you haven't posted this, are you asking for someone to write the test class for you?
SFDC n12SFDC n12
Nopes , I have written the test class for my above class


@isTest
public class Test_AF_OpportunityHandlerController
{

static testMethod void testAddcompinfoToopportunity()
{

  /*Setup user Data*/   
 
List<User> ae = [SELECT Id FROM User WHERE  isActive = true ];

/* set up account data */    
        Account a = new Account();   
        a.Name = 'Test Account - 1';   
        a.BillingStreet = 'AnyWhere St - 1';   
        a.BillingCity = 'Test City';   
        a.BillingPostalCode = '99922'; 
        a.BillingState = 'MI';  
        a.Ally_Primary_Dealer_Number__c = '98999';  
        
         /* insert accounts */  
             insert a;   
          
          
          
/* add new opportunities */
integer opptyCnt = 1;
List<Opportunity> opportunities = new List<Opportunity>();
     for (integer k=0; k<opptyCnt; k++) {
        Opportunity o = new Opportunity();
        o.AccountId = a.Id;
        o.Opp_Primary_Product__c = 'Retail';
        o.name = a.Name+'-'+o.Opp_Primary_Product__c;
        o.CloseDate = system.today().addDays(4);
        o.stageName = 'ClosedLost';
        o.Type = 'New Business';
        o.Competing_Lender__c ='Bank of America';
        o.Competing_Rate_Type__c ='Fixed';
        o.Competing_Spread__c =12;
        o.Competition_Net_Rate__c =12;
        o.Opportunity_Type__c ='Wholesale';
         o.ownerid = ae[0].id;
        opportunities.add(o);
    }
     /* insert opportunities*/
       insert opportunities;
     //List<Opportunity> insertedOpportunities = [Select Id,StageName,ownerId,owner.Name from Opportunity where Id IN :opportunities];
     //System.assertEquals(insertedOpportunities.size()==2);     
        /* validate */    
        List<Opportunity> insertedOpptys = [SELECT Id, OwnerId FROM Opportunity ORDER BY RecordType.Name ASC];    
        System.assertEquals(insertedOpptys.size(), opptyCnt);

     /* add new competitiveinfo*/

List<Competitive_Info__c> competitiveinfo = new List<Competitive_Info__c>();
for (integer k=0; k<opptyCnt; k++) {
        Competitive_Info__c  c = new Competitive_Info__c();
        c.Account__c =a.Id;
        c.Line_Of_Business__c ='New Business';
        c.OwnerId =ae[0].id;
        c.Competing_Lender__c ='Bank of America';
        c.Competitions_Rate_Index__c ='Fixed';
        c.Competitive_Spread__c =12;
        c.Competitions_Net_Rate__c =12;
        c.Date_Competitive_Info_Obtained__c =system.today().addDays(4);
        c.Loan_Type__c='Wholesale';
        competitiveinfo .add(c);
       
        }
       
         /* insert competitiveinfo*/
       insert competitiveinfo;


/*Instanciate the class */   
AF_OpportunityHandlerController afcominfoUpd = new AF_OpportunityHandlerController();


}
}

method is passing but the class is not covering , sorry i forgot to past my test class, let  me know if there is any error in my test class .

Thanks
Vinit_KumarVinit_Kumar
I don't see you calling your method createCIRecords() inside your Test Class.

You need to call your method something like below :-

afcominfoUpd.createCIRecords(<pass a list Opportunity>);

This was selected as the best answer
SFDC n12SFDC n12
got it , i forgot to cal the main one , thanks so much it worked..

Thanks