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
pdostapdosta 

Assistance with Controller Test

Can anyone give a suggestion on the below test.  When running the test, it does not return any rows and does not give me adequate coverage.

 

Controller

 

public class psuIdRequestController {
 private String requestid;
    public Participant_ID_Request__c participantmgr;
    public String strStatus;
    public User user;
    public Contact contactBusiness;
    public Contact contactTechnical;
    public Contact contactFinance;
    public Contact contactTreasury;
    public String comment;
    public String approver;
    public Date approvalDate;
    
    
    public psuIdRequestController(ApexPages.StandardController stdController) {
        requestid = ApexPages.currentPage().getparameters().get('requestid');

        //Fetch participant for Participant ID Request record.
        participantmgr = [select Id, Name, DCF_Approval_Due_Date__c,
                        Business_DCF_Approver__c, Technical_DCF_Approver__c, Finance_DCF_Approver__c, Treasury_DCF_Approver__c,
                        Business_DCF_Approval__c, Technical_DCF_Approval__c, Finance_DCF_Approval__c, Treasury_DCF_Approval__c,
                        Business_DCF_Approval_Date__c, Technical_DCF_Approval_Date__c, Finance_DCF_Approval_Date__c, Treasury_DCF_Approval_Date__c,
                        Business_DCF_Comments__c, Technical_DCF_Comments__c, Finance_DCF_Comments__c, Treasury_DCF_Comments__c      
                        from Participant_ID_Request__c where Id =:requestid limit 1];

     try{
        contactBusiness = [select Id, Name from Contact where Id =: participantmgr.Business_DCF_Approver__c limit 1];
        contactTechnical = [select Id, Name from Contact where Id =: participantmgr.Technical_DCF_Approver__c limit 1];
        contactFinance = [select Id, Name from Contact where Id =: participantmgr.Finance_DCF_Approver__c limit 1];
        contactTreasury = [select Id, Name from Contact where Id =: participantmgr.Treasury_DCF_Approver__c limit 1];
        
        }
    catch(QueryException e){
     // System.assert(false,'You must be assigned as an approver for this participant.');    
        
        }
    
    }
  
    public Participant_ID_Request__c getparticipant(){
        return participantmgr;
    }
    
    public void Approve(){
        setStatus('Approved');      
    }
    public void Reject(){
        setStatus('Rejected');
    }
    public String getUserName(){
        return UserInfo.getName();
    }
    public String getStatus(){ 
        if(UserInfo.getName() == contactBusiness.Name ){
                return participantmgr.Business_DCF_Approval__c;
            }
            else if (UserInfo.getName() == contactTechnical.Name ){
                return participantmgr.Technical_DCF_Approval__c;
            }
            else if (UserInfo.getName() == contactFinance.Name ){
                return participantmgr.Finance_DCF_Approval__c;
            }
            else if (participantmgr.Treasury_DCF_Approver__c <> Null ){
                if (UserInfo.getName() == contactTreasury.Name ){
                    return participantmgr.Treasury_DCF_Approval__c;
                }
            }
        return strStatus;
    }
    public void setStatus(String status){
    
        if(UserInfo.getName() == contactBusiness.Name ){
            participantmgr.Business_DCF_Approval__c = status;
            participantmgr.Business_DCF_Comments__c = comment;
            participantmgr.Business_DCF_Approval_Date__c = approvalDate;
            update participantmgr;
        }
        else if (UserInfo.getName() == contactTechnical.Name ){
            participantmgr.Technical_DCF_Approval__c = status;
            participantmgr.Technical_DCF_Comments__c = comment;
            participantmgr.Technical_DCF_Approval_Date__c = approvalDate;
            update participantmgr;
        }
        else if (UserInfo.getName() == contactFinance.Name ){
            participantmgr.Finance_DCF_Approval__c = status;
            participantmgr.Finance_DCF_Comments__c = comment;
            participantmgr.Finance_DCF_Approval_Date__c = approvalDate;
            update participantmgr;
        }
        else if (participantmgr.Treasury_DCF_Approver__c <> Null ){
            if (UserInfo.getName() == contactTreasury.Name ){
            participantmgr.Treasury_DCF_Approval__c = status;
            participantmgr.Treasury_DCF_Comments__c = comment;
            participantmgr.Treasury_DCF_Approval_Date__c = approvalDate;
            update participantmgr;
            }
        }
    }
            
