+ Start a Discussion
ForceRookieForceRookie 

How to get 100% coverage test class of batch class?

So this is my Battch class and Test class.. but it only have 45%  coverage. How to make it 100%?
global class AwesomeCBBatch implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        Decimal amt = 500;
	String query = 'SELECT Id, Contact__c, Amount FROM Opportunity WHERE Contact__c != null AND Amount >=: amt';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Opportunity> scope)
    {
        Map<Id, Contact> conts = new Map<Id, Contact>();
        for ( Opportunity opp : scope)
        {
            if(!conts.containsKey(opp.Contact__c) {
            conts.put(opp.Contact__c, new Contact(Id = opp.Contact__c, Awesome__c = true));
	    }
        }
        update conts.values();
    } 

    global void finish(Database.BatchableContext BC)
    {

    }

}
 
@isTest

public class TestAwesomeCBBatch {
    static testMethod void testAwesomeCBBatch() {
		
        Contact con = new Contact();
        con.FirstName = 'Test';
        con.LastName = 'Contact';
        con.Awesome__c = false;
        
        List<Opportunity> newopp = new List<Opportunity>();
        for (integer i = 0; i < 200; i++) {
            Opportunity o = new Opportunity();
            o.Name = 'TestOpp';
            o.Contact__c = con.Id;
            o.Amount = 600;
            o.CloseDate = Date.today();
            o.StageName = 'Prospecting';
            newopp.add(o);
        }
        insert newopp;
        
        Test.startTest();
        AwesomeCBBatch b = new AwesomeCBBatch();
        Database.executeBatch(b);
        Test.stopTest();
    }
}
Best Answer chosen by ForceRookie
Manohar kumarManohar kumar

Hi ForceRookie, which parts of the class is not getting covered? if execute method is not getting called then you can call it like this 

AwesomeCBBatch batch1 = new AwesomeCBBatch ();
batch1.execute(null,oppls);  // something like this ///batch1.execute(null,[select id from opportunity limit 2]);

Hope this helps. 

Thanks
 

All Answers

Manohar kumarManohar kumar

Hi ForceRookie, which parts of the class is not getting covered? if execute method is not getting called then you can call it like this 

AwesomeCBBatch batch1 = new AwesomeCBBatch ();
batch1.execute(null,oppls);  // something like this ///batch1.execute(null,[select id from opportunity limit 2]);

Hope this helps. 

Thanks
 

This was selected as the best answer
ForceRookieForceRookie
Hi Manohar, Yes, the execute method is not covered. But can you give the example of the code? Because I’m getting an error. Thanks!
ForceRookieForceRookie
System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []
ForceRookieForceRookie
Solved it!
I write my full query on batchname.execute();
then added insert con; above.. Thank you very much for the help!
Manohar kumarManohar kumar
i am glad i could help. 
ForceRookieForceRookie
Hello Manohar, I just have a question. How to apply the system assertion in there?
Manohar kumarManohar kumar

Hi ForceRookie, some like this will work. 

set<id> oppIds = new set<id>();
for(Opportunity opp: newopp) {
   oppIds.add(opp.id);
}

List<Opportunity> oppls = [select id from Opportunity where Awesome__c = false];

System.assertEquals(oppls.size() ,0);

Let me know if this does not work. 

 

Manohar kumarManohar kumar

Sorry i forgot to add oppIds in the query. 

try like this 

set<id> oppIds = new set<id>();
for(Opportunity opp: newopp) {
   oppIds.add(opp.id);
}
List<Opportunity> oppls = [select id from Opportunity where Awesome__c = false and id IN :oppIds];
System.assertEquals(oppls.size() ,0);