I am writing a unittest for a pagereference where I add some variables I currently get only 47% coverage. How do I pass the variables correctly.
I currently get the error: System.NullPointerException: Attempt to de-reference a null object
Apex:
public with sharing class EscrowLogin {
public string username{get;set;}
public string password{get;set;}
public PageReference retURL{get;set;}
List<EscrowValidator__mdt> ev = [SELECT Username__c , Pass__c, Acces_granted__c, Token__c FROM EscrowValidator__mdt];
public PageReference validateCredentials() {
map<string,string> tokens = new map<string,string>();
for(integer x=0;x<ev.size();x++){
tokens.put(ev[x].Username__c,ev[x].Token__c);
}
for(integer x=0;x<ev.size();x++){
if(username.equals(ev[x].Username__c) && password.equals(ev[x].Pass__c)){
PageReference retURL = new PageReference('/apex/escrowlink');
retURL.getParameters().put('token', tokens.get(ev[x].Username__c));
system.debug('tokens.get(ev[x].Token__c): '+ tokens.get(ev[x].Username__c));
retURL.setRedirect(true);
return retURL;
}
if(!username.equals(ev[x].Username__c) && !password.equals(ev[x].Pass__c) && ev.size() == x+1) {
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Login Error'));
}
}
return null;
}
}
Unittest
@istest
public class EscrowloginTest {
public string username{get;set;}
static testmethod void escrow(){
Account acct = new Account(Name='testAccount');
insert acct;
EscrowValidator__mdt ev = new EscrowValidator__mdt(Username__c='test',Pass__c='pass',Token__c='123456');
PageReference pageRef = Page.EscrowLogin;
pageRef.getParameters().put('token',ev.Token__c);
pageRef.getParameters().put('username',ev.Username__c);
pageRef.getParameters().put('password',ev.Pass__c);
system.debug('pageRef: '+pageRef);
EscrowLogin controller = new EscrowLogin();
controller.validateCredentials();
System.assertEquals(controller.validateCredentials(), pageRef);
}
}