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.



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

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);



You need to query result before the assertion.

For example:

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


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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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