+ Start a Discussion
NikiG22NikiG22 

Test Coverage

Can anyone help with the test coverage on this trigger? I am terrable with test classes.

 

trigger trg_new_AdProductFulfillment on Opportunity (after update)
{
   List<Opportunity> closedWonOpps=new List<Opportunity>();

   for (Opportunity opp : trigger.new)
   {
       Opportunity oldOpp=trigger.oldMap.get(opp.id);
       if ( (opp.StageName=='Closed Won') &&
            (oldOpp.StageName!='Closed Won')&&
            (opp.Message_Sent__c== False)) 
       {
          closedWonOpps.add(opp);
       }
   }
    
   if (!closedWonOpps.isEmpty())
   {
    List<OpportunityLineItem> olis = [SELECT ID, OpportunityId,Account_Manager_ID__c,Opportunity_Owner_id__c,PO_IO_Number__c, Account_ID__c,Standard_RateproductID__c,Closed_Won_Date__c,
        Quantity FROM OpportunityLineItem WHERE Ad_Product_Fulfillment__c ='True' and
        OpportunityId in :closedWonOpps];

    List<Ad_Product_Fulfillment__c> afToInsert=new List<Ad_Product_Fulfillment__c>();
   for (OpportunityLineItem oli : olis)
   {
    
    For (integer i = 0; i < oli.Quantity; i++){
            Ad_Product_Fulfillment__c newaf= new Ad_Product_Fulfillment__c();
            newaf.Opportunity__c = oli.Opportunityid;
            newaf.Placement__c = oli.Standard_RateproductID__c;
            newaf.Account__c = oli.Account_ID__c;
            newaf.IO_Start_Date__c = oli.Closed_Won_Date__c;
            newaf.PO_IO_Number__c = oli.PO_IO_Number__c;
            newaf.Quantity__c = oli.Quantity;
            newaf.Expiration_Date__c = oli.Closed_Won_Date__c +365;
            newaf.Account_Executive__c = oli.Opportunity_Owner_id__c;
            newaf.Account_Manager__c = oli.Account_Manager_ID__c;
            afToInsert.add(newaf);
       }
          }

    insert afToInsert;
   }
}

 Thank you,

Niki

Suresh RaghuramSuresh Raghuram

Hi Niki,

 

Start as follows

private class Testtrg_new_AdProductFulfillment{

 

static ApexPage.StandardController stdController;

 

Ad_Product_Fulfillment__c newaf= new Ad_Product_Fulfillment__c();
            newaf.Opportunity__c = your value;
            newaf.Placement__c = your value;
            newaf.Account__c = your value;
            newaf.IO_Start_Date__c = your value;
            newaf.PO_IO_Number__c = your value;
            newaf.Quantity__c = your value;
            newaf.Expiration_Date__c = your value;
            newaf.Account_Executive__c = your value;
            newaf.Account_Manager__c = your value;
            afToInsert.add(newaf);
Insert aftoInsert;
Remaining part change as for your requirement. the whole thing in the remaining part is it is calling the method or trigger you created in apex.
stdController = new ApexPages.StandardController(testContact);
       testContactControllerExt = new ContactControllerExt(stdController);
       testContactControllerExt.previous();


Rajesh SriramuluRajesh Sriramulu

Hi

 

Try this

 

@isTest(SeeAllData=True)

Public class AdProductFulfillment_Test{

static testmethod void AdProductFulfillment_Test()

{

Opportunity opp = new Opportunity();

opp.StageName==' Prospecting';

opp.Message_Sent__c== False;

opp.

opp. //here u insert the mandatory fields

 

insert opp

opp.StageName=='Closed Won';

update opp;

 

OpportunityLineItem oppt = new OpportunityLineItem();

oppt.OpportunityId=opp.Id;

oppt.//like this insert the mandotory fields

oppt.

 

 

Ad_Product_Fulfillment__c adg = new Ad_Product_Fulfillment__c();

adg.Opportunity__c = oli.Opportunityid;

adg.

adg.//like this insert the fields which are in trigger.

insert adg

 

}}

 

Regards,

Rajesh.

NikiG22NikiG22

Thank you Rajesh -

I built the test code but its is only 22%?

 

Test Code:

@isTest(SeeAllData=True)

Public class AdProductFulfillment_Test{

static testmethod void AdProductFulfillment_Test()

{

Opportunity opp = new Opportunity();
opp.StageName='Closed Won';
opp.Message_Sent__c= False;
opp.Name = 'Test';
opp.Account_Manager__c = '00550000000saAA';
opp.CloseDate =Date.newInstance(2012, 01, 15);
opp.Product_Interest__c = 'Job Posting';
opp.Billing_Email__c = 'test@otj.com';
opp.New_Dollar_Amount__c=100;
insert opp;


OpportunityLineItem oppt = new OpportunityLineItem();
oppt.OpportunityId=opp.Id;
oppt.Quantity =1;
oppt.Cost_Type__c ='weekly';
oppt.TotalPrice =100;
oppt.PricebookEntryId='01u50000003SHVz';
insert oppt;

    List<Ad_Product_Fulfillment__c> afToInsert=new List<Ad_Product_Fulfillment__c>();
   {
Ad_Product_Fulfillment__c adg= new Ad_Product_Fulfillment__c();
            adg.Opportunity__c = '006W0000002VxWF';
            adg.Placement__c = '01t50000001YRdD';
            adg.Account__c = '001W0000004yqYJ';
            adg.IO_Start_Date__c = Date.newInstance(2012, 01, 15);
            adg.PO_IO_Number__c = '123';
            adg.Quantity__c = 1;
            adg.Expiration_Date__c = Date.newInstance(2012, 01, 15);
            adg.Account_Executive__c = '00550000000saAA';
            adg.Account_Manager__c = '00550000000saAA';
insert afToInsert;
}

}}

