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.

Why does my taskOpp query below not return any records when I run this test? The query works fine in Workbench and my taskRT query works fine in both my code and in Workbench.

public class LinkCampaign2Task_test {
  static testMethod void validateOppTaskCampaignId(){
    List <Opportunity> taskOpp = [SELECT CampaignId,Id FROM Opportunity WHERE CampaignId != ''];
    List <RecordType> taskRT = [SELECT Id FROM RecordType WHERE Name = 'Outbound Call'];
share|improve this question

5 Answers 5

up vote 6 down vote accepted

You need to create Opportunities in your test class. When you run test classes its like a blank database. You need to create a Campaign then link an opportunity to it.

Campaign camp = new Campaign();
insert camp;

insert new Opportunity(CampaignId = camp.Id);

EDIT: Like others have said, you can use the @seealldata to see existing data in your org, but it is highly suggested to NOT do this. Instead it is better to create your test data in the code for tests (or an static resource import).

share|improve this answer
2  
As dphil says it is best practice to create your data set inside the unit test. Alternatively if needed you can use the IsTest(SeeAllData=true) Annotation to see existing data. salesforce.com/us/developer/docs/apexcode/Content/… –  CoryCowgill Aug 28 at 19:56

In your test example you have not populated any data for the test to run against. It is best practice to create test data for each unit test, them validate your code via asserts. Thankfully you do not have to worry about removing the data as Salesforce properly tracks and deletes the test objects for you at the end of each test method.

Additionally, while it is possible for you to access the org's real data within a unit test, that is bad practice. However, there are a few cases where that is required. Thankfully most of those cases are reported by the test runner.

share|improve this answer

As of API v.24 or later, data in your organization is isolated from tests. There are two ways to address this. You can change your tests to (SeeAllData=true), an annotation of @isTest or you can create your context data in your test prior to running a SOQL. I recommend creating your test data within your tests as a best practice because you can more tightly control your test scenario and you don't have to rely on instance data. Relying on instance data makes your code less portable because your tests rely on data + code logic to function correctly and only certain types of sandboxes will move your data, and none will keep data in sync with production.

See this article on Isolation of Test Data from the documentation.

share|improve this answer

@jwolf is correct, but I would like to add that you should not just throw in the SeeAllData tag as this is not best practice. For a test method, you first need to create your own data.

In the code below, your query will now return a list containing your newly created opportunity.

public class LinkCampaign2Task_test {

  static testMethod void validateOppTaskCampaignId(){

    Opportunity opp1 = new Opportunity();
    //Add your fields
    insert opp1;

    List <Opportunity> taskOpp = [SELECT CampaignId,Id FROM Opportunity WHERE CampaignId != ''];
    List <RecordType> taskRT = [SELECT Id FROM RecordType WHERE Name = 'Outbound Call'];

  }
}

I would take a look at some documentation around test data. Here are some good ones

https://developer.salesforce.com/page/How_to_Write_Good_Unit_Tests

http://www.tquila.com/blog/2011/11/17/salesforce-testing-best-practices

https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

share|improve this answer

Test methods cannot access any non setup data unless you use the @seeAllData annotation. See the "Isolation of Test Data from Organization Data in Unit Tests" article in the apex developer guide. Here's a link.

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.