You need to sign in to do that
Don't have an account?
Maria22
Need a help in test class
Hi Everyone,
I need yours all experet advises which help me to achieve more than 75% coverage for my batch class. Right now my code coverage for my class stands at 73%(46 lines covered out of 63). I have tried so much and gave try to almost all permutations and combinations still not able to achieve more than 75%.
Below is the batch class for which I wrote test class
Below is the test class which stands at 73%
Below are the lines of codes which is not getting covered
Any help will be greatly appreciated.
Mnay thanks in advance
Thanks & Regards,
Harjeet
I need yours all experet advises which help me to achieve more than 75% coverage for my batch class. Right now my code coverage for my class stands at 73%(46 lines covered out of 63). I have tried so much and gave try to almost all permutations and combinations still not able to achieve more than 75%.
Below is the batch class for which I wrote test class
global with sharing class OpportunityWithSurgeryBatch extends BasicJob { global OpportunityWithSurgeryBatch() { super(200); } global OpportunityWithSurgeryBatch(Integer batchSize) { super(batchSize, null); } /* Fetch all potential accounts to close the opportunities. */ global List<Application_Log__c> logRec2insert = new List<Application_Log__c>(); global override Database.QueryLocator start(Database.BatchableContext BC){ return Database.getQueryLocator([ SELECT ( SELECT Surgery_Date__c FROM Install_Base__r WHERE Surgery_Date__c <> NULL ORDER BY Surgery_Date__c DESC ), Candidate_Creation_Date__c, Lead_Status__pc, ID, ( SELECT ID, stageName, Surgery_Date__c,CreatedDate,Probability FROM Opportunities WHERE RecordType.name = 'Surgery' , AND stageName NOT IN('Surgery Complete',' Closed Won','Closed Lost') ORDER BY CreatedDate DESC ) FROM Account WHERE RecordType.name = 'Recipient' AND First_Surgery_Date__c <> NULL ]); } /* Process all the records */ global override void execute(Database.BatchableContext BC, List<sObject> scope){ String myLogMessage; Integer processedRecordsNumber; Map<Id,String> accIdLogMessageMap=new Map<Id,String>(); //create list of opportunities to update List<Opportunity> opportunities2update = new List<Opportunity>(); List<Account> accounts2update = new List<Account>(); //check update conditions processedRecordsNumber = 0; try{ for(Account acc : (List<Account>)scope) { if (acc.Install_Base__r.size() > 0) { Install_Base__c equipment = acc.Install_Base__r[0]; //set opportunity to update list<Opportunity> opp = acc.Opportunities; //Create a boolean variable to check if the processed oppty is the latest one Boolean FirstRecord = true; //Run through the oppty for (Opportunity op :opp) { if(Op.CreatedDate < equipment.Surgery_Date__c && FirstRecord){ FirstRecord = false; myLogMessage = ''; op.stageName = 'Closed Won'; op.Surgery_Date__c = equipment.Surgery_Date__c; op.Probability = 100; opportunities2update.add(op); myLogMessage = myLogMessage + 'opportunity ID =(' + op.ID + '), '; } //closed lost if(Op.CreatedDate < equipment.Surgery_Date__c && (Op.StageName<>'Surgery Complete' || Op.StageName<>'ClosedWon'||Op.StageName<>'Closed Lost')) { myLogMessage = ''; op.stageName = 'Closed Lost'; op.Surgery_Date__c = equipment.Surgery_Date__c; //op.Probability = 100; opportunities2update.add(op); myLogMessage = myLogMessage + 'opportunity ID =(' + op.ID + '), '; } if (myLogMessage <> ''){ ++processedRecordsNumber; myLogMessage = 'Record number in the batch (' + processedRecordsNumber + '), ' + myLogMessage; accIdLogMessageMap.put(acc.ID,myLogMessage ); } else{ accIdLogMessageMap.put(acc.ID,'' ); } } } } update opportunities2update; for(Id accId:accIdLogMessageMap.keySet()){ // set account for update Account acc=new Account(id=accId); acc.Lead_Status__pc = 'Surgery Complete'; accounts2update.add(acc); if(accIdLogMessageMap.get(accId)!=''){ //set log to insert Application_Log__c myLog = new Application_Log__c(); myLog.Source__c = 'OpportunityWithSurgery'; myLog.Source_Function__c = 'close'; myLog.Reference_Id__c = acc.ID; myLog.Reference_Info__c = 'MasterAccountId'; myLog.Status__c = 'Completed'; myLog.Message__c = accIdLogMessageMap.get(accId); logRec2insert.add(myLog); } } update accounts2update; }catch (Exception ex){ ApplicationLog.logMessage('Opportunity update Fail', 'OpportunityWithSurgery', 'close', 'EXCEPTION', null, null, null, ex); } finally { ApplicationLog.logMessage('Completed', 'OpportunityWithSurgery', 'close', 'EXCEPTION', null, ' Total SQL: ' + Limits.getLimitQueries() + ' Used SQL:' + Limits.getQueries(), null, null); } } global override void finish(Database.BatchableContext BC){ insert logRec2insert; } }
Below is the test class which stands at 73%
@isTest public class OpportunityWithSurgeryBatchTest{ @testSetup static void setup() { Id recipientId = RecordTypeUtility.getRecordTypeId(Account.sobjecttype, Constants.ACCOUNT_RECORDTYPE_RECIPIENT); Id surgeryId = RecordTypeUtility.getRecordTypeId(Opportunity.sobjecttype, Constants.OPP_RECORDTYPE_SURGERY); Date My_Base_Test_Date = System.today(); //set some test date List<Account> accountList = new List<Account>(); List<Install_Base__c> ibList = new List<Install_Base__c>(); List<Opportunity> OppList = new List<Opportunity>(); for(Integer i =0;i<10;i++){ Account acct = new Account(); acct.LastName = 'TestOpportunityWithSurgery'; acct.RecordTypeId = recipientId; acct.First_Surgery_Date__c = My_Base_Test_Date.addDays(+i); accountList.add(acct); } INSERT accountList; for(Integer i =0;i<10;i++){ Install_Base__c ib = new Install_Base__c(); ib.Owner__c = accountList[i].Id; ib.Surgery_Date__c = My_Base_Test_Date; ibList.add(ib); Opportunity opp = new Opportunity(); opp.AccountId = accountList[i].Id; opp.Name = 'OppName'; opp.RecordTypeID = surgeryId; opp.StageName = 'New'; opp.CreatedDate = My_Base_Test_Date.addDays(-i); opp.CloseDate = System.today(); OppList.add(opp); } INSERT ibList; INSERT OppList; } static testMethod void test(){ Id recipientId = RecordTypeUtility.getRecordTypeId(Account.sobjecttype, Constants.ACCOUNT_RECORDTYPE_RECIPIENT); Id surgeryId = RecordTypeUtility.getRecordTypeId(Opportunity.sobjecttype, Constants.OPP_RECORDTYPE_SURGERY); Date My_Base_Test_Date = System.today(); //set some test date Account acct = new Account(); acct.LastName = 'TestOpportunityWithSurgery'; acct.RecordTypeId = recipientId; //make registration date before surgery acct.First_Surgery_Date__c = My_Base_Test_Date.addDays(+1); //acct.Candidate_Creation_Date__c = My_Base_Test_Date.addDays(-1); acct.Lead_Status__pc = 'Surgery Complete'; INSERT acct; Install_Base__c ib = new Install_Base__c(); ib.Owner__c = acct.Id; ib.Surgery_Date__c = My_Base_Test_Date; INSERT ib; Opportunity opp = new Opportunity(); opp.AccountId = acct.Id; opp.Name = 'OppName'; opp.RecordTypeID = surgeryId; //opp.StageName = 'New'; opp.StageName ='Closed Won'; opp.CloseDate = System.today(); opp.CreatedDate = My_Base_Test_Date.addDays(-1); opp.Surgery_Date__c = My_Base_Test_Date; INSERT opp; Integer beforeCount = getApplicationLogCount(); Test.startTest(); OpportunityWithSurgeryBatch obj = new OpportunityWithSurgeryBatch(); DataBase.executeBatch(obj); Test.stopTest(); Integer afterCount = getApplicationLogCount(); acct = getAccount(acct.Id); System.assert(acct.Lead_Status__pc == 'Surgery Complete'); opp = getOpportunity(acct.Id); System.assert(opp.StageName == 'Closed Won'); System.assert(opp.Surgery_Date__c == ib.Surgery_Date__c); System.assertEquals(beforeCount + 2 ,afterCount); } private static Account getAccount(Id acctId) { return [SELECT Lead_Status__pc FROM Account WHERE Id = :acctId]; } private static Opportunity getOpportunity(Id acctId) { return [SELECT stageName, Surgery_Date__c, Probability FROM Opportunity WHERE AccountId = :acctId]; } private static Integer getApplicationLogCount() { return [SELECT count() FROM Application_Log__c WHERE Source__c = 'OpportunityWithSurgery' AND Source_Function__c = 'close']; } static testMethod void testforMultipleRecords(){ Integer beforeCount = getApplicationLogCount(); Test.startTest(); OpportunityWithSurgeryBatch obj = new OpportunityWithSurgeryBatch(); DataBase.executeBatch(obj,200); Test.stopTest(); Integer afterCount = getApplicationLogCount(); System.assertEquals(beforeCount + 2, afterCount); } }
Below are the lines of codes which is not getting covered
for(Id accId:accIdLogMessageMap.keySet()){ // set account for update Account acc=new Account(id=accId); acc.Lead_Status__pc = 'Surgery Complete'; accounts2update.add(acc); if(accIdLogMessageMap.get(accId)!=''){ //set log to insert Application_Log__c myLog = new Application_Log__c(); myLog.Source__c = 'OpportunityWithSurgery'; myLog.Source_Function__c = 'close'; myLog.Reference_Id__c = acc.ID; myLog.Reference_Info__c = 'MasterAccountId'; myLog.Status__c = 'Completed'; myLog.Message__c = accIdLogMessageMap.get(accId); logRec2insert.add(myLog); } }
Any help will be greatly appreciated.
Mnay thanks in advance
Thanks & Regards,
Harjeet
Raj Vakati
TRY this code