I have a controller where I get a token from a visualforce page with account as a standard controller and controller extension. In my unittest I insert the variables but the accid throws an exception because because of an empty soql. I already have enough code coverage but like to know what I am missing

USER_DEBUG [21]|DEBUG|ev: (EscrowValidator__mdt:{Username__c=Test, Pass__c=123, Acces_granted__c=false, Token__c=123456, Id=m000U00000002tNQAQ})

USER_DEBUG [25]|DEBUG|pageRef: System.PageReference[/apex/escrowlink?token=123456]

DEBUG|An exception accId occurred: List has no rows for assignment to SObject


public without sharing class Escrowlink{
    public List<ContentDistribution> urlsContentDistribution {get;set;}
    public map<string,string> urlmap{get;set;}
    public Id accId;
    public Escrowlink(Apexpages.StandardController con){
                urlsContentDistribution = new List<ContentDistribution>();
                urlmap = new map<string,string>();      
            string token = apexpages.currentpage().getparameters().get('token');
            accId  = [SELECT id , Token__c FROM Account WHERE Token__c =:token LIMIT 1].id;
        catch(Exception e){
            System.debug('An exception accId occurred: ' + e.getMessage());
           urlsContentDistribution = [SELECT DistributionPublicUrl, ContentDownloadURL ,name FROM ContentDistribution WHERE RelatedRecordId = :accId];
            for(ContentDistribution m:urlsContentDistribution){
        catch(Exception e){
            System.debug('An exception idsContentdocument occurred : ' + e.getMessage());


public class EscrowLinkTest {
    static testmethod void escrowlinktest(){  
        EscrowLogin.testCase = 'Unittest1';
        Account acc = new Account(Name='accounttest',Token__c='123456');
        insert acc;

        ContentVersion cv=new Contentversion();
        cv.PathOnClient ='test';
        Blob b=Blob.valueOf('Unit Test Attachment Body');
        cv.versiondata=EncodingUtil.base64Decode('Unit Test Attachment Body');
        insert cv;

        ContentDistribution contentdist = new ContentDistribution(Name='contentdist',RelatedRecordId = acc.id, ContentVersionId= cv.id);
        insert contentdist;              

        List<EscrowValidator__mdt> ev = [SELECT Username__c , Pass__c, Acces_granted__c, Token__c 
                                         FROM EscrowValidator__mdt 
                                         WHERE Unittest__c = :EscrowLogin.testCase];
        system.debug('ev: '+ev);

        PageReference pageRef = Page.EscrowLink;
        system.debug('pageRef: '+pageRef);

        Apexpages.StandardController sc = new Apexpages.standardController(acc);
        EscrowLink ext = new EscrowLink(sc);
        system.debug('sc: '+sc);

        System.assertEquals(ext.urlmap.get(contentdist.DistributionPublicUrl), contentdist.DistributionPublicUrl);        
  • 1
    Never, ever use an empty catch block. It is one of worst anti-patterns you could implement. If your catch block does nothing you can assert against, the try/catch should be removed entirely. – Adrian Larson May 19 '19 at 21:20

In your test class, you're setting PageReference to a variable, this does not adds the parameter to Page's URL in testing mode.

A small step you're missing is to set the PageReference with current page using Test.setCurrentPage:

PageReference pageRef = Page.EscrowLink;
// set current page

// continue testing
Apexpages.StandardController sc = new Apexpages.standardController(acc);
  • Thanks didnt know about the setCurrentPage(). – Thomas May 19 '19 at 14:56
  • We learn something new everyday :) – Raul May 19 '19 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.