 Thank you for all your help!

Niki

 

Rajesh SriramuluRajesh Sriramulu

Hi

 

U did't insert the orderlineitem fields  like oppt.Ad_Product_Fulfillment__c ='True' which are in query try to insert remaining fields and let me know which lines are not covered,put those lines in red color.

 

 

Regards,

Rajesh.

NikiG22NikiG22

ok - i added the line

@isTest(SeeAllData=True)

Public class AdProductFulfillment_Test{

static testmethod void AdProductFulfillment_Test()

{

Opportunity opp = new Opportunity();
opp.StageName='Closed Won';
opp.Message_Sent__c= False;
opp.Name = 'Test';
opp.Account_Manager__c = '00550000000saAA';
opp.CloseDate =Date.newInstance(2012, 01, 15);
opp.Product_Interest__c = 'Job Posting';
opp.Billing_Email__c = 'test@otj.com';
opp.New_Dollar_Amount__c=100;
insert opp;

List<OpportunityLineItem> olis = [SELECT ID, OpportunityId,Account_Manager_ID__c,Opportuntiy_Owner_id__c,PO_IO_Number__c, Account_ID__c,Standard_RateproductID__c,Closed_Won_Date__c,
        Quantity FROM OpportunityLineItem WHERE Ad_Product_Fulfillment__c ='True'];
        
OpportunityLineItem oppt = new OpportunityLineItem();
oppt.OpportunityId=opp.Id;
oppt.Quantity =1;
oppt.Cost_Type__c ='weekly';
oppt.TotalPrice =100;
oppt.PricebookEntryId='01u50000003SHVz';
insert oppt;

    List<Ad_Product_Fulfillment__c> afToInsert=new List<Ad_Product_Fulfillment__c>();
   {
Ad_Product_Fulfillment__c adg= new Ad_Product_Fulfillment__c();
            adg.Opportunity__c = '006W0000002VxWF';
            adg.Placement__c = '01t50000001YRdD';
            adg.Account__c = '001W0000004yqYJ';
            adg.IO_Start_Date__c = Date.newInstance(2012, 01, 15);
            adg.PO_IO_Number__c = '123';
            adg.Quantity__c = 1;
            adg.Expiration_Date__c = Date.newInstance(2012, 01, 15);
            adg.Account_Executive__c = '00550000000saAA';
            adg.Account_Manager__c = '00550000000saAA';
insert afToInsert;
}

}}

