+ Start a Discussion
Maf_007Maf_007 

Test Method Passed But shows error on Class??

Hi,

I have a trigger on Opportunity which fires once opportunity stage = closed won. My Test method passes but shows error on class?

 

/*
Author: Mahfuz Choudhury
Date: 13.09.13
TestClassName:TestCloneOppLineItem on Opportunity
*/
@isTest(seeAllData=true) 
private class TestCloneOppLineItem{
    static testMethod void TestOppMethod() {
        List<Contact> contlist = new List<Contact>{};
        
        //Start of the test execution
        Test.starttest();
        //create a new account to associate with the opportunity
        Account a = new Account(Name = 'Test Account');
        insert a;
        
        //Create a contact to assign as primary contact role
        Contact con = new Contact(FirstName = 'Maf', LastName = 'Sample', AccountId = a.Id);
        Contact con1 = new Contact(FirstName = 'Lewis', LastName = 'Test', AccountId = a.Id);
        contlist.add(con);
        contlist.add(con1);
        insert contlist;
        
        //Select A Standard pricebook for the product
        Pricebook2  standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];
        
        //Create a Test PriceBook for OpportunityLineItem
        Pricebook2 pbk = new Pricebook2 (Name='Test Pricebook Entry 1',Description='Test Pricebook Entry 1', isActive=true);
        insert pbk;
        
        //Insert a new Product for test class
        Product2 prd = new Product2 (Name='Premium Product',Description='Test Product Entry 1',productCode = 'ABC', isActive = true);
        insert prd;
        
        //Insert a new Pricebook entry
        PricebookEntry pbe = new PricebookEntry (Product2ID=prd.id,Pricebook2ID=standardPb.id,UnitPrice=50, isActive=true,UseStandardPrice=false);
        insert pbe;
        
        //create a new opportunity to test
        Opportunity opp = new Opportunity();
        opp.Name = 'Test Opportunity';
        Opp.AccountId = a.id;
        Opp.StageName = 'Closed Won';
        Opp.Type = 'Existing Business';
        Opp.CloseDate =  Date.today()+2;
        insert opp;
        
        OpportunityLineItem OLI = new OpportunityLineItem();
        OLI.Quantity = 2;
        OLI.UnitPrice = 5.00;
        OLI.OpportunityId = Opp.id;
        //OLI.ProductId = pbe.Id;
        OLI.PricebookEntryId = pbe.id;
        insert OLI;
        
        //creating opportunity contact role and make it primary
        OpportunityContactRole ocr = new OpportunityContactRole(OpportunityId = Opp.Id, ContactId = Con.Id, IsPrimary = true);
        insert ocr;
        
        System.assertEquals('Closed Won',Opp.StageName);
        System.assertEquals(Opp.IsOnlineOrder__c, false);
        
        Opp.IsOnlineOrder__c = true;
        Update Opp;
        System.assertEquals(Opp.IsOnlineOrder__c, true);
        Test.stopTest();
    }
}

 Can I get some help on this plz...

Best Answer chosen by Admin (Salesforce Developers) 
Maf_007Maf_007

Hi Guys, The following class is getting 100% Coverage if anyone needs it:

@isTest(seeAllData=true)
private class TestCloneOppLineItem{
    static testMethod void TestOppMethod() {
        CloneLineItemOppClosedWon ObjConfirm = new CloneLineItemOppClosedWon();
        List<Contact> contlist = new List<Contact>{};
        List <Opportunity> bulkopp = new List<Opportunity>{};
        List <Sales_Agreement__c> SO1 = New List <Sales_Agreement__c>{};
   		List <Delivery__c> newdelivery = new List<Delivery__c>{};
        
        //Select A Standard pricebook for the product
        Pricebook2  standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];
        
        //Start of the test execution
        Test.starttest();
        //create a new account to associate with the opportunity
        Account a = new Account(Name = 'Test Account');
        insert a;
        
        //Create a contact to assign as primary contact role
        Contact con = new Contact(FirstName = 'Maf', LastName = 'Sample', AccountId = a.Id);
        Contact con1 = new Contact(FirstName = 'Lewis', LastName = 'Test', AccountId = a.Id);
        contlist.add(con);
        contlist.add(con1);
        insert contlist;
        
