function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ForceRookieForceRookie 

How to create test class of Batch class?

Field                API Name          Datatype
Contact           Contact__c         LookUp        //field from Opportunity
Awesome        Awesome__C    CheckBox    //field from Contact
global class BatchAwesomeContact implements Database.Batchable<sObject>{
    Map<Id, Contact> conts = new Map<Id, Contact>();
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String Query = 'SELECT Id, Contact__c, Amount, (SELECT Id, FirstName, LastName, Awesome__c FROM Contact) FROM Opportunity WHERE Id IN :conts.keySet()';
        return Database.getQueryLocator(Query);
    }
    
    global void execute(Database.BatchableContext BC, List<Opportunity> scope) {
        for (Opportunity s : scope) {
            if (s.Contact__c != null) {
                if (s.Amount >= 500 && !conts.containsKey(s.Contact__c)) {
                    conts.put(s.Contact__c, new Contact(Id = s.Contact__c, Awesome__c = true));
                }
            }
        }
        update conts.values();
    }
    
    global void finish(Database.BatchableContext BC) {
        
    }
}
Deepak Maheshwari 7Deepak Maheshwari 7
@isTest()
public class BatchNewTopicsUpdateTest {

    static testMethod void myUnitTest() {
    	
    	Contact con= new Contact(lastname='test_Contact', Awesome=true);
    	Insert con;
    	
    	Opportunity opp = new Opportunity(name='test_Opp', Contact__c =con.id , Amount=600);
    	Insert opp;

        Test.StartTest();

        BatchAwesomeContact newTopicsUpdate = new BatchAwesomeContact();
     	
     	Database.executeBatch(newTopicsUpdate);
	    
	    Test.StopTest();   
        
    }
}


Please try the above test class.

Hope this helps you! 

ForceRookieForceRookie

I get this error when I run the test class --
Errors: System.QueryException: unexpected token: '('
Stack Trace: Class.BatchAwesomeContact.start: line 5, column 1

Maybe there's something wrong with my query above? I can't configure it.

DeveloperSudDeveloperSud
Hi ,
Use the query like below.Hope this will help.

'SELECT Id, Contact__c, Amount, (SELECT Id, FirstName, LastName, Awesome__c FROM Contacts) FROM Opportunity WHERE Id IN :conts.keySet()';
ForceRookieForceRookie
That’s exactly my query. Maybe I have to do it in dot notation? How to do it?
DeveloperSudDeveloperSud
I figured out the problem.you have created a parent child relationship between contact and opportunity and in query you are keeping contact as child object which is totally wrong.The logic itself is not correct.
 
ForceRookieForceRookie
Help me to debug. Because there’s really something wrong with the query.
ForceRookieForceRookie
This is the original trigger of that..
Will you please change it to batch class?
trigger AwesomeCB on Opportunity (after insert, after update) {

Map<Id, Contact> conts = new Map<Id, Contact>();
for (Opportunity opp : Trigger.new) {
if (opp.Contact__c != null) {
if (opp.Amount >= 500 && !conts.containsKey(opp.Contact__c)) {
conts.put(opp.Contact__c, new Contact(Id = opp.Contact__c, Awesome__c = true));
}
}
}
update conts.values();
}