0

System.QueryException: List has no rows for assignment to SObject Class.Account_Create_Contact_TestClass.myUnitTest: line 29, column 1

@isTest
private class Account_Create_Contact_TestClass {

    static testMethod void myUnitTest() {

        Profile p = [SELECT Id FROM Profile WHERE Name = 'BLX Telemarketer' LIMIT 1];
        User u = [SELECT Id FROM User WHERE ProfileId = :p.Id AND User.IsActive = true LIMIT 1];
        List<Account> accs = new List<Account>();
        List<Account> conAccs = new List<Account>();
        Set<Id> accIds = new Set<Id>();

        Integer i = 0;
        System.runAs(u) {

            // Test Accounts with no Last Name has been added
            for (Account a : [SELECT Id FROM Account LIMIT 50]) {
                a.Eerstenaam__c = 'Test First Name';
            }

            if (!accs.isEmpty()) {
                update(accs);
                accs.clear();
            }

            for (Account a : [SELECT Id FROM Account LIMIT 50]) {
                accIds.add(a.Id);
                a.Eerstenaam__c = 'Test First Name';
                a.Achternaam__c = 'Test Last Name';
                a.Nieuwsbrieven_ontvangen__c = false;
                if (i == 0) {
                    a.Nieuwsbrieven_Ontvangen__c = true;
                    a.Dezelfde_Email__c = true;
                    i = 1;
                }
                else if (i == 1) {
                    i = 2;
                    a.Status_Belactie_2010__c = 'Wil Gebeld Worden Door Account Manager';
                    a.Opmerkingen__c = 'This is a test Task';
                }
                else {
                    a.Achternaam__c = NULL;
                }
                accs.add(a);
            }
            if (!accs.isEmpty()) {
                update(accs);
            }

            for (Contact c : [SELECT Id, LastName, AccountId FROM Contact WHERE AccountId IN :accIds AND LastName = 'Test Last Name']) {
                Account a = new Account(Id=c.AccountId);
                a.Achternaam__c = c.LastName;
                conAccs.add(a);
            }

            if (!conAccs.isEmpty()) {
                update(conAccs);
            }           
        }


    }
}

It won't let me do an input of data from sandbox. While the apex class works, it fails every test i run with it. It's like not funny :(..

3
  • 5
    It doesn't look like it from what you have shared but are you creating any test data in your class? I.e. Are there any Account records?
    – Girbot
    Commented Aug 1, 2016 at 14:32
  • are you creating any records(accounts) to run the tests ?
    – Anurag
    Commented Aug 1, 2016 at 14:57
  • The error you are receiving happens when you are attempting to assign the results of a SOQL query to an instance of an sObject rather than a List, but no results are being returned. Looking at your code, no such statement exists. Did you leave out any code? I copied the entire class into my IDE and line 29 shows up as a.Nieuwsbrieven_ontvangen__c = false; which couldn't have caused the issue.
    – dBeltowski
    Commented Aug 1, 2016 at 16:28

1 Answer 1

0

There are two ways to look at the test data.

  • You can access org data for testing using seeAllData annotation.

The syntax for this will be,

// All test methods in this class can access all data.
@isTest(SeeAllData=true)
public class TestDataAccessClass {
    // This test method can also access all data
    // because the containing class is annotated with @isTest(SeeAllData=true).
    @isTest static void myTestMethod1() {
        // Can access all data in the organization.
   }
}
  • The second way is to create test data in Test method itself and use that data for testing. This data never commits to the actual data base. It will be available only during the testing context.

For example:

  @isTest
public class TestDataAccessClass {
    // Create Data for testing
    @isTest static void myTestMethod1() {
        // Insert Account Record(s)
        Account acc1 = new Account(Name = 'Test Acc');
        insert acc1;
        Test.startTest();
        // Invoke controller methods for testing activity
        Test.stopTest();
        // Write some asssert statements for verify your tests

   }
}
2
  • 5
    Devendra -- the use cases for seeAllData=true are rare and should generally be strongly discouraged for OP
    – cropredy
    Commented Aug 1, 2016 at 16:10
  • @cropredy - Yes, you are right. As a best practise, we should always create a test data for Test methods, instead of accessing org data.
    – Devendra
    Commented Aug 1, 2016 at 16:12

You must log in to answer this question.

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