You need to sign in to do that
Don't have an account?
roni shore
test class for community user
Hi - can anyone please help me with the test class for below, I have written the test class but it's not covering the else block
class
====
public class Comm_Locale {
public User ur = new User();
public String redirectUrl {get;set;}
public Comm_Locale(){
Map<String,global__c> cSetting_Map = Locale_on_Global__c.getAll();
ur = [Select id, Contact.Locale__c FROM User where Id =: UserInfo.getUserId()];
if(String.isNotBlank(ur.Contact.Locale__c)){
global__c cSettingList = cSetting_Map.get(ur.Contact.Locale__c);
if(String.isNotBlank(cSettingList.Account_URL__c)){
redirectUrl = cSettingList.Account_URL__c;
}else{
redirectUrl = 'https//:www.google.com';
}
}else{
// Need to assign default web link if required
redirectUrl = 'https//:www.google.com';
}
}
}
test class
======
@isTest
private class Comm_Locale_tetd{
static testMethod void CheckLocale() {
Global__c log = new Global__c(name = 'test',Account_URL__c = 'https://www.test.com');
insert log;
contact con = new contact(LastName = 'test name',Locale__c = 'en_US');
insert con;
Comm_Locale pbc = new Comm_Locale ();
}
class
====
public class Comm_Locale {
public User ur = new User();
public String redirectUrl {get;set;}
public Comm_Locale(){
Map<String,global__c> cSetting_Map = Locale_on_Global__c.getAll();
ur = [Select id, Contact.Locale__c FROM User where Id =: UserInfo.getUserId()];
if(String.isNotBlank(ur.Contact.Locale__c)){
global__c cSettingList = cSetting_Map.get(ur.Contact.Locale__c);
if(String.isNotBlank(cSettingList.Account_URL__c)){
redirectUrl = cSettingList.Account_URL__c;
}else{
redirectUrl = 'https//:www.google.com';
}
}else{
// Need to assign default web link if required
redirectUrl = 'https//:www.google.com';
}
}
}
test class
======
@isTest
private class Comm_Locale_tetd{
static testMethod void CheckLocale() {
Global__c log = new Global__c(name = 'test',Account_URL__c = 'https://www.test.com');
insert log;
contact con = new contact(LastName = 'test name',Locale__c = 'en_US');
insert con;
Comm_Locale pbc = new Comm_Locale ();
}
Try the below test Class:
@isTest
private class Comm_Locale_tetd{
@isTest static testMethod void CheckLocale() {
Global__c log = new Global__c(name = 'test',Account_URL__c = 'https://www.test.com');
insert log;
contact con = new contact(LastName = 'test name',Locale__c = 'en_US');
insert con;
Comm_Locale pbc = new Comm_Locale ();
}
@isTest static testMethod void CheckLocaleelse1() {
Global__c log = new Global__c(name = 'test',Account_URL__c = '');
insert log;
contact con = new contact(LastName = 'test name',Locale__c = 'en_US');
insert con;
Comm_Locale pbc = new Comm_Locale ();
}
@isTest static testMethod void CheckLocaleelse2() {
contact con = new contact(LastName = 'test name',Locale__c = '');
insert con;
Comm_Locale pbc = new Comm_Locale ();
}
}
Can you please Let me know if it helps or not!!!
If it helps don't forget to mark this as a best answer!!!
Thanks,
Raj
You are using 'UserInfo.getUserId()' this in your class which is always a current user so this if condition 'String.isNotBlank(ur.Contact.Locale__c)' always giving true thatswhy you are unable to cover else part.
You have to set user for this scenerio like:-
User user = new User(Alias='Jesse', Email='jesse.creech@ncino.com',
EmailEncodingKey='UTF-8', LastName='Creech',
LanguageLocaleKey='en-US', LocaleSidKey='en_US', TimeZoneSidKey='America/Los_Angeles');
contact con = new contact(LastName = 'test name',Locale__c = 'en_US');
user.Contact = con;
System.runAs(user){
Test.StartTest();
//call function here
Test.StopTest();
}
Thanks