You need to sign in to do that
Don't have an account?
ForceRookie
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(); } }
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
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
I write my full query on batchname.execute();
then added insert con; above.. Thank you very much for the help!
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.
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);