+ Start a Discussion
emillar_altiaemillar_altia 

Test class failures when deploying to production but not in sandbox

I have a test class in sandbox which has passed, but when I take it into Production I get the below error, I don't understand what the problem is when its passed in Sandbox? 

 

TestCopyOpportunityProductFields.testCopy() Class 47 1

Failure Message: "System.AssertException: Assertion Failed: Expected: 2013-10-22 00:00:00, Actual: null", Failure Stack Trace: "Class.TestCopyOpportunityProductFields.testCopy: line 47, column 1"

 

I have copied in my test class below. 

 

@isTest(SeeAllData=true)
public class TestCopyOpportunityProductFields    {
    static testMethod void testCopy() {
    
        Product2 p = new Product2();
        p.Name = 'SFDC99 Rocks';
        p.Deferral_Percentage__c = 0;
        insert p;
        
        Pricebook2 pb = [SELECT Id FROM Pricebook2 where isStandard = true];
        PricebookEntry pbe = new PricebookEntry();
        pbe.Product2Id   = p.Id;
        pbe.UnitPrice    = 99;
        pbe.Pricebook2Id = pb.Id;
        pbe.IsActive     = true;
        insert pbe;
        
        Opportunity o = new Opportunity();
        o.CloseDate = Date.today() + 30;
        o.StageName = 'New';
        o.Name      = 'Follow me in Twitter';
        insert o;
        
        OpportunityLineItem oli = new OpportunityLineItem();
        oli.OpportunityId    = o.Id;
        oli.PricebookEntryId = pbe.Id;
        oli.Quantity         = 99;
        oli.UnitPrice        = 1;
        oli.Start_Date__c    = Date.today();
        oli.Duration__c      = 5;
        insert oli;
        
        Quote q = new Quote();
        q.OpportunityId = o.Id;
        q.Name          = '@dvdkliu';
        q.Pricebook2Id  = pb.Id;
        insert q;
        
        QuoteLineItem qli = new QuoteLineItem();
        qli.PricebookEntryId = pbe.Id;
        qli.QuoteId          = q.Id;
        qli.Quantity         = 99;
        qli.UnitPrice        = 1;
        insert qli;
        
        List<QuoteLineItem> qlis = [SELECT Id, Start_Date__c, Duration__c FROM QuoteLineItem WHERE Id = :qli.Id];
        System.assertEquals(oli.Start_Date__c, qlis[0].Start_Date__c);
        System.assertEquals(oli.Duration__c, qlis[0].Duration__c);
        
    }
}

 

 

Also, my trigger is saying this. 

 

CopyOpportunityProductFields       Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required

 

trigger CopyOpportunityProductFields on QuoteLineItem (after insert) {

    // Query quote line items
    List<QuoteLineItem> qlis = [SELECT Id, PricebookEntry.Product2Id, Quote.OpportunityId FROM QuoteLineItem WHERE Id IN :Trigger.new];

    // Query opp line items
    Map<Id, OpportunityLineItem> productIdToOli = new Map<Id, OpportunityLineItem>();
    List<OpportunityLineItem> olis = [SELECT Id, PricebookEntry.Product2Id, Start_Date__c, Duration__c, End_Date__c FROM OpportunityLineItem WHERE OpportunityId = :qlis[0].Quote.OpportunityId];
    if (olis != null) {
        for (OpportunityLineItem oli : olis) {
            productIdToOli.put(oli.PricebookEntry.Product2Id, oli);
        }
    }

    // Iterate across quote line items
    for (QuoteLineItem qli : qlis) {
        OpportunityLineItem oli = productIdToOli.get(qli.PricebookEntry.Product2Id);   
        if (oli != null) {
            if (oli.Start_Date__c != null) { qli.Start_Date__c = oli.Start_Date__c; }
            if (oli.Duration__c   != null) { qli.Duration__c   = oli.Duration__c ;  }
        }
    }
    
    // Update 
    update qlis;
}

Best Answer chosen by Admin (Salesforce Developers) 
Eric_HDCEric_HDC

Shouldn't that say:

System.assertEquals(oli.Start_Date__c, qli[0].Start_Date__c);

instead of:

System.assertEquals(oli.Start_Date__c, qlis[0].Start_Date__c);

All Answers

Eric_HDCEric_HDC

Shouldn't that say:

System.assertEquals(oli.Start_Date__c, qli[0].Start_Date__c);

instead of:

System.assertEquals(oli.Start_Date__c, qlis[0].Start_Date__c);

This was selected as the best answer
emillar_altiaemillar_altia

l'll test it and find out thanks.