-1

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.When I enter the user details through community a contact is created corresponding to the user . I am writing he code below.Thanks in advance

Public with sharing class WelcomePageController {
    //Public Id runningUserid{get;set;}
    Public WelcomePageController (){

    }

    Public String getCustomerAddressId() {
        Id runningUserid=userinfo.getUserid();
        User user=[SELECT ID,ContactId FROM User WHERE IsActive=True AND ID=:runningUserid];//005P0000001bEjB
        //User user=[SELECT ID,ContactId FROM User WHERE IsActive=True AND ID='005P0000001bEjB'];//005P0000001bEjB
        system.debug('user@@@@@'+user);

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

        return contact.Customer_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.Id=runningUserid;
        //user.UserRole=;
        insert user;        

        Customer_Address__c  customer = new Customer_Address__c ();
        customer.Customer__c = contact.id;
        //customer.Customer_Address_ID__c  = 'LB!23f';
        insert customer;

        WelcomePageController welcomePageController = new WelcomePageController();
        //welcomePageController.runningUserid='005P0000001bEjB';
        WelcomePageController.finishRegistration();
        WelcomePageController.getCustomerAddressId();

    }
}
2
  • 2
    you need to use System.runas(user) after inserting user in your test class
    – Himanshu
    May 10, 2017 at 7:30
  • 2
    You need to run the code as System.runAs(user) the user which u r inserting in the test class so that in the code runningUserId is set as the testUser.
    – RCS
    May 10, 2017 at 7:31

1 Answer 1

2

Try with below code

@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.Id=runningUserid;
    //user.UserRole=;
    insert user;        

    Customer_Address__c  customer = new Customer_Address__c ();
    customer.Customer__c = contact.id;
    //customer.Customer_Address_ID__c  = 'LB!23f';
    insert customer;

    system.runAs() {
       WelcomePageController welcomePageController = new WelcomePageController();
      //welcomePageController.runningUserid='005P0000001bEjB';
       WelcomePageController.finishRegistration();
       WelcomePageController.getCustomerAddressId();
    }

}
}
0

You must log in to answer this question.

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