2

Let me preface this by saying that I am very unfamiliar with Salesforce. I have written a set of small discovery type tests to try and feel my way through the syntax and how things work, but I am getting stuck with no values coming back from my queries.

@isTest
private class SmartSearchTests {
    //works exactly as expected
    static testMethod void trueIsTrue() {
        System.assertEquals(true, true);
    }

    //works as expected
    static testMethod void trueIsNotTrue() {
        System.assertNotEquals(true, false);
    }

    //Same value 0
    static testMethod void canGetASmallListOfContacts() {
        List<Contact> ccs = [SELECT Id from Contact Limit 10];
        System.assertNotEquals(0, ccs.size());
    }

    //invalid ID field: 0Q0M987654AcTg
    static testMethod void canLoadAQuoteById() {
        Quote q = [SELECT Id from Quote where Id = '0Q0M987654AcTg'];
        System.assertNotEquals(null, q);    
    }
}

I can see that on the 4th test, I am doing something incorrect by trying to use a string as an Id, but I cannot for the life of me firgure out my the third test returns no data. The same is true when I run this test against any built in object type and I know there is data there.

Also, does [SELECT Id from Contact Limit 10] evaluate to a concrete type? For example could I pass that block of code around to other functions, i.e., doSomething(id, [select Id from Account])?

Thanks, Joe

2

1 Answer 1

6

You can find the detailed explanation here: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_isTest.htm

The short answer is that unless you specify it, from an apex test you don't have visibility on the org's existing data, therefore, you need to create it first.

6
  • Thanks! @IsTest(SeeAllData=true) is exactly what I needed.
    – Joe
    Commented Jan 10, 2014 at 19:52
  • 1
    I would not use SeeAllData as an alternative to get around this. It will solve the problem, but its a very bad habit to get into. Creating tests that are dependent on your org data is not a good idea. It is not best practice, I would create your own test data. Take a look here wiki.developerforce.com/page/How_to_Write_Good_Unit_Tests Commented Jan 10, 2014 at 20:06
  • @sfdc_ninja - thanks for the pointers. I will make sure to not use this in a real world case, but am just trying to wrap my head around syntax and usage with discovery type tests.
    – Joe
    Commented Jan 13, 2014 at 13:24
  • No prob. Just trying to help you in the long run. Good luck. Commented Jan 13, 2014 at 13:36
  • @sfdc_ninja do you think it's okay to use SeeAllData if my main class leverages custom settings to avoid hard coded data? I've created ~40 custom setting records that I'd prefer to not have to re-create in my test class.
    – kbentsen
    Commented Jan 13, 2014 at 18:22

You must log in to answer this question.

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