        //Create a Test PriceBook for OpportunityLineItem
        Pricebook2 pbk = new Pricebook2 (Name='Standard Price Book',Description='Test Pricebook Entry 1', isActive=true);
        insert pbk;
        
        //Insert a new Product for test class
        Product2 prd = new Product2 (Name='Premium Product',Description='Test Product Entry 1',productCode = 'ABC', isActive = true);
        insert prd;
        
        //Insert a new Pricebook entry
        PricebookEntry pbe = new PricebookEntry (Product2ID=prd.id,Pricebook2ID=standardPb.id,UnitPrice=50, isActive=true,UseStandardPrice=false);
        insert pbe;
        
        //create a new opportunity to test
        Opportunity opp = new Opportunity();
        opp.Name = 'Test Opportunity';
        Opp.AccountId = a.id;
        Opp.StageName = 'Qualification';
        Opp.Trigger_Fired__c = false;
        Opp.Type = 'Existing Business';
        Opp.CloseDate =  Date.today()+2;
        bulkopp.add(Opp);
        
        CloneLineItemOppClosedWon.OpportunityCloner(bulkopp);
        insert bulkopp;
        
        System.assertEquals('Qualification',Opp.StageName);
        System.assertEquals(Opp.IsOnlineOrder__c, false);
        
        OpportunityLineItem OLI = new OpportunityLineItem();
        OLI.Quantity = 2;
        OLI.UnitPrice = 5.00;
        OLI.OpportunityId = Opp.id;
        OLI.PricebookEntryId = pbe.id;
        insert OLI;
        
        //creating opportunity contact role and make it primary
        OpportunityContactRole ocr = new OpportunityContactRole(OpportunityId = Opp.Id, ContactId = Con.Id, IsPrimary = true);
        insert ocr;
        
        Opp.StageName = 'Closed Won';
        Opp.IsOnlineOrder__c = true;
        //Opp.Trigger_Fired__c = true;
        Update Opp;
        //Update Opp1;
        bulkopp.add(Opp);
        //bulkopp.add(Opp1);
        //update bulkopp;
        CloneLineItemOppClosedWon.OpportunityCloner(bulkopp);
        System.assertEquals(Opp.IsOnlineOrder__c, true);
        
        //Creating a new Salesagreement with given condition
        Sales_Agreement__c R1 = New Sales_Agreement__c();
       	R1.Opportunity__c = OLI.Opportunityid;
        R1.Account__c = opp.AccountId;
        R1.Product__c = OLI.PricebookEntry.product2id;
        R1.Contact__c = ocr.ContactID;
        R1.Quantity__c = OLI.Quantity;
        SO1.add(R1);
        insert SO1;
        
        List<Sales_Agreement__c> salesonline = [Select Id, DeliveryCount__c, Product__c, Quantity__c, Opportunity__c From Sales_Agreement__c
                                         where Opportunity__c = :Opp.id limit 1];
        
        System.assertEquals(1, salesonline[0].DeliveryCount__c);
        
        Opp.IsOnlineOrder__c = true;
        update Opp;
        
        List<Sales_Agreement__c> salesonline1 = [Select Id, DeliveryCount__c, Product__c, Quantity__c, Opportunity__c From Sales_Agreement__c
                                         where Opportunity__c = :Opp.id limit 1];
        
        List<Delivery__c> newdel = [Select Id, Units__c, Product__c, Sales_Agreement__c, Opportunity__c From Delivery__c
                                         where Opportunity__c = :Opp.id limit 1]; 
        
        System.assertEquals(OLI.Quantity, salesonline[0].Quantity__c);
        System.assertEquals(1, salesonline1[0].DeliveryCount__c);
        System.assertEquals(2, newdel[0].Units__c);

        //System.assertEquals(Opp1.Trigger_Fired__c, true);
        
        Test.stopTest();
    }
}

 

All Answers

dphilldphill
We would need more info. Like where is it showing the error? What line? What does the code look like around where it errored? What is the error message?
Maf_007Maf_007
Hi,

My Trigger fires when opportunity stage name=closed won (after insert, after update)

It creates a custom object record called salesA_c which is associated with opportunity product

It also create another record on custom object named Delivery_c if IsOnlineDelivery(custom field on opportunity) = true.

when I run my test class on developer console:

I see a failure sign on my test log but when I expand it or go debug log it shows test method passed and a green tick next to it.

when I go to Develop->apex class it says test class failed, test nethod passed!!

