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
gowtham murugesangowtham murugesan 

hi all,am new to Test class ,kindly help me out

This is my code,


global class OpportunityInvoiceReport implements Database.Batchable<sObject>,Schedulable {
    
    global void execute(SchedulableContext SC) {
        Id batch = Database.executeBatch(new OpportunityInvoiceReport(),100);
    }
    global Database.QueryLocator start(Database.BatchableContext BC){
        String query = 'SELECT Id,AccountId,Invoice_Number__c FROM Opportunity WHERE StageName = \'Implementation Complete (MED)\'';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<Opportunity> scope){
        Set<String> invoiceNumber = new Set<String>();
        Map<String,Opportunity> oppMap = new Map<String,Opportunity>();
        List<Opportunity_Invoices__c> oppInvoiceInsert = new  List<Opportunity_Invoices__c>();
        for(Opportunity opp:scope){
            invoiceNumber.add(opp.Invoice_Number__c);
            oppMap.put(opp.Invoice_Number__c,opp);
        }
        for(Invoice__c invoice:[SELECT Id,Name FROM Invoice__c WHERE Name =:invoiceNumber]){
            Opportunity_Invoices__c oppInvoice = new Opportunity_Invoices__c();
            oppInvoice.Invoice__c = invoice.Id;
            oppInvoice.Account__c = oppMap.get(invoice.Name).AccountId;
            oppInvoice.Opportunity__c = oppMap.get(invoice.Name).Id;
            oppInvoiceInsert.add(oppInvoice);
        }
        insert oppInvoiceInsert;
    }
    
    global void finish(Database.BatchableContext BC){
        System.debug('OpportunityInvoiceReport Batch Successfully COmpleted..!');
    }
}
Best Answer chosen by gowtham murugesan
Ajay K DubediAjay K Dubedi
Hi Gowtham,
Try this code :
@isTest
private class OpportunityInvoiceReport_Test {
    @isTest static void testInvoiceReport() {
        Opportunity opp = new Opportunity();
        opp.Name = 'Test Opportunity';
        opp.CloseDate = system.today();
        opp.StageName = 'Implementation Complete (MED)';
        opp.Invoice_Number__c = 'Test123';
        //All required field.
        insert opp;
        System.assertNotEquals(opp.Id, null,'Assertion Failed : Opportunity Not Inserted');
        
        Account accObj = new Account();
        accObj.Name='Test Account' ;
        //All required field.
        insert accObj;
        System.assertNotEquals(accObj.Id, null,'Assertion Failed : Account Not Inserted');
        
        Invoice__c inObj = new Invoice__c();
        inObj.Name = 'Test123';
        inObj.Account__c = accObj.Id;
        inObj.Opportunity__c = opp.Id;
        //All required field.
        Insert inObj;
        System.assertNotEquals(inObj.Id, null,'Assertion Failed : Invoice Not Inserted');
        
        Test.startTest();
        OpportunityInvoiceReport oppbatch = new OpportunityInvoiceReport();
        Database.executebatch(oppbatch);
        Test.stopTest();
        
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi

All Answers

Himanshu.SFDCHimanshu.SFDC
Hey,
Try this:
@isTest
public class OpportunityInvoiceReportTest {
	
    static testMethod void MainMethod(){
     Test.startTest();
       //Insert Invoice record
        Invoice__c inv = new Invoice__c ();
        inv.name = 'Invoice-0001';
        insert inv;
       
       //insert Account record
        Account testAccount = new Account();
		testAccount.Name='Test Account' ;
		insert testAccount;
       
       //Insert opportunity record
       Opportunity opp = new Opportunity();
       opp.Name = 'Test Opportunity';
       opp.StageName = 'Implementation Complete (MED)';
       opp.CloseDate = system.today();
       opp.AccountId = testAccount.id;
       opp.Invoice_Number__c = inv.name;
       insert opp;
       
        OpportunityInvoiceReport oppbatch = new OpportunityInvoiceReport();
        Database.executebatch(oppbatch);
        
    Test.stopTest();
    }
}
Please mark this as best anser if it helps.
 
Ajay K DubediAjay K Dubedi
Hi Gowtham,
Try this code :
@isTest
private class OpportunityInvoiceReport_Test {
    @isTest static void testInvoiceReport() {
        Opportunity opp = new Opportunity();
        opp.Name = 'Test Opportunity';
        opp.CloseDate = system.today();
        opp.StageName = 'Implementation Complete (MED)';
        opp.Invoice_Number__c = 'Test123';
        //All required field.
        insert opp;
        System.assertNotEquals(opp.Id, null,'Assertion Failed : Opportunity Not Inserted');
        
        Account accObj = new Account();
        accObj.Name='Test Account' ;
        //All required field.
        insert accObj;
        System.assertNotEquals(accObj.Id, null,'Assertion Failed : Account Not Inserted');
        
        Invoice__c inObj = new Invoice__c();
        inObj.Name = 'Test123';
        inObj.Account__c = accObj.Id;
        inObj.Opportunity__c = opp.Id;
        //All required field.
        Insert inObj;
        System.assertNotEquals(inObj.Id, null,'Assertion Failed : Invoice Not Inserted');
        
        Test.startTest();
        OpportunityInvoiceReport oppbatch = new OpportunityInvoiceReport();
        Database.executebatch(oppbatch);
        Test.stopTest();
        
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
This was selected as the best answer