4

could anyone help in writing a test class for the following code

    global class CaptureLastLoginDate implements Database.Batchable<sObject>{

        //This is the query that is passed to the execute method. .

        String ids = UserInfo.getUserId();
        String query = 'SELECT id, LastLoginDate,LastLoginDate__c FROM User ';

        global database.queryLocator start(Database.BatchableContext BC) {
             System.debug(query);
             return database.getQueryLocator(query);
        }
        //close start method
        global void execute(Database.BatchableContext BC, List<sObject> scope){
            List<Schema.User> u = new List<Schema.User> ();
            for(sObject s: Scope){
                Schema.User inv = (Schema.User)s;
                inv.LastLoginDate__c= inv.LastLoginDate;
                u.add(inv);
                System.debug(u);
            }
            update u;
        }

        global void finish(Database.BatchableContext sc){

        }       
   }

Update

Thank you for the replying. Here is the test method I'm using:

@IsTest(SeeAllData=true)
 Private class LastLogintest
 {

String ids = UserInfo.getUserId();

    // Making the assumption that at least one user will match this. Better to assign to list and check that has at least one record.
    Schema.User testUser = [Select Id, LastLoginDate__c from User where LastLoginDate != null limit 1];
    // Set it to anything other than LastLoginDate so we can tell it has changed.
   testUser.LastLoginDate__c = null;
   update testUser;

    Test.startTest();
    CaptureLastLoginDate captureBatch = new CaptureLastLoginDate();
    // Modify the query here so we only get our one testing user of interest
    captureBatch.query = 'SELECT id, LastLoginDate,LastLoginDate__c FROM User where Id =: testUser.Id limit 1';
    ID batchprocessid = Database.executeBatch(captureBatch);
    Test.StopTest();

    User afterBatchUser = [Select Id, LastLoginDate, LastLoginDate__c from User where Id =:testUser.Id limit 1];
    System.AssertEquals(afterBatchUser.LastLoginDate, afterBatchUser.LastLoginDate__c);
}

ERROR:Error: Compile Error: unexpected token: 'testUser.LastLoginDate__c' at line 10 column 3

5

Update for

ERROR:Error: Compile Error: unexpected token: 'testUser.LastLoginDate__c' at line 10 column 3

You need to check the API name of your custom field on the User object.

That, or you are missing the method definition.

@IsTest(SeeAllData=true)
Private class LastLogintest
{
     public @IsTest(SeeAllData=true) void copyLastLoginDate() {
         String ids = UserInfo.getUserId();
         // Rest of the method body goes here...
         System.AssertEquals(afterBatchUser.LastLoginDate, afterBatchUser.LastLoginDate__c);
     }
}

An important thing to remember with testing batch jobs is:

you can test only one execution of the execute method - Source Testing Batch Apex

So, you need to setup the test data and/or query so that when you run the batch the execute method will only be called once. If you are using @IsTest(SeeAllData=true) it is possible you will have more than one batch.

Conversely, if you don't have SeeAllData=true then you won't have any users to run the test against.

Your best option is usually to insert the Users you will require for the test. However, in this case you are also relying on the internal LastLoginDate field, that I suspect you can't set manually yourself.

So the best option is to probably find an existing user or two with a LastLoginDate value and modify the query to suit.

public @IsTest(SeeAllData=true) void copyLastLoginDate() {
    // Making the assumption that at least one user will match this. Better to assign to list and check that has at least one record.
    User testUser = [Select Id, LastLoginDate__c from User where LastLoginDate != null limit 1];
    // Set it to anything other than LastLoginDate so we can tell it has changed.
    testUser.LastLoginDate__c = null;
    update testUser;

    Test.startTest();
    CaptureLastLoginDate captureBatch = new CaptureLastLoginDate();
    // Modify the query here so we only get our one testing user of interest
    captureBatch.query = 'SELECT id, LastLoginDate,LastLoginDate__c FROM User where Id = \'' + testUser.Id + '\'';
    ID batchprocessid = Database.executeBatch(captureBatch);
    Test.StopTest();

    User afterBatchUser = [Select Id, LastLoginDate, LastLoginDate__c from User where Id = :testUser.Id limit 1];
    System.AssertEquals(afterBatchUser.LastLoginDate, afterBatchUser.LastLoginDate__c);
}

You will probably need to make the query public or allow it to be passed in the constructor.

2

Testing batch class is easy if it doesn't involve complex arguments to constructors

Below is a sample code you can improove it

   Test.StartTest();
   CaptureLastLoginDate Capture = new CaptureLastLoginDate();
   ID batchprocessid = Database.executeBatch(Capture);
   Test.StopTest();

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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