Any ideas would be appreciated....
Vishnu7700Vishnu7700

Add try catch block as below

try{

//Your code

}

Expection(expection e){

}

Maf_007Maf_007

Hi,

 

This test class is giving me real pain now!! Now I am getting the following error:

System.ListException: List index out of bounds: 0
Class.TestCloneOppLineItem.TestOppMethod: line 69, column 1

 My trigger is below:

/*
Author: Mahfuz Choudhury
Date: 10/09/13
Description: Trigger on opportunity to create sales agreement and delivery
*/
trigger CloneOppLineItem on Opportunity (after insert, after update) {
    if(recursionPrevent.preventflag == false) {
   		List <Opportunity> Opp1 = Trigger.new;
   		List <Sales_Agreement__c> SO1 = New List <Sales_Agreement__c>{};
   		List <Delivery__c> newdelivery = new List<Delivery__c>{};
   		Set <Id> Oppid = New Set <Id>{};
        Set <Id> oppidonline = New Set<Id>{};
   		//Map<id, OpportunityContactRole> oppidconrole = new Map <id, OpportunityContactRole>{};
       
         for (Opportunity Opp2: Opp1){
             //Fetching opportunity id for all opportunity with closed won status
             if (Opp2.StageName == 'Closed Won'){
         			Oppid.add(Opp2.id);
             }
             /*Fetching opportunity id for all opportunity with closed won status and order is online
             else if(Opp2.StageName == 'Closed Won' && Opp2.IsOnlineOrder__c == true){
             		oppidonline.add(Opp2.id);
             }*/
         }
    
    	//Retrieving Opportunitycontactrole from contact role..
    	List <OpportunityContactRole> OppConRole = [select ContactID, OpportunityId from OpportunityContactRole where IsPrimary = true and OpportunityId in: Oppid];
    
    	//query opportunitylineitem and fetch relevant fields from pricebookentry   
    	List <OpportunityLineItem> OLI = [Select Id, OpportunityId, PricebookEntry.product2id, Quantity From OpportunityLineItem Where Opportunityid in: Oppid];
        
        for (OpportunitylineItem OLI1: OLI){
            for(OpportunityContactRole Opprole:OppConRole){
            	//creating a new Sales Agreement when opportunity stagename is closed won
            	Sales_Agreement__c R1 = New Sales_Agreement__c(
            	Opportunity__c = OLI1.Opportunityid, 
            	Product__c = OLI1.PricebookEntry.product2id,
            	Contact__c = Opprole.ContactID,
                Quantity__c = OLI1.Quantity
            	);
            //add new sales agreement record into the list
            SO1.add(R1);
            break;
            }
        }
    
    	//inserting sales agreement into database
    	Insert SO1;
        
        //query opportunitylineitem and fetch relevant fields from pricebookentry   
    	//List <OpportunityLineItem> OLIOnline = [Select Id, OpportunityId, PricebookEntry.product2id From OpportunityLineItem Where Opportunityid in: oppidonline];
        
        List <Opportunity> OnlineOpp = [Select Id, IsOnlineOrder__c,StageName From Opportunity Where id in: oppid];
        
        //Look for Sales Agreement where status is online order and opportunity is closed won
    	List <Sales_Agreement__c> salesonline = [Select Id, Name, Product__c, Quantity__c, Opportunity__c, Initial_Delivery_Date__c From Sales_Agreement__c];
        
        for (Opportunity Oponline: OnlineOpp){
            if(Oponline.IsOnlineOrder__c == true && Oponline.StageName == 'Closed Won'){
        		for(Sales_Agreement__c SAOnline: salesonline){
            		//creating a new delivery when opportunity stagename is closed won
            		Delivery__c initialdelivery = new Delivery__c(
            		Sales_Agreement__c = SAOnline.id,
            		Opportunity__c = SAOnline.Opportunity__c,
            		Product__c = SAOnline.Product__c,
            		Requested_Delivery_Date__c = SAOnline.Initial_Delivery_Date__c,
                    Units__c = SAOnline.Quantity__c
            		);
            
           			//add new delivery record into the list
            		newdelivery.add(initialdelivery);
                	break;
        		}
            }
        }
        if(newdelivery.size()>0){
    		System.debug('Insert here');
        	Insert newdelivery;
        	System.debug('Inserted');
        }
        recursionPrevent.preventflag = true;
    }
}

 And Test Class is below:

