function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Himanshu Patel 55Himanshu Patel 55 

Test class Failed: List has no rows for assignment to SObject

Hello Everyone,

My test class is failing and I can't figure out why. Any assistance is greatly appreciated.

This is the error message:

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

Method 1: System.QueryException: List has no rows for assignment to SObject 
Stack Trace: Class.portalRequestControllerTests.AAAAAAAAAAAAAAAtestInsert: line 126, column 1

Method 2: System.QueryException: List has no rows for assignment to SObject 
Stack Trace: Class.portalRequestControllerTests.testInsertContactNotMatched: line 155, column 1

Method 3: System.QueryException: List has no rows for assignment to SObject 
Stack Trace: Class.portalRequestControllerTests.testMisMatchedAccount: line 184, column 1

Method 4: System.AssertException: Assertion Failed: Same value: null 
Stack Trace: Class.portalRequestControllerTests.testVeterinarianSchool: line 24, column 1

Method 5: System.AssertException: Assertion Failed: Same value: null 
Stack Trace: Class.portalRequestControllerTests.test_method_one: line 46, column 1

This is the test class as a whole:

@isTest
private class portalRequestControllerTests {
  
  @isTest static void testVeterinarianSchool() {
    portalRequestController ctlr     = new portalRequestController(new ApexPages.StandardController(new Portal_Request__c() ));
    RecordType rt             = [select Id from RecordType where SObjectType = 'Account' and Name  = 'Veterinarian School'];

      Account a    = new Account(Name='Test Account', RecordTypeId = rt.Id, Active__c = true, Account_Classification__c = 'School');
      insert a;


    List<Account> searchList       = portalRequestController.lookupAccount('Test Account');
    System.assertEquals(searchList.size(), 1);

      ctlr.pr.First_Name__c       = 'Joe';
      ctlr.pr.Last_Name__c       = 'Test';
      ctlr.pr.Email_Address__c     = 'test@mailinator.com';
      ctlr.clinicId           = portalRequestController.lookupAccount('Test Account')[0].Id;
      ctlr.pr.Clinic_Phone_Number__c   = '416 555 5555';
      ctlr.pr.Title__c         = 'Tester';
      ctlr.lang             = ctlr.getLanguageOptions()[0].getValue();

      ctlr.createRequest();
      System.assertNotEquals(ctlr.pr.Id, null);

  }


  @isTest static void test_method_one() {
    portalRequestController ctlr     = new portalRequestController(new ApexPages.StandardController(new Portal_Request__c() ));

        Account a    = new Account(Name='Test Account', RecordTypeId = slimfitUtils.getClinicAccountRecordType().Id, Active__c = true, Account_Classification__c = 'School');
        insert a;

      ctlr.createRequest();

      ctlr.pr.First_Name__c       = 'Joe';
      ctlr.pr.Last_Name__c       = 'Test';
      ctlr.pr.Email_Address__c     = 'test@mailinator.com';
      ctlr.clinicId           = portalRequestController.lookupAccount('Test Account')[0].Id;
      ctlr.pr.Clinic_Phone_Number__c   = '416 555 5555';
      ctlr.pr.Title__c         = 'Tester';
      ctlr.lang             = ctlr.getLanguageOptions()[0].getValue();

      ctlr.createRequest();
      System.assertNotEquals(ctlr.pr.Id, null);
  }
  

  @isTest static void test_rejection() {
    portalRequestController ctlr     = new portalRequestController(new ApexPages.StandardController(new Portal_Request__c() ));

      ctlr.createRequest();

      ctlr.pr.First_Name__c       = 'Joe';
      ctlr.pr.Last_Name__c       = 'Test';
      ctlr.pr.Email_Address__c     = 'test@mailinator.com';
      ctlr.pr.Clinic_Phone_Number__c   = '416 555 5555';
      ctlr.pr.Title__c         = 'Tester';

      ctlr.createRequest();
      System.assertEquals(ctlr.pr.Id, null);
  }



