0

i wrote a test class that is working fine in my sandbox enviroment, but for some reason on production it fails nad gives me the message:

"System.QueryException: List has no rows for assignment to SObject"

Test Class:

@isTest
private class AvaliableCandidatesTest {

 @testSetup
static void prepData(){
    try{
                Contact firstCon = new Contact(
                    FirstName='first',
                    LastName='last',
                    Process_status__c ='Visa Requested',    
                    Passport_Expiry_Date__c= Date.newInstance(2018,5,5), 
                    Degree__c = 'test',
                    Student_University__c = '0014E00000NYFMcQAP',
                    Education_Major__c = 'Animal',                         
                    Year_of_Completion__c = '1978',
                    Passport_Issue_Date__c  = Date.newInstance(2016,5,5),
                    Passport_ID__c= '123456',
                    Visa_Confirmation_Date_of_Request__c = Date.newInstance(2017,12,31),
                    Visa_Application_Verified_Approved__c  = true, 
                    Visa_Application_1402_Attached__c = true, 
                    Visa_Application_956_Attached__c = true, 
                    Initial_Engagement_Attached__c = true, 
                    Health_Insurance_Attached__c = true, 
                    CV_Attached__c = true, 
                    Highest_Qualification_Attached__c = true,    
                    Passport_Attached__c = true
                );
insert firstCon;

    Account a = new Account(Name = 'third farm', RecordTypeId = '01258000000VAZTAA4',Type = 'Farm');
    insert a;

    }catch(Exception e){
    System.debug('-----' + e.getMessage());
    }                 
}

public static testMethod void testMyController() {
    PageReference pageRef = Page.Available_Candidates;
    Test.setCurrentPage(pageRef);

    Contact cont = [SELECT Id FROM Contact LIMIT 1] ; //!!! HERE IT FAILS 
    Account acc = [SELECT Id FROM Account LIMIT 1] ;

    AvailableCandidates control = new AvailableCandidates();
    Test.startTest();
control.processSelected();
Test.stopTest() ; 
}

}

It fails on requesting for contact line: "Contact cont = [SELECT Id FROM Contact LIMIT 1] ;"

Why is that happening?

  • Your data is not inserted properly in the testsetup o.w. it should be retrieved from testmethod. – RCS Dec 29 '16 at 10:50
  • @RCS can you give me an example? – Eli Dec 29 '16 at 11:58
2

The likely cause is this line in your testmethod

Student_University__c = '0014E00000NYFMcQAP',

This is an Account ID and as such , may not exist in PROD

Never use hard-coded IDs in testmethods as there is no guarantee that they will be the same between orgs (sandbox v PROD v other sandbox)

Mock the Account for the Student University in testSetup and use its ID when mocking the Contact

And, if testsetup gets any errors in the mocking, the testmethod needs to stop immediately - preferably with a system.assert(false,'mocking failed' +e.getMessage());

N.B. RecordType Ids should also never be hard-coded; query for them or use Describe methods (less desirable since describe only locates labels, not developernames)

1

The problem is that in production your contact that you are inserting does not meet some criteria that was not present in the sandbox

When you try to insert the contact it fails. Since you wrapped it in a try catch block you neve know it failed to insert.

Remove the try catch block and you will see why it failed. Best to not use try catch block in test setup methods anyways as you need the data inserted.

Also, you can view the debug log when running the test to see the error so you know what you need to fix before moving to production.

  • Usually best to use asserts to view the data being created correctly – Dave Humm Dec 29 '16 at 14:20
  • That's a whole nother topic. But I agree. Test setup should never be caught or used to create data that is ok if it fails to be inserted – Eric Dec 29 '16 at 14:21
0

Because it is expecting the List returned.To prevent it you can use;

List<Contact> cont = [SELECT Id FROM Contact LIMIT 1] ;

Same goes to the query.If you don't want to use List, you can use try and catch to handle the exception.

Please refer to the article here : List has no rows for assignment to SObject

  • thanx @unidha but why cant i see it in sandbox as well? – Eli Dec 29 '16 at 10:59
  • What does it mean by 'see it in sandbox'? – unidha Dec 29 '16 at 12:14
  • the error. how can i validate it in sandbox? – Eli Dec 29 '16 at 12:37
  • Check what is the api version . Also check if the data successfully insert in Production for ex. maybe some Contact validation rules prevent the data being inserted. – unidha Dec 29 '16 at 13:03
  • @Eli suggest you look at my answer as to why. A list will just get you an empty list here – Eric Dec 29 '16 at 14:22

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.