/*
Author: Mahfuz Choudhury
Date: 13.09.13
TestClassName:TestCloneOppLineItem on Opportunity
*/
@isTest(seeAllData=true)
private class TestCloneOppLineItem{
    static testMethod void TestOppMethod() {
        List<Contact> contlist = new List<Contact>{};
        
        //Select A Standard pricebook for the product
        Pricebook2  standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];
        
        //Start of the test execution
        Test.starttest();
        //create a new account to associate with the opportunity
        Account a = new Account(Name = 'Test Account');
        insert a;
        
        //Create a contact to assign as primary contact role
        Contact con = new Contact(FirstName = 'Maf', LastName = 'Sample', AccountId = a.Id);
        Contact con1 = new Contact(FirstName = 'Lewis', LastName = 'Test', AccountId = a.Id);
        contlist.add(con);
        contlist.add(con1);
        insert contlist;
        
        //Create a Test PriceBook for OpportunityLineItem
        Pricebook2 pbk = new Pricebook2 (Name='Standard Price Book',Description='Test Pricebook Entry 1', isActive=true);
        insert pbk;
        
        //Insert a new Product for test class
        Product2 prd = new Product2 (Name='Premium Product',Description='Test Product Entry 1',productCode = 'ABC', isActive = true);
        insert prd;
        
        //Insert a new Pricebook entry
        PricebookEntry pbe = new PricebookEntry (Product2ID=prd.id,Pricebook2ID=standardPb.id,UnitPrice=50, isActive=true,UseStandardPrice=false);
        insert pbe;
        
        //create a new opportunity to test
        Opportunity opp = new Opportunity();
        opp.Name = 'Test Opportunity';
        Opp.AccountId = a.id;
        Opp.StageName = 'Closed Won';
        Opp.Type = 'Existing Business';
        Opp.CloseDate =  Date.today()+2;
        insert opp;
        
        OpportunityLineItem OLI = new OpportunityLineItem();
        OLI.Quantity = 2;
        OLI.UnitPrice = 5.00;
        OLI.OpportunityId = Opp.id;
        //OLI.ProductId = pbe.Id;
        OLI.PricebookEntryId = pbe.id;
        insert OLI;
        
        //creating opportunity contact role and make it primary
        OpportunityContactRole ocr = new OpportunityContactRole(OpportunityId = Opp.Id, ContactId = Con.Id, IsPrimary = true);
        insert ocr;
        
        System.assertEquals('Closed Won',Opp.StageName);
        System.assertEquals(Opp.IsOnlineOrder__c, false);
        
        Opp.IsOnlineOrder__c = true;
        Update Opp;
        System.assertEquals(Opp.IsOnlineOrder__c, true);
        
        List<Sales_Agreement__c> salesonline = [Select Id, Product__c, Quantity__c, Opportunity__c From Sales_Agreement__c
                                         where Opportunity__c = :Opp.id limit 1];
        System.assertEquals(OLI.Quantity, salesonline[0].Quantity__c);
        
        Test.stopTest();
    }
}

 

Can Anyone figure out what's happening there?? 

Or any alternative to get 100% cover??

Maf_007Maf_007

I have fixed the error since I was trying to query sales agreement object without inserting a sales agreement. I have inserted a sales agreement once Opportunity is updated and also inserted a delivery. But still it's not giving me enough coverage, I am getting only 62%, Which I was getting anyway even before inserting these two records. Also, Winter 14 you can't view which part of class is not covered by test class as well!! So, I am a bit stuck here. Any suggestion???

Maf_007Maf_007

Hi Guys, The following class is getting 100% Coverage if anyone needs it:

@isTest(seeAllData=true)
private class TestCloneOppLineItem{
    static testMethod void TestOppMethod() {
        CloneLineItemOppClosedWon ObjConfirm = new CloneLineItemOppClosedWon();
        List<Contact> contlist = new List<Contact>{};
        List <Opportunity> bulkopp = new List<Opportunity>{};
        List <Sales_Agreement__c> SO1 = New List <Sales_Agreement__c>{};
   		List <Delivery__c> newdelivery = new List<Delivery__c>{};
        
        //Select A Standard pricebook for the product
        Pricebook2  standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];
        