    public String getParticipantName() {
        return participantmgr.Name;       
    }
    
    public String getComment(){
    
            if(UserInfo.getName() == contactBusiness.Name ){
                return participantmgr.Business_DCF_Comments__c ;
            }
            else if (UserInfo.getName() == contactTechnical.Name ){
                return participantmgr.Technical_DCF_Comments__c;
            }
            else if (UserInfo.getName() == contactFinance.Name ){
                return participantmgr.Finance_DCF_Comments__c;
            }
            else if (participantmgr.Treasury_DCF_Approver__c <> Null ){
                if (UserInfo.getName() == contactTreasury.Name ){
                    return participantmgr.Treasury_DCF_Comments__c;
                }
                else{
                    comment = '';
                    return comment; 
                }
            }
            else{
                comment = '';
                return comment; 
            }
    }
    
    public void setComment(String value){
        comment = value;        
    }
    
    public Date getapprovalDate(){
    
            if(UserInfo.getName() == contactBusiness.Name ){
                return participantmgr.Business_DCF_Approval_Date__c;
            }
            else if (UserInfo.getName() == contactTechnical.Name ){
                return participantmgr.Technical_DCF_Approval_Date__c;
            }
            else if (UserInfo.getName() == contactFinance.Name ){
                return participantmgr.Finance_DCF_Approval_Date__c;
            }
            else if (participantmgr.Treasury_DCF_Approver__c <> Null ){
                if (UserInfo.getName() == contactTreasury.Name ){
                    return participantmgr.Treasury_DCF_Approval_Date__c;
                }
                else{
                    approvalDate = null;
                    return approvalDate; 
                }
            }
            else{
                approvalDate = null;
                return approvalDate;
            } 
    }
    
    public String getApprover(){
            if(UserInfo.getName() == contactBusiness.Name ){
                return contactBusiness.Name;
            }
            else if (UserInfo.getName() == contactTechnical.Name ){
                return contactTechnical.Name;
            }
            else if (UserInfo.getName() == contactFinance.Name ){
                return contactFinance.Name;
            }
            else if (participantmgr.Treasury_DCF_Approver__c <> Null ){
                if (UserInfo.getName() == contactTreasury.Name ){
                    return contactTreasury.Name;
                }
                else{
                approver = 'Not Eligilble to approve';
                return approver; 
                }
            }
            else{
                approver = 'Not Eligilble to approve';
                return approver; 
            }
    }

}

 Test

 

    static testmethod void testPsuIdRequest()
    {
        User BusinessUser = [select id,name,ContactId from User where Profile.Name = 'PORTAL: GNS Technical Account Management' and IsActive = true limit 1];
        User TechnicalUser = [select id,name,ContactId from User where Profile.Name = 'PORTAL: GNS Partner Operations' and IsActive = true limit 1];
        User FinanceUser = [select id,name,ContactId from User where Profile.Name = 'PORTAL: GNS Finance' and IsActive = true limit 1];
        User InvalidUser = [select id,name,ContactId from User where Profile.Name = 'PORTAL: AMEX Treasury' and IsActive = true limit 1];

        System.RunAs(BusinessUser){        
        RecordType recreq = [select ID,Name from RecordType where Name = 'ID REQUEST: Draft Approved' and SObjectType = 'Participant_ID_Request__c'];
        Participant_ID_Request__c preq = new Participant_ID_Request__c(recordtypeid=recreq.Id,
        Name='Test Deal1'); 
        insert preq;
        

            ApexPages.PageReference testpage = Page.psudcfreview;
            ApexPages.StandardController con = new ApexPages.StandardController(preq);
            psuIdRequestController pnid = new psuIdRequestController(con);
            Test.setCurrentPage(testpage);
            ApexPages.currentPage().getParameters().get('requestid');
    
            pnid = new psuIdRequestController(con);
            pnid.comment = 'testing';
            pnid.approve();
            }
        }

 

 

 

vanessenvanessen

It is more adequate to  create the objects instance Account,Partner,Finance and treasury...you can avoid create a user.

 

 

