2

I am having a hard time wrapping my head around a concept.

I write a query that pulls out data from salesforce and sends it to a web service. The webservice sends me back data for a report. This data is saved into a custom object.

Why am I able to hard code, in my test class, in my dev environment, the data I am sending to the webservice? Why does this hardcoded mock data not transfer over to prod? In dev I am getting full test coverage and the green check to upload. But when my test class runs against the prod's tests, it fails with the error of null data.

Why am I able to deploy from sandbox to sandbox with hardcoded test data but not deploy to prod?

It doesn't make sense to me and I think I need to understand this before I am able to find the solution to deploy to prod.

public static testmethod void testAccountCallout() 
{ 
    createBVS();
    oppId = [SELECT Opportunity__c FROM Credit_Review__c WHERE Credit_Review__r.Id = '123456789'].Opportunity__c; 
    SingleRequestMockClass fakeResponse = new SingleRequestMockClass(201,'Complete','{\"SignorZipCode\":"92109,\"SignorStreetAddress\":"123 Main St",\"SignorState\":"CA",\"SignorSSN\":"123222222",\"SignorLastName\":null,\"SignorHomePhone\":null,\"SignorFirstName\":null,\"SignorCity\":null,\"PhysicalAddressZip\":\"92109\",\"PhysicalAddressState\":\"CA\",\"PhysicalAddressCity\":\"San Diego\",\"PhysicalAddress1\":\"111 Rio Way\",\"FedTaxId\":"11111111",\"CompanyLegalName\":\"Broker The Joker\"}',null);
    Test.startTest();
    Test.setMock(HttpCalloutMock.class, fakeResponse);
    sendToWebService2 ctrl = new sendToWebService2(new ApexPages.StandardController(new Credit_Review__c()));
    ctrl.pullReport();
    Test.stopTest(); 
    System.assert(true);
    System.assertNotEquals(null, fakeResponse, 'The callout returned a null response.');
}
public static testmethod void createBVS() {
    Business_Verification_Summary__c bvs = new Business_Verification_Summary__c();
    bvs.Lexis_Nexis_Report_Link__c = 'jjjjjjjjjjjsssssssssonnnnnn';
    bvs.Status__c = 'Completed';
    system.assertEquals('Completed', bvs.Status__c);
}

UPDATE:

public static testmethod void createBVS() {
    Account a = new Account(Name = 'Test Account Olivia');
    insert a;
    Opportunity o = new Opportunity(AccountId = a.Id, Name = 'LexisNexis', CloseDate = Date.today(), StageName = 'Prospecting');
    insert o;
    Credit_Review__c crc = new Credit_Review__c(Opportunity__c = o.Id);
    insert crc;
    Business_Verification_Summary__c bvs = new Business_Verification_Summary__c();
    bvs.Lexis_Nexis_Report_Link__c = 'jjjjjjjjjjjsssssssssonnnnnn';
    bvs.Status__c = 'Completed';
    bvs.Credit_Review__c = crc.Id;
    insert bvs;
    system.assertEquals('Completed', bvs.Status__c);
}
8
  • You will probably get more illuminating answers if you share your code.
    – Adrian Larson
    Commented Nov 1, 2016 at 0:18
  • oppId = [SELECT Opportunity__c FROM Credit_Review__c WHERE Credit_Review__r.Id = '123456789'].Opportunity__c; is the line that prod is saying has null data. But it does have this data, in the sandbox I am working in at least.
    – Olivia
    Commented Nov 1, 2016 at 0:24
  • Where is your Credit_Review__c record coming from? Is there a @isTest(SeeAllData=true) annotation used on that method?
    – Nick C
    Commented Nov 1, 2016 at 0:26
  • It is a custom object in salesforce. The order is-> Create a new lead- convert the lead to an account - the opportunity that is created with the account has Credit_Review__c on it - Inside of Credit_Review__c page is Business_Verification_Summary__c. When I use isTest(SeeAllData=true) it fails in prod. I should not have to use this.
    – Olivia
    Commented Nov 1, 2016 at 0:29
  • @Olivia But where is that happening? The code you've provided does none of that.
    – Nick C
    Commented Nov 1, 2016 at 0:40

1 Answer 1

3

Note from Isolation of Test Data from Organization Data in Unit Tests:

Starting with Apex code saved using Salesforce API version 24.0 and later, test methods don’t have access by default to pre-existing data in the organization, such as standard objects, custom objects, and custom settings data, and can only access data that they create. However, objects that are used to manage your organization or metadata objects can still be accessed in your tests such as:

  • User
  • Profile
  • Organization
  • AsyncApexJob
  • CronTrigger
  • RecordType
  • ApexClass
  • ApexTrigger
  • ApexComponent
  • ApexPage

Any data you want to exist in your test, you should set up yourself. This will always include record instantiation and an insert statement.

MyObject__c record = new MyObject__c(/*required fields*/); // instantiate
insert record; // insert

If there are relationships to other objects, you must create the parent records first, and then set the parent Id in the child record's lookup field.

Parent__c parent = new Parent__c();
insert parent;
MyObject__c record = new MyObject__c(Parent__c=parent.Id);
insert record;

Related Reading:

3
  • This does make sense and I did not define the parents. I made some updates with your suggestions. Additional question - does this mean you can never surpass the parents and just declare the value of the children?
    – Olivia
    Commented Nov 1, 2016 at 1:02
  • You only need to insert the parents if the lookup is required, or if you are trying to query based on its value.
    – Adrian Larson
    Commented Nov 1, 2016 at 1:03
  • once again you saved my ass. It validated. Thank you, this was an educational day!
    – Olivia
    Commented Nov 1, 2016 at 1:30

You must log in to answer this question.

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