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
ArjunmcaArjunmca 

Test class correction

Hi,

 

Can any body correct the test class.

 

Actual Class

----------------------

 

global public with sharing class OrgExpireQueryBatchable implements Database.Batchable<sObject>, Database.Stateful, Database.AllowsCallouts {


 public OrgExpireQueryBatchable(){
  
 }
 
 global Database.QueryLocator  start(Database.BatchableContext ctx) {
 
   return Database.getQuerylocator('Select Trial_Expiration_Date__c, Name, Master_Password__c, Master_Login__c, Login__c From Test_Org__c where Name like \'IO%\'');  
  
 }
  
 global void execute(Database.BatchableContext ctx, List<sObject> batchRecord) {
    Test_Org__c record =  (Test_Org__c)batchRecord[0];
    System.debug('Master Login' + record.Master_Login__c);
    Id OrgId =  record.Id;
    String loginName;
    String loginPassword;
    loginName = record.Master_Login__c;
    loginPassword = record.Master_Password__c;
    try{ 
        partnerSoapSforceCom.Soap stub = new partnerSoapSforceCom.Soap();
        partnerSoapSforceCom.LoginResult loginResult; 
        
        If(!Test.isRunningTest())
        {
           loginResult = stub.login(loginName,loginPassword);
        }
        stub.SessionHeader =  new partnerSoapSforceCom.SessionHeader_element();
        stub.endpoint_x = loginResult.serverURL;
        stub.SessionHeader.sessionId = loginResult.sessionId;
        string queryStringExpDate ='SELECT Id,TrialExpirationDate   FROM Organization';
        DateTime currentTime = DateTime.now() ;
        DateTime temp =  currentTime.addMinutes(-10);
        String tempStr =  temp.format('yyyy-MM-dd\'T\'kk:mm:ssZ');
        string queryStringLastLogin ='SELECT LoginTime from LoginHistory Where LoginTime < ' +  tempStr + ' order by LoginTime Desc Limit 1';
        partnerSoapSforceCom.QueryResult  qResultExpDate = stub.query(queryStringExpDate );
        partnerSoapSforceCom.QueryResult  qResultLastLogin = stub.query(queryStringLastLogin );
        System.debug('@@@@ query Results: ' + qResultExpDate );
        System.debug('@@@@ query Results.sOject_x: ' + qResultExpDate .records );
        
        DateTime ExpDate=qResultExpDate.records[0].TrialExpirationDate;
        DateTime LastLoginDate = qResultLastLogin.records[0].LoginTime;
        
        // Converting Datetime to Date
        Date dateTemp_ExpDate = Date.newInstance(ExpDate.year(),ExpDate.month(),ExpDate.day());
        Date dateTemp_LastLoginDate  = Date.newInstance(LastLoginDate.year(),LastLoginDate.month(),LastLoginDate.day());
        record.Trial_Expiration_Date__c = dateTemp_ExpDate;
        record.Last_Login_Date__C = dateTemp_LastLoginDate;
        
        update record;
    }catch(CalloutException ex)
    {
        
    }
 
 }
 
 global void finish(Database.BatchableContext ctx) {
    
 }
}

 

 

 

 

Test Class

-----------------

 

@IsTest
public class TestOrgExpireQueryBatchable
{
 
  @IsTest static void TestExecute()
  {
     string loginName='admin@xyz.com';
     string loginPassword= 'test1';
 
    
     Test_Org__c obj=new Test_Org__c();
     obj.Name= 'DEV96';
     obj.Master_Login__c = 'admin@xyz.com';
     obj.Master_Password__c ='test1';
     insert obj;
     
 
     List<SObject> batchobjs = new List<SObject>();
     batchobjs.add(obj);
     
     OrgExpireQueryBatchable objbatch= new OrgExpireQueryBatchable();
     objbatch.start(null);
     objbatch.execute(null, batchobjs );
     partnerSoapSforceCom.Soap stub = new partnerSoapSforceCom.Soap();
     partnerSoapSforceCom.LoginResult loginResult = new partnerSoapSforceCom.LoginResult();
     stub.SessionHeader.sessionId = loginResult.sessionId;
     stub.SessionHeader =  new partnerSoapSforceCom.SessionHeader_element();
     stub.endpoint_x = loginResult.serverURL;
     stub.SessionHeader.sessionId = loginResult.sessionId;
     string queryStringExpDate ='SELECT Id,TrialExpirationDate FROM Organization';
     
     Test_Org__c swbobj=new Test_Org__c();
     Date curdate = Date.today();
     swbobj.Trial_Expiration_Date__c = curdate;
     swbobj.Last_Login_Date__C = curdate.addDays(2);
     insert swbobj;
     
     swbobj.Last_Login_Date__C = curdate.addDays(3);
     update swbobj;
     
    
     System.assertEquals(curdate,swbobj.Trial_Expiration_Date__c);
     System.assertEquals(curdate.addDays(3),swbobj.Last_Login_Date__C);
     
   }

}

 

 

My test class is not covering below lines

 

 stub.SessionHeader.sessionId = loginResult.sessionId;
     stub.SessionHeader =  new partnerSoapSforceCom.SessionHeader_element();
     stub.endpoint_x = loginResult.serverURL;

 

 

Please correct this test case.

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox
partnerSoapSforceCom.LoginResult loginResult;

Change to:

 

partnerSoapSforceCom.LoginResult loginResult = new partnerSoapSforceCom.LoginResult();

Reason: When running test mode, stub.login isn't called, so loginResult will be null. Your code throws a NullPointerException because you can't access loginResult.serverUrl on line 31. Creating an empty login result will fix this problem (but this code will need more work later). Your next problem will come up on line 38, however, because you can't call the web service without a valid session ID (nor at all, because you can't callout during test methods). However, you'll still probably get to close to 75% with that one change.

 

 

All Answers

David SupuranDavid Supuran

You're not asking a question and wanting help, but you're asking someone to do your work for you.

Hire a consultant for that.

sfdcfoxsfdcfox
partnerSoapSforceCom.LoginResult loginResult;

Change to:

 

partnerSoapSforceCom.LoginResult loginResult = new partnerSoapSforceCom.LoginResult();

Reason: When running test mode, stub.login isn't called, so loginResult will be null. Your code throws a NullPointerException because you can't access loginResult.serverUrl on line 31. Creating an empty login result will fix this problem (but this code will need more work later). Your next problem will come up on line 38, however, because you can't call the web service without a valid session ID (nor at all, because you can't callout during test methods). However, you'll still probably get to close to 75% with that one change.

 

 

This was selected as the best answer
ArjunmcaArjunmca

Thanks for the response.

 

 

You mean to change in  Actual class ?

 

From

partnerSoapSforceCom.LoginResult loginResult

 

To

partnerSoapSforceCom.LoginResult loginResult = new partnerSoapSforceCom.LoginResult();

 

 

sfdcfoxsfdcfox
That's correct.