You need to sign in to do that
Don't have an account?

need some help in writing Test classes
Hi friends
Plz help me out in writing test class for the below class
public class EF_UpdateOKTA
{
public EF_UpdateOKTA()
{
UpdateEFKotaQueue();
}
public void UpdateEFKotaQueue()
{
List<string> lstEF_OKTA_QueueID = new List<string>();
/*string strQuery = EF_SOQL_Statements__c.getValues('EF_OKTA_Queue_Batch_Query').Type__c + ' ' + EF_SOQL_Statements__c.getValues('EF_OKTA_Queue_Batch_Query').Fields__c +
EF_SOQL_Statements__c.getValues('EF_OKTA_Queue_Batch_Query').Relationship_fields__c + ' from ' + EF_SOQL_Statements__c.getValues('EF_OKTA_Queue_Batch_Query').Object__c +
' where ' + EF_SOQL_Statements__c.getValues('EF_OKTA_Queue_Batch_Query').Filter__c;
system.debug('strQuery strQuery strQuery ::' + strQuery );
List <EF_OKTA_Queue__c> lstEFOKTA_Queue_QueryData = Database.Query(strQuery);*/
List <EF_OKTA_Queue__c> lstEFOKTA_Queue_QueryData = [SELECT Id, status__c, EF_Requested_Resource__c, OKTA_JSON__c,OKTA_Response__c, number_tried__c, EF_Requested_Resource__r.EF_Resource_Metadata__r.OKTA_App_ID__c, EF_Requested_Resource__r.EF_Request_ID__r.User_Name__r.Okta_Id__c, EF_Requested_Resource__r.Request_Type__c from EF_OKTA_Queue__c where status__c='Request Sent' and number_tried__c<10 limit 10];
system.debug('lstEFOKTA_Queue_QueryDatalstEFOKTA_Queue_QueryData::' + lstEFOKTA_Queue_QueryData);
for(EF_OKTA_Queue__c objEF_OKTA_Queue_Local : lstEFOKTA_Queue_QueryData)
{
lstEF_OKTA_QueueID.add(objEF_OKTA_Queue_Local.Id);
}
if(lstEF_OKTA_QueueID.size()>0)
{
list<EF_OKTA_Queue__c> lstEfOKtatoUpdate = new list<EF_OKTA_Queue__c>();
for(EF_OKTA_Queue__c oQueue: [select id, EF_Requested_Resource__r.EF_Request_ID__r.Okta_Id__c,
EF_Requested_Resource__r.EF_Request_ID__r.User_Name__r.Okta_Id__c,
OKTA_JSON__c, number_tried__c, EF_Requested_Resource__r.Request_Type__c,
EF_Requested_Resource__r.EF_Resource_Metadata__r.OKTA_App_ID__c, Status__c
from EF_OKTA_Queue__c
where id in : lstEF_OKTA_QueueID])
{
string userID = oQueue.EF_Requested_Resource__r.EF_Request_ID__r.Okta_Id__c!=''?
oQueue.EF_Requested_Resource__r.EF_Request_ID__r.Okta_Id__c:oQueue.EF_Requested_Resource__r.EF_Request_ID__r.User_Name__r.Okta_Id__c;
string appID = oQueue.EF_Requested_Resource__r.EF_Resource_Metadata__r.OKTA_App_ID__c;
string strURLEnd = OKTA_EndPoint__c.getValues('OKTA').Site_URL__c;
strURLEnd = strURLEnd + '/apps/' + appID + '/users/' + userID;
String result = '[]';
String authToken = OKTA_EndPoint__c.getValues('OKTA').Auth_token__c;
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(strURLEnd);
req.setMethod('GET');
req.setHeader('Accept', 'application/json');
req.setHeader('Authorization', authToken);
HttpResponse res = h.send(req);
if (200 == res.getStatusCode())
{
result = res.getBody();
map<string, Object> mapDataToUpdate = (map<string, Object>)JSON.deserializeUntyped(result);
if(mapDataToUpdate.get('status') == 'Completed')
{
oQueue.Status__c = 'Complete';
lstEfOktatoUpdate.add(oQueue);
}
}
}
if(lstEfOKtatoUpdate.size()>0)
{
try{update lstEfOKtatoUpdate;}
catch(Exception ex){EF_Error_Log.logException('EF_OKTA_Queue', lstEfOKtatoUpdate, 'DML', 'HIGH', ex.getMessage());
}
}
}
}
}
=====================================================================
I am struggling with the Error test fail
@isTest
private class EF_UpdateOKTA_Test {
//There are three A's(Arrange, Act, Assert) that you'll need to take care of in your test class
static testMethod void testCallout() {
/* Populate Test data, in your case in EF_abc_Queue__c object and other parent object where the data is being pulled up by the SOQL you
have in actuall class satisfying the WHERE condition(where status__c='Request Sent' and number_tried__c<10)
Also you need to populate data into the custom setting abc_EndPoint__c
This is where you are arranging the test data
*/
profile p =[select id from Profile WHERE Name ='Standard User' LIMIT 1];
User user = new User();
//Populate data for rest of the fields
user.Alias = 'rrn';
user.username = 'ganga@g.com';
user.Email = 'rg@h.com';
user.CommunityNickname ='raj';
user.ProfileId = p.id;
user.LastName = 'ganga';
user.TimeZoneSidKey = 'America/Los_Angeles';
user.LocaleSidKey = 'en_US';
user.EmailEncodingKey = 'UTF-8';
user.LanguageLocaleKey = 'en_US';
user.Okta_Id__c = 'hi';
insert user;
EF_Request__c efreqobj = new EF_Request__c();
efreqobj.Okta_Id__c = 'abc';
insert efreqobj;
EF_Requested__c efreqstedobj = new EF_Requested__c();
efreqstedobj.EF_Request_ID__c = efreqobj.id ;
efreqstedobj.Request_Type__c = 'Phone';
insert efreqstedobj;
EF_Resource_Metadata__c efresourceobj = new EF_Resource_Metadata__c();
efresourceobj.Name = 'abhi';
efresourceobj.OKTA_App_ID__c ='abcd';
insert efresourceobj ;
EF_OKTA_Queue__c efoktaQ = new EF_OKTA_Queue__c ();
efoktaQ.EF_Requested_Resource__c = efreqstedobj.id;
efoktaQ.OKTA_JSON__c = 'Hi';
efoktaQ.OKTA_Response__c = 'hi this is a test';
efoktaQ.CurrencyIsoCode = 'USD';
efoktaQ.status__c = 'Request Sent';
efoktaQ.Number_Tried__c = 8;
insert efoktaQ;
OKTA_EndPoint__c customSetting = new OKTA_EndPoint__c();
//Populate data for the data set 'abc'
customSetting.Name = 'testingendpoint';
insert customSetting;
Test.setMock(HttpCalloutMock.class, new EF_UpdateOKTA_MockHttpResponseGenerator());
Test.startTest();
EF_UpdateOKTA testObj = new EF_UpdateOKTA();
Test.stopTest();
EF_OKTA_Queue__c q = [Select Status__c from EF_OKTA_Queue__c where Id = :efoktaQ.id];
System.assert(q.Status__c == 'Complete');
}
}
with one mocktest callout class written in another class already
now the Error message is
System.NullPointerException: Attempt to de-reference a null object
Class.EF_UpdateOKTA.UpdateEFKotaQueue: line 39, column 1
Class.EF_UpdateOKTA.<init>: line 5, column 1
Class.EF_UpdateOKTA_Test.testCallout: line 78, column 1
Thanks in Advance
Plz help me out in writing test class for the below class
public class EF_UpdateOKTA
{
public EF_UpdateOKTA()
{
UpdateEFKotaQueue();
}
public void UpdateEFKotaQueue()
{
List<string> lstEF_OKTA_QueueID = new List<string>();
/*string strQuery = EF_SOQL_Statements__c.getValues('EF_OKTA_Queue_Batch_Query').Type__c + ' ' + EF_SOQL_Statements__c.getValues('EF_OKTA_Queue_Batch_Query').Fields__c +
EF_SOQL_Statements__c.getValues('EF_OKTA_Queue_Batch_Query').Relationship_fields__c + ' from ' + EF_SOQL_Statements__c.getValues('EF_OKTA_Queue_Batch_Query').Object__c +
' where ' + EF_SOQL_Statements__c.getValues('EF_OKTA_Queue_Batch_Query').Filter__c;
system.debug('strQuery strQuery strQuery ::' + strQuery );
List <EF_OKTA_Queue__c> lstEFOKTA_Queue_QueryData = Database.Query(strQuery);*/
List <EF_OKTA_Queue__c> lstEFOKTA_Queue_QueryData = [SELECT Id, status__c, EF_Requested_Resource__c, OKTA_JSON__c,OKTA_Response__c, number_tried__c, EF_Requested_Resource__r.EF_Resource_Metadata__r.OKTA_App_ID__c, EF_Requested_Resource__r.EF_Request_ID__r.User_Name__r.Okta_Id__c, EF_Requested_Resource__r.Request_Type__c from EF_OKTA_Queue__c where status__c='Request Sent' and number_tried__c<10 limit 10];
system.debug('lstEFOKTA_Queue_QueryDatalstEFOKTA_Queue_QueryData::' + lstEFOKTA_Queue_QueryData);
for(EF_OKTA_Queue__c objEF_OKTA_Queue_Local : lstEFOKTA_Queue_QueryData)
{
lstEF_OKTA_QueueID.add(objEF_OKTA_Queue_Local.Id);
}
if(lstEF_OKTA_QueueID.size()>0)
{
list<EF_OKTA_Queue__c> lstEfOKtatoUpdate = new list<EF_OKTA_Queue__c>();
for(EF_OKTA_Queue__c oQueue: [select id, EF_Requested_Resource__r.EF_Request_ID__r.Okta_Id__c,
EF_Requested_Resource__r.EF_Request_ID__r.User_Name__r.Okta_Id__c,
OKTA_JSON__c, number_tried__c, EF_Requested_Resource__r.Request_Type__c,
EF_Requested_Resource__r.EF_Resource_Metadata__r.OKTA_App_ID__c, Status__c
from EF_OKTA_Queue__c
where id in : lstEF_OKTA_QueueID])
{
string userID = oQueue.EF_Requested_Resource__r.EF_Request_ID__r.Okta_Id__c!=''?
oQueue.EF_Requested_Resource__r.EF_Request_ID__r.Okta_Id__c:oQueue.EF_Requested_Resource__r.EF_Request_ID__r.User_Name__r.Okta_Id__c;
string appID = oQueue.EF_Requested_Resource__r.EF_Resource_Metadata__r.OKTA_App_ID__c;
string strURLEnd = OKTA_EndPoint__c.getValues('OKTA').Site_URL__c;
strURLEnd = strURLEnd + '/apps/' + appID + '/users/' + userID;
String result = '[]';
String authToken = OKTA_EndPoint__c.getValues('OKTA').Auth_token__c;
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(strURLEnd);
req.setMethod('GET');
req.setHeader('Accept', 'application/json');
req.setHeader('Authorization', authToken);
HttpResponse res = h.send(req);
if (200 == res.getStatusCode())
{
result = res.getBody();
map<string, Object> mapDataToUpdate = (map<string, Object>)JSON.deserializeUntyped(result);
if(mapDataToUpdate.get('status') == 'Completed')
{
oQueue.Status__c = 'Complete';
lstEfOktatoUpdate.add(oQueue);
}
}
}
if(lstEfOKtatoUpdate.size()>0)
{
try{update lstEfOKtatoUpdate;}
catch(Exception ex){EF_Error_Log.logException('EF_OKTA_Queue', lstEfOKtatoUpdate, 'DML', 'HIGH', ex.getMessage());
}
}
}
}
}
=====================================================================
I am struggling with the Error test fail
@isTest
private class EF_UpdateOKTA_Test {
//There are three A's(Arrange, Act, Assert) that you'll need to take care of in your test class
static testMethod void testCallout() {
/* Populate Test data, in your case in EF_abc_Queue__c object and other parent object where the data is being pulled up by the SOQL you
have in actuall class satisfying the WHERE condition(where status__c='Request Sent' and number_tried__c<10)
Also you need to populate data into the custom setting abc_EndPoint__c
This is where you are arranging the test data
*/
profile p =[select id from Profile WHERE Name ='Standard User' LIMIT 1];
User user = new User();
//Populate data for rest of the fields
user.Alias = 'rrn';
user.username = 'ganga@g.com';
user.Email = 'rg@h.com';
user.CommunityNickname ='raj';
user.ProfileId = p.id;
user.LastName = 'ganga';
user.TimeZoneSidKey = 'America/Los_Angeles';
user.LocaleSidKey = 'en_US';
user.EmailEncodingKey = 'UTF-8';
user.LanguageLocaleKey = 'en_US';
user.Okta_Id__c = 'hi';
insert user;
EF_Request__c efreqobj = new EF_Request__c();
efreqobj.Okta_Id__c = 'abc';
insert efreqobj;
EF_Requested__c efreqstedobj = new EF_Requested__c();
efreqstedobj.EF_Request_ID__c = efreqobj.id ;
efreqstedobj.Request_Type__c = 'Phone';
insert efreqstedobj;
EF_Resource_Metadata__c efresourceobj = new EF_Resource_Metadata__c();
efresourceobj.Name = 'abhi';
efresourceobj.OKTA_App_ID__c ='abcd';
insert efresourceobj ;
EF_OKTA_Queue__c efoktaQ = new EF_OKTA_Queue__c ();
efoktaQ.EF_Requested_Resource__c = efreqstedobj.id;
efoktaQ.OKTA_JSON__c = 'Hi';
efoktaQ.OKTA_Response__c = 'hi this is a test';
efoktaQ.CurrencyIsoCode = 'USD';
efoktaQ.status__c = 'Request Sent';
efoktaQ.Number_Tried__c = 8;
insert efoktaQ;
OKTA_EndPoint__c customSetting = new OKTA_EndPoint__c();
//Populate data for the data set 'abc'
customSetting.Name = 'testingendpoint';
insert customSetting;
Test.setMock(HttpCalloutMock.class, new EF_UpdateOKTA_MockHttpResponseGenerator());
Test.startTest();
EF_UpdateOKTA testObj = new EF_UpdateOKTA();
Test.stopTest();
EF_OKTA_Queue__c q = [Select Status__c from EF_OKTA_Queue__c where Id = :efoktaQ.id];
System.assert(q.Status__c == 'Complete');
}
}
with one mocktest callout class written in another class already
now the Error message is
System.NullPointerException: Attempt to de-reference a null object
Class.EF_UpdateOKTA.UpdateEFKotaQueue: line 39, column 1
Class.EF_UpdateOKTA.<init>: line 5, column 1
Class.EF_UpdateOKTA_Test.testCallout: line 78, column 1
Thanks in Advance
By looking at the code and the line which is throwing the exception, it seems that no records are found for OKTA_EndPoint__c custom setting. Try creating a OKTA_EndPoint__c record in test code and see if the exception goes away.
Thanks