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
rplrpl 

Test class for a Class used only for the customer portal users

below is my class and the related test class that i have created for it.

if i run the code without using System.runas(test_portal_user) it gives me 61% coverage. i am trying to increase the coverage and so i created a test account for a test user and run the code as the test_portal_user.

 

public class Scp_Portal_Tab_Controller
{
    public integer offsetValue = 0;
    public integer LimitValue = 10;
    public integer recordCount =0;
    public boolean PortalUser{set; get;} 
    Service_Credit_Program__c  SCP=new Service_Credit_Program__c ();
    list<Service_Credit_Program__c> recList=new list<Service_Credit_Program__c>();
    User U=[select id,name,Email,Phone,city,PostalCode,Street,state,CompanyName,AccountId,IsPortalEnabled from User where id=:userinfo.getuserid()];
   
    public Scp_Portal_Tab_Controller() 
    {
        if(u.AccountId!=null)
        {
          account a =[select id, Name, Phone, billingStreet, billingCity, billingPostalCode, Total_SCP_Credits_del__c, Total_Man_Days__c from account where id=:U.AccountId];
                     
         /* SCP.address__C=a.billingcity;
          SCP.Telephone__c=A.Phone;
          SCP.Accounts__c= a.id; 
          SCP.ZIP_Code_P_O_Box__c=A.billingPostalCode;
          SCP.City__c=A.billingStreet; */
          SCP.Name__c= a.name;
          SCP.Total_Credits__c= a.Total_SCP_Credits_del__c;
          SCP.Total_Man_Days__c= a.Total_Man_Days__c;
          recordcount = [select count() from Service_Credit_Program__c where Accounts__c=:u.AccountId ];
          if(U.isPortalEnabled==true)
          {
            if(recordcount==0)
            {    
                PortalUser=false;
                ApexPages.Message error= new ApexPages.message(ApexPages.severity.info,'There are no Service Credit Program Requests Made');
                ApexPages.addMessage(error);  
            }
            else
            {
                PortalUser=true;  
            } 
          }           
       } 
        else
        {
            portaluser=false;
            apexpages.message err= new ApexPages.message(apexpages.severity.warning,'Access Service Credit Program from the respective Account of the Requester');
            apexpages.addmessage(err);
        }         
    }
 
    public Service_Credit_Program__c getSCP() 
    {
        return SCP;
    }
    
    public list<Service_Credit_Program__c> getrecList()
    {
        recList=[select id,name,Accounts__c,Credits__c,Man_Days__c,Service_Items__c,SCP_Type__c,Status__c,Created_Date__c from Service_Credit_Program__c where accounts__c=:U.accountid ORDER BY Created_Date__c DESC limit:limitValue offset:offsetValue]; 
        return (recList);
    }
    
    public pagereference next()
    {
        offsetValue += limitValue;
        return null;
    }    
    public pagereference previous()
    {
         offsetvalue -= limitvalue;
         return null;
    }    
    public boolean getisPreviousDisabled()
    {
         if(offsetvalue == 0)
         return true;
         else
         return false;
    }    
    public boolean getisNextDisabled()
    {
         if(offsetvalue + limitvalue >= recordCount)
         return true;
         else
         return false;
    }
}

 

below is the related test class with 21% coverage.

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

and

Stack Trace: 

Class.Scp_Portal_Tab_Controller.<init>: line 15, column 1
Class.Scp_Portal_Tab_Controller_TC.Scp_Portal_Tab_Controller: line 14, column 1

 

 

@isTest
public class Scp_Portal_Tab_Controller_TC
{   

static testMethod void Scp_Portal_Tab_Controller()

Profile p = [Select Id from Profile where name = 'NE Customer Profile'];
user testUser=new user();
testUser=[select id from user where isPortalEnabled=:true and profileid=:p.id limit 1];
  
account testAcc=new account(name='I_am_tester1',ownerid=testUser.id,billingcity='tester_city',Phone='123test789',Total_SCP_Credits_del__c=111,billingPostalCode='321',billingStreet='tester_street',Total_Man_Days__c=121);
insert(testAcc);
system.runas(testUser)
{
 Scp_Portal_Tab_Controller scp1= new Scp_Portal_Tab_Controller();
 Service_Credit_Program__c testSCP=new Service_Credit_Program__c(accounts__c=testAcc.id,man_days__c=5);
 insert(testSCP); 
 testSCP.Name__c= testAcc.name;
 testSCP.Total_Credits__c= testAcc.Total_SCP_Credits_del__c;
 testSCP.Total_Man_Days__c= testAcc.Total_Man_Days__c;
 }
 Scp_Portal_Tab_Controller scp= new Scp_Portal_Tab_Controller();
 scp.getSCP();
 scp.getrecList();
 scp.next();
 scp.previous();
 scp.getisPreviousDisabled();
 scp.getisNextDisabled();  

}

}

 

Please help me resolve this issue. Thanks!