0

I need to create a test class for the below mentioned class so far I am getting the error "List has no rows for assignment ".Here customer address is an object whose parent is contact

Public with sharing class WelcomePageController {

    Public WelcomePageController (){

    }

    Public String getCustomerAddressId() {
        Id runningUserid=userinfo.getUserid();

        User user=[SELECT ID,ContactId FROM User WHERE IsActive=True AND ID=:runningUserid];
        system.debug('user@@@@@'+user);

        Contact contact=[SELECT ID FROM Contact WHERE ID=:user.ContactId ];
        system.debug('contact@@@@@'+contact);

        Customer_Address__c customerAddress=[SELECT ID,Customer__c,Customer_Address_ID__c FROM Customer_Address__c WHERE Customer__c =:contact.Id ];
        system.debug('customerAddress@@@@@'+customerAddress);

        return customerAddress.Customer_Address_ID__c ;
    }

   Public PageReference finishRegistration() {
       PageReference page = new PageReference('/apex/Dashboard');
       page.setredirect(true);
       return page;
   }    
}

Test Class

@isTest(seeAlldata=False)
    public class WelcomePageController_Test {

    @isTest
    public static void testWelcomePageController(){
        Profile profile = [SELECT Id,Name FROM Profile WHERE Name='Customer Community Login User']; 

        Account account = new Account(Name='Test Account Name');
        insert account;

        Contact contact=new Contact();
        contact.LastName='Test';
        contact.AccountId=account.Id;
        insert contact;

        User user=new User();
        user.Email='[email protected]';
        user.Username='[email protected]';
        user.LastName='Test';
        user.Alias='testf';
        user.CommunityNickname='[email protected]';
        user.TimeZoneSidKey='America/Los_Angeles';
        user.EmailEncodingKey='UTF-8';
        user.LocaleSidKey='en_US';
        user.ProfileId=profile.Id;
        user.LanguageLocaleKey='en_US';
        user.ContactId=contact.id;
        user.IsActive = true;
        //user.UserRole=;
        insert user;
        WelcomePageController welcomePageController = new WelcomePageController();
        //welcomePageController.runningUserid='005P0000001bEjB';
        WelcomePageController.finishRegistration();
        WelcomePageController.getCustomerAddressId();

    }
}

1 Answer 1

1

Since Contact is parent for Customer_Address__c Object, there might be case that one Contact can have multiple Customer_Address__c Object.

So replace this line:

Customer_Address__c customerAddress=[SELECT ID,Customer__c,Customer_Address_ID__c FROM Customer_Address__c WHERE Customer__c =:contact.Id ];

With:

List<Customer_Address__c> customerAddressList=[SELECT ID,Customer__c,Customer_Address_ID__c FROM Customer_Address__c WHERE Customer__c =:contact.Id ];

Also in your test class you're not creating customer_address__c data. That's why you're getting that error. Add below lines after inserting contact in your test class:

Customer_Address__c  customer = new Customer_Address__c ();
customer.Customer__c = contact.id;
customer.Customer_Address_ID__c  = 'customer Id';
insert customer;
8
  • I made the required changes but still I am getting the sameerror May 8, 2017 at 9:10
  • @SaswataDas on which line you're getting an error.? May 8, 2017 at 9:11
  • I am getting the error in this line WelcomePageController.getCustomerAddressId(); May 8, 2017 at 9:13
  • Are you returning like this: return customerAddressList[0].Customer_Address_ID__c ; because now it would a list . Also since you've made some changes. Post your code. May 8, 2017 at 9:16
  • 1
    @SaswataDas Before calling getCustomerAddressId() from test class, add System.runAs(user) since you're retrieving current logged in user in your controller. May 10, 2017 at 9:00

You must log in to answer this question.

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