and to get more coverage..you can explicitly call your method individually after the initialisation of the class :

 

 

            psuIdRequestController pnid = new psuIdRequestController(con);
            pnid.comment = 'testing';
             pnid.approve();
 pnid.getparticipant()
 pnid.getUserName()
 pnid.getComment()
 pnid.getapprovalDate()
 pnid.getApprover()
      

Maybe you will have to see whcih to call first.It is not necessary to catch the values return.

another things that i didn't see u calling or using : the big method setStatus. Where do you call it in your methods????

 

* call the method by .getComment and not by .Comment as in VF page...ok

 

Melvin_DeveloperMelvin_Developer

Please don't avoid setting up users etc...
If you do your test method will get coverage but for future modification to the org you cannot then count on your test method to properly recognize any issues that could arise.
Test methods are very important to prove the validity of your code aswell as making updates and modifications easier to manage.

I have created some sample code below that should help, be aware it may need a little modification to work with your page/class.

Since you are passing in a standard controller I assume that your page is using this class as an extension,
in which case your test method should look something like this:

 

 

static testmethod void testPsuIdRequest()
{
	//create instance of object the standard controller is for
	Object__c testObj = new Object__c (Name = 'Test Obj'); //replace with creation of your object
	insert testObj;

	//setup user stuff
	Profile p = [select id from profile where name='Standard User']; //use your profile here
	User u = new User(name = 'stringName', profileid = p.Id); //add any other required fields

        System.RunAs(u){        

            PageReference testpage = Page.psudcfreview;
	    testpage.getParameters().put('requestid', testObj.Id);
	    Test.setCurrentPage(testpage);
		
	    // Custom Controller
	    ApexPages.StandardController con = new ApexPages.StandardController(testObj);
					
	    // extension
	    psuIdRequestController pnid = new psuIdRequestController(con);
	
            pnid.approve();
	    System.assertEquals('Approved', pnid.getStatus());


            
        }
}

 Please not the use of asserts, these are very important within testmethods and will also help you resolve any issues you encounter whilst writing your test method.

 

Hope this helps,
                             Melvin!

 

pdostapdosta

Thanks Melvin, this is much cleaner, but I still run into trhe same issue where the test fails because it cannot find the record for the requestid.  I get the following error: "System.QueryException: List has no rows for assignment to SObject" and it points to not being able to pull a row for this part of the code:

 

        participantmgr = [select Id, Name, DCF_Approval_Due_Date__c,
                        Business_DCF_Approver__c, Technical_DCF_Approver__c, Finance_DCF_Approver__c, Treasury_DCF_Approver__c,
                        Business_DCF_Approval__c, Technical_DCF_Approval__c, Finance_DCF_Approval__c, Treasury_DCF_Approval__c,
                        Business_DCF_Approval_Date__c, Technical_DCF_Approval_Date__c, Finance_DCF_Approval_Date__c, Treasury_DCF_Approval_Date__c,
                        Business_DCF_Comments__c, Technical_DCF_Comments__c, Finance_DCF_Comments__c, Treasury_DCF_Comments__c      
                        from Participant_ID_Request__c where Id =:requestid limit 1];

 I cannot figure out why it cannot find the record I created.  Any thoughts?

Melvin_DeveloperMelvin_Developer

sorry, this was not an issue with the test method but with the class itself so I ignored it.
As far as im aware a select query will return a list even with limit 1 applied (limit 1 simply sets the maximum size of the list returned to 1)

try something like this:

 

 

List<Participant_ID_Request__c> objList = select[];

//ensure that a value has been returned
if(objList.size() > 0){
//set value
participantmgr = obj[0];
}else {
//error
}

 

 

Hope this helps,
                           Melvin!

 

 

pdostapdosta

Will it matter if I change the query.  I am currently passing the "requestid" variable from a Custom Button.  wil that make a difference in how I build the test?

Melvin_DeveloperMelvin_Developer

No, if you look at the sample test method I provided, you will see that I have passed the requestid into the page param before assigning the page for use in the test. This mimics the way you would access the page through a button click passing the id param. If you were to change the query this would still work, however if the use of the id param becomes redundent you will simply need to remove the add param line of code form the test method.

Hope this answers your question,
                       Melvin!