+ Start a Discussion

Need test clas for below batch apex

global class UserLogoutEvents implements Database.batchable<String>, Database.Stateful{
  global list<Document> doc=new List<Document>();
  Blob logoutcontentFile;
 global Iterable<String> start(Database.BatchableContext BC){   
                    doc=[SELECT id,body,BodyLength,ContentType,CreatedDate,Description,DeveloperName,Name,Type FROM Document   ];
                    String[] filelines = new String[]{};
                    string nameFile=logincontentFile.toString();
                    return new Utility_RowIterator(nameFile,'\n');
 global void execute(Database.BatchableContext BC, List<string> filelines ){
    Map<id,user> userList=new map<id,user>([select id,name,Email,Division,Username from user]);
        List<LogHistory__c> accstoupload;
        accstoupload = new List<LogHistory__c>();
        for (Integer i=1;i<filelines.size();i++)
            String[] inputvalues = new String[]{};
            inputvalues = filelines[i].split(',');
            LogHistory__c a = new LogHistory__c();
            a.Name = inputvalues[0].remove('"');// event type
            a.USER_ID__c = inputvalues[4].remove('"'); // user id      
            a.CLIENT_IP__c = inputvalues[10].remove('"');//client ip
            //a.SOURCE_IP__c = inputvalues[11].remove('"');
            a.TIMESTAMP_DERIVED__c = inputvalues[18].remove('"');
            a.User_Division__c =userList.get( a.USER_ID__c).Division;
            a.User_Email__c=userList.get( a.USER_ID__c).email;
            a.User_Login_Mail__c=userList.get( a.USER_ID__c).Username;
            a.User_Name__c=userList.get( a.USER_ID__c).name;
      insert accstoupload;
 global void finish(Database.BatchableContext BC){              
Since writing a test class to cover all of the facets of this class is not something that anyone on here will do for you, I can give you some pointers and hopefully get you started.  I would recommend that you do some reading on testing [1] [2] [3] [4] [5] to get a better understanding.  Each of your individual tests should only tests one specific portion of you class (ie a constructor test, sendEmail test, contactSelected test, etc).  You should also have both a postitive (everything works perfectly) and a negative (things are not right) test.

Each test should follow the following structure:
  • Setup of test data. This includes creation of any data needed by your class.  Account, Contacts etc
  • Starting the test. This is calling Test.startTest() to reset the governor limits
  • Calling your class / method
  • Stopping the test.This is calling Test.stopTest() to reset the governor limits and allow for any async jobs to finish
  • Asserting that your changes have worked
    • If you have inserted/updated/deleted data, you need to query for the updates
    • Run System.assert, System.assertEquals, System.assertNotEquals to verify that you got the correct data back
If you have any specific problems with your tests, feel free to create a new post with the part of the class you are trying to test and your current test method, and you will more likely get a better response then asking for someone to essentially write an entire test class for you.

[1] http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/
[2] http://pcon.github.io/presentations/testing/
[3] http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/
[4] http://blog.deadlypenguin.com/blog/testing/strategies/
[5] https://developer.salesforce.com/docs/atlas.en-us.apex_workbook.meta/apex_workbook/apex_batch_2.htm