        //Start of the test execution
        Test.starttest();
        //create a new account to associate with the opportunity
        Account a = new Account(Name = 'Test Account');
        insert a;
        
        //Create a contact to assign as primary contact role
        Contact con = new Contact(FirstName = 'Maf', LastName = 'Sample', AccountId = a.Id);
        Contact con1 = new Contact(FirstName = 'Lewis', LastName = 'Test', AccountId = a.Id);
        contlist.add(con);
        contlist.add(con1);
        insert contlist;
        
        //Create a Test PriceBook for OpportunityLineItem
        Pricebook2 pbk = new Pricebook2 (Name='Standard Price Book',Description='Test Pricebook Entry 1', isActive=true);
        insert pbk;
        
        //Insert a new Product for test class
        Product2 prd = new Product2 (Name='Premium Product',Description='Test Product Entry 1',productCode = 'ABC', isActive = true);
        insert prd;
        
        //Insert a new Pricebook entry
        PricebookEntry pbe = new PricebookEntry (Product2ID=prd.id,Pricebook2ID=standardPb.id,UnitPrice=50, isActive=true,UseStandardPrice=false);
        insert pbe;
        
        //create a new opportunity to test
        Opportunity opp = new Opportunity();
        opp.Name = 'Test Opportunity';
        Opp.AccountId = a.id;
        Opp.StageName = 'Qualification';
        Opp.Trigger_Fired__c = false;
        Opp.Type = 'Existing Business';
        Opp.CloseDate =  Date.today()+2;
        bulkopp.add(Opp);
        
        CloneLineItemOppClosedWon.OpportunityCloner(bulkopp);
        insert bulkopp;
        
        System.assertEquals('Qualification',Opp.StageName);
        System.assertEquals(Opp.IsOnlineOrder__c, false);
        
        OpportunityLineItem OLI = new OpportunityLineItem();
        OLI.Quantity = 2;
        OLI.UnitPrice = 5.00;
        OLI.OpportunityId = Opp.id;
        OLI.PricebookEntryId = pbe.id;
        insert OLI;
        
        //creating opportunity contact role and make it primary
        OpportunityContactRole ocr = new OpportunityContactRole(OpportunityId = Opp.Id, ContactId = Con.Id, IsPrimary = true);
        insert ocr;
        
        Opp.StageName = 'Closed Won';
        Opp.IsOnlineOrder__c = true;
        //Opp.Trigger_Fired__c = true;
        Update Opp;
        //Update Opp1;
        bulkopp.add(Opp);
        //bulkopp.add(Opp1);
        //update bulkopp;
        CloneLineItemOppClosedWon.OpportunityCloner(bulkopp);
        System.assertEquals(Opp.IsOnlineOrder__c, true);
        
        //Creating a new Salesagreement with given condition
        Sales_Agreement__c R1 = New Sales_Agreement__c();
       	R1.Opportunity__c = OLI.Opportunityid;
        R1.Account__c = opp.AccountId;
        R1.Product__c = OLI.PricebookEntry.product2id;
        R1.Contact__c = ocr.ContactID;
        R1.Quantity__c = OLI.Quantity;
        SO1.add(R1);
        insert SO1;
        
        List<Sales_Agreement__c> salesonline = [Select Id, DeliveryCount__c, Product__c, Quantity__c, Opportunity__c From Sales_Agreement__c
                                         where Opportunity__c = :Opp.id limit 1];
        
        System.assertEquals(1, salesonline[0].DeliveryCount__c);
        
        Opp.IsOnlineOrder__c = true;
        update Opp;
        
        List<Sales_Agreement__c> salesonline1 = [Select Id, DeliveryCount__c, Product__c, Quantity__c, Opportunity__c From Sales_Agreement__c
                                         where Opportunity__c = :Opp.id limit 1];
        
        List<Delivery__c> newdel = [Select Id, Units__c, Product__c, Sales_Agreement__c, Opportunity__c From Delivery__c
                                         where Opportunity__c = :Opp.id limit 1]; 
        
        System.assertEquals(OLI.Quantity, salesonline[0].Quantity__c);
        System.assertEquals(1, salesonline1[0].DeliveryCount__c);
        System.assertEquals(2, newdel[0].Units__c);

        //System.assertEquals(Opp1.Trigger_Fired__c, true);
        
        Test.stopTest();
    }
}

 

This was selected as the best answer