Skip to main content
Hi,

I'm just doing a real basic trigger where if shipping information is not filled in on an opportunity, it pulls this information from the account shipping information.  When I test it in the test org, it's working fine.  But my test class isn't seeing the same thing.  I'm not sure what I'm doing wrong.

trigger

trigger OpportunityTrigger on Opportunity (before insert) {

Set<String> oppSet = new Set<String>();

for(Opportunity opp:Trigger.New) {

oppSet.add(opp.AccountId);

}

List<Account> acctList = [SELECT Id, ShippingStreet, ShippingCity, ShippingState, ShippingPostalCode FROM Account WHERE Id IN:oppSet];

Map<String, Account> accMap = new Map<String, Account>();

for(Account acct:acctList) {

accMap.put(acct.Id, acct);

}

for(Opportunity opp:Trigger.new) {

Account acct = accMap.get(opp.AccountId);

if (opp.Shipping_Address__c == NULL) { opp.Shipping_Address__c = acct.ShippingStreet; }

if (opp.Shipping_City__c == NULL) { opp.Shipping_City__c = acct.ShippingCity; }

if (opp.Shipping_State__c == NULL) { opp.Shipping_State__c = acct.ShippingState; }

if (opp.Shipping_Zip__c == NULL) { opp.Shipping_Zip__c = acct.ShippingPostalCode; }

}

}

and the test class

@isTest

public class OpportunityTriggerTest {

@isTest public static void OpportunityTest() {

Account acct = new Account (Name = 'test 123',

ShippingState = 'CA',

ShippingStreet = '1234 drive',

ShippingCity = 'Buellton');

Insert(acct);

Opportunity opp = new Opportunity(Name='test opp',

AccountId=acct.id,

StageName = 'W.Win',

CloseDate = system.today(),

of_Doors__c = 0);

test.startTest();

insert(opp);

System.assertEquals(acct.ShippingStreet, opp.Shipping_Address__c);

test.stopTest();

}

}

Thanks,
3 answers
  1. Jun 27, 2015, 3:59 AM
    Hi Mathew,

    Everything seems to be fine in your test class except one SOQL prior to assert. you need to query Opp from the database to do the assert.

    Use following test class code it should work

     

    @isTest

    public class OpportunityTriggerTest {

    @isTest public static void OpportunityTest() {

    Account acct = new Account (Name = 'test 123',

    ShippingState = 'CA',

    ShippingStreet = '1234 drive',

    ShippingCity = 'Buellton');

    Insert(acct);

    Opportunity opp = new Opportunity(Name='test opp',

    AccountId=acct.id,

    StageName = 'W.Win',

    CloseDate = system.today(),

    of_Doors__c = 0);

    test.startTest();

    insert(opp);

      Opportunity insertedopp = [select Shipping_Address__c from opportunity where id =: opp.id];

    System.assertEquals(acct.ShippingStreet, insertedopp.Shipping_Address__c);

    test.stopTest();

    }

    }

    Thanks,

    Himanshu

     
0/9000