+ Start a Discussion

Test Class for Bath Apex - Please Help!!!

Hi All-
     I need your help in writing a test class for the following batch apex class. This batch creates new child records on being processed. I saw multiple posts where batches update the exisiting record and understood how to writes a test class for the same. But since my batch is creating completely new records I am having difficulty in writing its test class. I have written something but that does not cover the entire execute methos , it covers everything else. Please help me.

Batch Class: 

global class CreateNewSummaryFormRecords implements Database.Batchable<Sobject>, Database.Stateful {

    global Database.QueryLocator start(Database.BatchableContext bc) {
        //first, you gather all the records you want to create a new record with
        String query = 'Select Id,Risk_Assessment_Active__c,Project_Manager__c,Chair_Reviewer__c from Project_Risk_Review__c where Risk_Assessment_Active__c = true and Review_Meeting_Date_Tentative__c = False and Number_Of_Days_For_Next_Review__c=2';
        //below checks to see if the batch is being called from a test class. If the record count
        //returned by the query above is over 200, the test will fail
            query += ' limit 200';
        return Database.getQueryLocator(query);
    global void execute(Database.BatchableContext bc, List<Project_Risk_Review__c> originalrecords){
        //now we want to loop through the records from the query above and create the new records
        //and store them in a list
        List<Project_Form__c> newrecords = new List<Project_Form__c>();
        for(Project_Risk_Review__c yo1:originalrecords){ // this is the loop
            Project_Form__c yo2 = new Project_Form__c(); //this sets up a new record to be created
            yo2.Project_Risk_Assessment__c= yo1.Id;
            //add any other fields on the new record that you want to set
            //add to list to insert into SFDC later

        //this checks to see if there are any records to insert. If there are, then it will 
        //create them
        //newrecords is the list of records you put in during the for loop. The "false" is saying,
        //if one records fails to insert, still insert the rest (All or Nothing)
    global void finish(Database.BatchableContext bc) {}

Sample Test Class:

public class CreateNewSummaryFormRecordsTest
  static testMethod void testMethod1(){
        Account testAccount = TestUtil.generateTestAccount();
        insert testAccount;

        Opportunity testOpp = new Opportunity();
        testOpp.Name = 'Opportunity For Unit Test';
        testOpp.AccountId = testAccount.Id;
        testOpp.StageName = 'Qualification';
        testOpp.CloseDate = Date.Today().AddDays(365);
        testOpp.Org__c= '1010101';
        testOpp.Project_Director__c = UserInfo.getUserId();
        insert testOpp;
        List<User> u1= [SELECT Id,IsActive FROM User WHERE isActive=True and ProfileId=:Label.PRAS_RTI_BusinessDevelopment_Profile_Id Limit 4];
        List<User> u2= [SELECT Id,IsActive FROM User WHERE isActive=False and ProfileId=:Label.PRAS_RTI_BusinessDevelopment_Profile_Id Limit 1];
        Costpoint_Project__c testCostpointProject = new Costpoint_Project__c();
        testCostpointProject.Name = '0281300.003';
        testCostpointProject.Project_Name__c = '0281300.003';
        testCostpointProject.Project_Number__c = '0281300.003';
        insert testCostpointProject;
        //Fetch related Project Risk Review form.
        Project_Risk_Review__c ProRiskRev=[Select id,DVP__c,Project_Manager__c,Chair_Reviewer__c,Delegate__c 
                                           from Project_Risk_Review__c where Costpoint_Project__c=:testCostpointProject.Id Limit 1];
        Update ProRiskRev;
        Update ProRiskRev;
        Project_Form__c ProForm=new Project_Form__c();
        insert ProForm;
        CreateNewSummaryFormRecords obj = new CreateNewSummaryFormRecords();


Hi Shrey,
I am not sure which part of your class/method is not getting covered for test coverage.
I could suggest few finding.
1. If your using SOQL inside test class, better use annotation @seeAllDataTrue. So your test class will access org data to satisfy your method. Though this is not the best solution. you should create your own data to be used inside test method. Even you should create a user inside the test method rather than querying u1/u2.
2. your batch class execute method looks for Project_Risk_Review__c> originalrecords. Does your test method have list of Project_Risk_Review__c before you call DataBase.executeBatch(obj);?

Hope this would help you!.