  @isTest static void testExistingPortalRequest() {
      Account a    = new Account(Name='Test Account');
      insert a;

        Portal_Request__c portalReqObj1           = new Portal_Request__c ();
        portalReqObj1.First_Name__c   = 'TestFirstName1';
        portalReqObj1.Last_Name__c     = 'TestLastName';
        portalReqObj1.Status__c        = 'New';
        portalReqObj1.Clinic__c       = a.id;
        portalReqObj1.Email_Address__c   = 'TestMail@testing.com';
        insert portalReqObj1;

    portalRequestController ctlr     = new portalRequestController(new ApexPages.StandardController(new Portal_Request__c() ));

      ctlr.pr.First_Name__c       = 'Joe';
      ctlr.pr.Last_Name__c       = 'Test';
      ctlr.pr.Email_Address__c     = portalReqObj1.Email_Address__c;
      ctlr.pr.Clinic_Phone_Number__c   = '416 555 5555';
      ctlr.pr.Title__c         = 'Tester';

      ctlr.createRequest();
      System.assertEquals(ctlr.pr.Id, null);
  }



    static testMethod void AAAAAAAAAAAAAAAtestInsert() {
    // prod
    ExactTarget_Settings__c settings = new ExactTarget_Settings__c(
      accessToken__c='bg5ccutef4qqcdxkjk63bd2u', 
      accessToken_Expiration_Date__c = Datetime.now().addYears(1),
      TriggeredSendEmail_Endpoint__c = 'https://www-exacttargetapis-com-nhz9abyp4su1.runscope.net/messaging/v1/messageDefinitionSends/key:%7B0%7D/send?access_token={1}',
      requestToken_EndPoint__c = 'https://auth.exacttargetapis.com/v1/requestToken',
      clientSecret__c = 'k6fs4dkvJr6XC3Uzw4yxVrYT',
      clientId__c = 'xdf5ehwugdv5afpybxrda9uq'
    );
    // dev
    //TMSSettings__c settings = new TMSSettings__c(Blocked_Off_Record_Type_ID__c=blackoutRecordTypeId, Shipment_Record_Type_ID__c=shipmentRecordTypeId, Carrier_New_Ship_Email_Template_ID__c='00XW0000000I8juMAC', Carrier_Profile_ID__c='00eW0000000QMpFIAW', Carrier_Updated_Ship_Email_Template_ID__c='00XW0000000I8jpMAC', From_Email_Notification_ID__c='0D250000000TN1PCAW', Portal_Domain_Name__c='  royalcanincanada.force.com', TMS_RC_Notification_Group_ID__c='00G50000001jledEAA', Warehouse_Address__c='Any street, Toronto', Warehouse_Phone__c='9056667890', Warehouse_Profile_ID__c='01IW0000000D4fx' , POD_Logistics_Email_Template_ID__c='00XW0000000I9xxMAC', POD_Carrier_Email_Template_ID__c='00XW0000000I9xsMAC');
    insert settings;

      Account a    = new Account(Name='Test Account');
      insert a;
        Account a1    = [select Id, OwnerId from Account where Id = :a.Id];

        Contact c    = new Contact(FirstName = 'first', LastName = 'last', Email = 'test@me.com', AccountId = a.Id, RecordTypeId = slimfitUtils.getClinicVetRecordType().Id, Vet_Portal_Welcome_Sent__c = true);
        insert c;
        
    portalRequestController ctlr     = new portalRequestController(new ApexPages.StandardController(new Portal_Request__c() ));

      ctlr.pr.First_Name__c       = 'Arthur';
      ctlr.pr.Last_Name__c       = c.LastName;
      ctlr.pr.Email_Address__c     = c.Email;
      ctlr.ClinicId           = a.Id;
      ctlr.pr.Clinic_Phone_Number__c   = '416 555 5555';
      ctlr.pr.Title__c         = 'Tester';
      ctlr.lang             = 'en_US';
      ctlr.pr.Email_Opt_In__c     = true;
      ctlr.createRequest();

    Portal_Request__c pr         = [select Id, Status__c, Clinic__c from Portal_Request__c where Id = :ctlr.pr.Id];
      //System.assertEquals(pr.Status__c, 'Approved');
      System.assertEquals(pr.Clinic__c, a.Id);

    Contact cCheck     = [select Id, FirstName, Vet_Express_Consent__c from Contact where Id = :c.Id];
    System.assertEquals(cCheck.FirstName, ctlr.pr.First_Name__c);
    System.assertEquals(cCheck.Vet_Express_Consent__c, true);
    }


