0

I'm trying to test a trigger that I've created, however the test class I made doesn't ever finish testing - it stops after two lines of code.

This is the Debug log that I get from Eclipse:

Debug Log:

29.0  APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;VALIDATION,INFO;WORKFLOW,INFO
12:46:59.492 (5492695950)|EXECUTION_STARTED
12:46:59.492 (5492727516)|CODE_UNIT_STARTED|[EXTERNAL]|01pg0000000E0SX|BusinessDivisionSystemLeadTest.myUnitTest
12:46:59.493 (5493155740)|METHOD_ENTRY|[3]|01pg0000000E0SX|BusinessDivisionSystemLeadTest.BusinessDivisionSystemLeadTest()
12:46:59.493 (5493166994)|METHOD_EXIT|[3]|BusinessDivisionSystemLeadTest
12:46:59.493 (5493526755)|SOQL_EXECUTE_BEGIN|[7]|Aggregations:0|select id from User where name = 'System'
12:46:59.500 (5500320555)|SOQL_EXECUTE_END|[7]|Rows:1
12:46:59.556 (5556219949)|SOQL_EXECUTE_BEGIN|[10]|Aggregations:0|select id from User where name = 'SF Admin'
12:46:59.594 (5594632531)|SOQL_EXECUTE_END|[10]|Rows:1
12:46:57.919 (5666769519)|CUMULATIVE_LIMIT_USAGE
12:46:57.919|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 2 out of 100
  Number of query rows: 2 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 1 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

12:46:57.919|CUMULATIVE_LIMIT_USAGE_END

12:46:59.666 (5666808270)|CODE_UNIT_FINISHED|BusinessDivisionSystemLeadTest.myUnitTest
12:46:59.666 (5666821851)|EXECUTION_FINISHED

This is the test class I created:

@isTest

private class BusinessDivisionSystemLeadTest 
{
    private static testMethod void myUnitTest()
    {
        User H = [select id from User where name='System'];
        System.runAs(H)
        {    
            User M = [select id from User where name = 'SF Admin'];
            Lead l = new Lead();
            l.Company = 'Test Company';
            l.LastName = 'Test';
            if (l.createdBy == H)
            {
                insert l;
                l.OwnerId = M.id;
                update l;
            }
        }


    }
}

My trigger is receiving 0% code coverage, as the test class isn't running every line. No errors are being thrown, so I don't know what's wrong. Any help would be great. Thanks!

0

2 Answers 2

3

The line:

 if (l.createdBy == H)

will never be true as the createdBy Field isn't instantiated until after an insert operation

1

I +1 crop's answer but I can give a little more detail.

When you hit the line:

if (l.createdBy == H)

the lead has not yet been created. That means this field will not have been populated yet. You already know you are running as user H, and so this check really doesn't do anything useful. It will evaluate to true 100% of the time if it was working the way that you intended.

Basically, just remove the line if (l.createdBy == H) and it should work.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .