Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I'm having trouble with what should be a simple test class for a simple trigger.

The trigger inserts the ID of an account named Retail into a lookup field on the opportunity named Project__c if that field is blank. The trigger works but the test class fails.

The error I'm getting is:-->System.AssertException: Assertion Failed: Expected: 0011100000ArLgYAAV, Actual: null

So, my test isn't updating the ID although in real application the ID is updated?

If anyone can let me know what I've done wrong it would be much appreciated.

Thanks!

Trigger

trigger AutoRetail on Opportunity (before insert, before update) {

Account RetailAccount = [
    Select Id
    From Account
    Where Name = 'Retail'
];

for (Opportunity o : Trigger.new) {

        if(o.Project__c == Null){

o.Project__c = RetailAccount.Id;

Test Class

@IsTest
private class AutoRetail_Test {

static testMethod void AutoRetail() {

    // Create an Account
    Account a = new account();
    a.Name = 'Test Account';
    insert a;

    // Create a 'Retail' Account
    Account r = new account();
    r.Name = 'Retail';
    insert r;

    // Create an Opportunity
    Opportunity opp = new Opportunity();
    opp.Name = 'TestOppFromTestClass' ;
    opp.StageName = 'Stage 1 - Reviewing Information' ;
    opp.CloseDate = Date.Today() ;
    opp.Accountid = a.id;
    insert opp ;


    // Query Success

    system.AssertEquals(r.id, opp.Project__c);


}

}
share|improve this question

2 Answers 2

up vote 4 down vote accepted

You need to query result before the assertion.

For example:

List<Opportunity> Opp=[Select Project__c FROM Opportunity WHERE ID =:opp.id];

system.AssertEquals(r.id,Opp[0].Project__c);
share|improve this answer
    
Worked a treat thank you! Still learning. –  user7324 Mar 12 at 15:24

Try to retrieve the inserted opportunity again and then do the assertion against that.
opp = [SELECT Name, Project__c FROM Opportunity WHERE ID =: opp.Id]; System.AssertEquals(r.id, opp[0].Project__c);

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.