1

The following code fails, but when I execute the query in the Query Editor I get 2 records, thus the test should pass. I have no idea what I am doing wrong. I am C# developer, first Visualforce plugin I am creating and need a little assistance.

@isTest
public class SendLoanApplicationTest {

static testMethod void verifyCanGetLeads(){

    String soql = 'SELECT COUNT() FROM Lead';
    Integer cnt = Database.countQuery(soql);

    // This fails
    System.assertEquals(cnt, 2);

    /*
    List<Lead> leadList = new List<Lead>{};
    Integer x = 0;
    for(Lead l: [SELECT Id, Name FROM Lead]){
        leadList.add(l);
    }

    // This fails
    System.assert(leadList.size() > 0);
    */
  }
}
4
  • 3
    Test cases will by default run in isolation of existing org data. So it won't be able to see any existing leads. Ideally your test should insert any test data it needs first. See also @isTest(SeeAllData=true) Try to avoid using live data whenever you can. – Daniel Ballinger Oct 15 '15 at 23:11
  • 1
    I'd be a bit stronger than Daniel (who I have a great deal of respect for) - testmethods should never rely on org data unless it is data that simply can't be mocked (like ContentWorkspace). Leads can be mocked – cropredy Oct 15 '15 at 23:38
  • 1
    Thanks @Daniel, that solved my problem. Adding (SeeAllData = true) worked. – Robert Green MBA Oct 16 '15 at 0:00
  • 1
    @RobertGreen That's great that it is working now. Now take a moment and consider scripting in the test data you need to get the test passing rather than using SeeAllData=true. Yes it takes more work now, but it makes your tests much more robust. You'll be happy you took the time to do it right later on. – Daniel Ballinger Oct 16 '15 at 0:24
1

As per the comments:

Test cases will by default run in isolation of existing org data. So it won't be able to see any existing leads. Ideally your test should insert any test data it needs first. See also @isTest(SeeAllData=true) Try to avoid using live data whenever you can.

If you do find adding SeeAllData=true makes your test class pass, I'd strongly advise revisiting the test to script in any required data and switching back to @isTest(SeeAllData=false).

"Why?" you ask when your test case is now passing?
Your test might be working right now, but some point in the future the records that it depends on may change or be deleted. Records will differ as you deploy changes between sandboxes and production. Test case code that depends on existing data becomes very fragile and will break at the most inopportune moment.

There are plenty of examples on this site of people having deployment problems late Friday night and asking for urgent help because the tests that were working sometime last year are now mysteriously failing in production. Someone or something changed in the underlying data. Now they can't deploy the urgent change set that the sales department wants ASAP and the CTO is standing at their desk demanding answers.

Yes it can be a pain to setup all the required test data. However, it is a good habit to get into and will save you from much greater pain later on.

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.