 here are the lines not covered

 

trigger trg_new_AdProductFulfillment on Opportunity (after update)
{
   List<Opportunity> closedWonOpps=new List<Opportunity>();

   for (Opportunity opp : trigger.new)
   {
       Opportunity oldOpp=trigger.oldMap.get(opp.id);
       if ( (opp.StageName=='Closed Won') &&
            (oldOpp.StageName!='Closed Won')&&
            (opp.Message_Sent__c== False))
       {
          closedWonOpps.add(opp);
       }
   }
    
   if (!closedWonOpps.isEmpty())
   {
    List<OpportunityLineItem> olis = [SELECT ID, OpportunityId,Account_Manager_ID__c,Opportuntiy_Owner_id__c,PO_IO_Number__c, Account_ID__c,Standard_RateproductID__c,Closed_Won_Date__c,
        Quantity FROM OpportunityLineItem WHERE Ad_Product_Fulfillment__c ='True' and
        OpportunityId in :closedWonOpps];

    List<Ad_Product_Fulfillment__c> afToInsert=new List<Ad_Product_Fulfillment__c>();
   for (OpportunityLineItem oli : olis)
   {
    
    For (integer i = 0; i < oli.Quantity; i++){
            Ad_Product_Fulfillment__c newaf= new Ad_Product_Fulfillment__c();
            newaf.Opportunity__c = oli.Opportunityid;
            newaf.Placement__c = oli.Standard_RateproductID__c;
            newaf.Account__c = oli.Account_ID__c;
            newaf.IO_Start_Date__c = oli.Closed_Won_Date__c;
            newaf.PO_IO_Number__c = oli.PO_IO_Number__c;
            newaf.Quantity__c = oli.Quantity;
            newaf.Expiration_Date__c = oli.Closed_Won_Date__c +365;
            newaf.Account_Executive__c = oli.Opportuntiy_Owner_id__c;
            newaf.Account_Manager__c = oli.Account_Manager_ID__c;
            afToInsert.add(newaf);
       }
          }

    insert afToInsert;
   }
}

 

Thanks again for all your help.

 

Niki

Rajesh SriramuluRajesh Sriramulu

Hi

 

Sorry I am little busy.

 

Here is 100% code coverage.

Try this

 

@isTest(SeeAllData=True)

Private class opp_Test{

static testmethod void AdProductFulfillment_Test()
{
List<Opportunity> closedWonOpps=new List<Opportunity>();
Opportunity opp = new Opportunity();
opp.StageName='Closed Won';
opp.Message_Sent__c= False;
opp.Name = 'Test';
Opp.StageName='Qualification';
opp.Message_Sent__c=false;
//opp.Account_Manager_ID__c = '005saAA';
opp.CloseDate =Date.newInstance(2012, 01, 15);
//opp.Product_Interest__c = 'Job Posting';
//opp.Billing_Email__c = 'test@otj.com';
//opp.New_Dollar_Amount__c=100;
 closedWonOpps.add(opp);
insert closedWonOpps;
Account acc = new Account();
acc.Description='test';
acc.name='test';
insert acc;

 
 
Contact con = new contact();
con.lastname='test';
insert con;

sObject s = [select ID from Pricebook2 where IsStandard = TRUE];

//for (Product2 newProduct: Trigger.new) {
product2 pro = new product2();
pro.Name='test';
//pro.Product_Id__c
insert pro;

PricebookEntry z = new PricebookEntry(Pricebook2Id=s.ID,Product2Id=pro.Id, UnitPrice=0.00, IsActive=TRUE, UseStandardPrice=FALSE);
insert z;


OpportunityLineItem oppp = new OpportunityLineItem();
oppp.OpportunityId=opp.Id;
oppp.Account_Manager_ID__c='tetere';
oppp.Opportunity_Owner_id__c=con.Id;
oppp.PO_IO_Number__c=123464;
oppp.Account_ID__c=acc.Id;
oppp.Quantity=23.56;
oppp.Standard_RateproductID__c='test';
oppp.Closed_Won_Date__c=system.today();
oppp.Ad_Product_Fulfillment__c='True';
oppp.TotalPrice=12356.20;
oppp.PricebookEntryId=z.Id;
insert oppp;

           
    List<Ad_Product_Fulfillment__c> afToInsert=new List<Ad_Product_Fulfillment__c>();
 
Ad_Product_Fulfillment__c adg= new Ad_Product_Fulfillment__c();
            adg.Opportunity__c = opp.Id;
            adg.Placement__c = oppp.Standard_RateproductID__c;
            adg.Account__c = oppp.Account_ID__c;
            adg.IO_Start_Date__c =oppp.Closed_Won_Date__c;
            adg.PO_IO_Number__c = oppp.PO_IO_Number__c;
            adg.Quantity__c = oppp.Quantity;
            adg.Closed_Won_Date__c=oppp.Closed_Won_Date__c;
           // adg.Standard_RateproductID__c='sgetre4534';
            //adg.Account_ID__c='43dfdferw';
           // adg.Opportunity_Owner_id__c='3423dfd';
            adg.Expiration_Date__c = oppp.Closed_Won_Date__c +365;
            adg.Account_Executive__c = oppp.Opportunity_Owner_id__c;
            adg.Account_Manager__c = oppp.Account_Manager_ID__c;
            afToInsert.add(adg);
insert afToInsert;
opp.StageName='Closed Won';
 update opp;
    
    }

}

 

Regards,

Rajesh.