    static testMethod void testInsertContactNotMatched() {
      Account a    = new Account(Name='Test Account');
      insert a;
        Account a1    = [select Id, OwnerId from Account where Id = :a.Id];

        Contact c    = new Contact(FirstName = 'first', LastName = 'last', Email = 'test@me.com', AccountId = a.Id);
        insert c;
        
    portalRequestController ctlr     = new portalRequestController(new ApexPages.StandardController(new Portal_Request__c() ));
      ctlr.pr.First_Name__c       = 'Arthur';
      ctlr.pr.Last_Name__c       = c.LastName;
      ctlr.pr.Email_Address__c     = c.Email;
      ctlr.ClinicId           = a.Id;
      ctlr.pr.Clinic_Phone_Number__c   = '416 555 5555';
      ctlr.pr.Title__c         = 'Tester';
      ctlr.lang             = 'en_US';
      ctlr.pr.Email_Opt_In__c     = true;
      ctlr.createRequest();

    Portal_Request__c pr         = [select Id, Status__c, Contact__c, Clinic__c from Portal_Request__c where Id = :ctlr.pr.Id];
      System.assertEquals(pr.Contact__c, null);
      System.assertEquals(pr.Clinic__c, a.Id);

    Contact cCheck     = [select Id, FirstName, Vet_Express_Consent__c from Contact where Id = :c.Id];
    System.assertNotEquals(cCheck.FirstName, ctlr.pr.First_Name__c);
    System.assertNotEquals(cCheck.Vet_Express_Consent__c, true);
    }


    static testMethod void testMisMatchedAccount() {
      Account a    = new Account(Name='Test Account');
      Account b     = new Account(Name='B Account');
      insert new List<Account> { a, b };

        Contact c    = new Contact(FirstName = 'first', LastName = 'last', Email = 'test@me.com', AccountId = b.Id);
        insert c;
        
    portalRequestController ctlr     = new portalRequestController(new ApexPages.StandardController(new Portal_Request__c() ));
      ctlr.pr.First_Name__c       = 'Arthur';
      ctlr.pr.Last_Name__c       = c.LastName;
      ctlr.pr.Email_Address__c     = c.Email;
      ctlr.ClinicId           = a.Id;
      ctlr.pr.Clinic_Phone_Number__c   = '416 555 5555';
      ctlr.pr.Title__c         = 'Tester';
      ctlr.lang             = 'en_US';
      ctlr.pr.Email_Opt_In__c     = true;
      ctlr.createRequest();

    Portal_Request__c pr         = [select Id, Status__c, Clinic__c from Portal_Request__c where Id = :ctlr.pr.Id];
      //System.assertEquals(pr.Status__c, 'Approved');
      System.assertEquals(pr.Clinic__c, a.Id);
      System.assertNotEquals(pr.Status__c, 'Approved');

    List<User> uList           = [select Id from User where Email = :c.Email];
    System.assertEquals(uList.size(), 0);

    List<Account> searchList       = portalRequestController.lookupAccount('Account');
    System.assertEquals(searchList.size(), 0);
    }}

I have highlighted error 
MandyKoolMandyKool
Seems like "createRequest()" method is failing somewhere in your "portalRequestController" controller.
You can enable the logs and see what's the issue in createRequest. Or post your findings so that someone can help you.