You need to sign in to do that
Don't have an account?
KR_Force
test class help for Batch class
Can someone please help to buld the test class for below batch class?
global class Batch1234 implements Database.Batchable<sobject>, Database.Stateful
{
public String query;
global String error;
global database.queryLocator start(Database.BatchableContext BC)
{ String query = 'select AccountId,Account.Product_Subscriptions__c, Id,oracle_product_family__c, Product_Family__c FROM Asset where oracle_product_family__c!= \'\'';
return database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<sObject> scope)
{
List<asset> assetList =(List<Asset>)scope;
set<id>acIDs=new set<Id>();
Map<Id,Set<String>> actPsubs = new Map<Id,Set<String>>();
List<Account> updateAccountList = new List<Account>();
Map<Id,Account> updateAccountMap = new Map<Id,Account>();
System.Debug('assetList ****'+assetList);
for(Asset a :assetList){
String prodstring='';
set<string>unique=new set<String>();
for(String s:a.oracle_product_family__c.split(':')){
unique.add(s);
}
if(a.Account.Product_Subscriptions__c!=null && a.Account.Product_Subscriptions__c!=''){
System.Debug('at#1 ****');
for(String Key:a.Account.Product_Subscriptions__c.split(';')){
unique.add(key);
System.Debug('at#2 ****');
}
}
if(unique.size()!=0){
for(String st : unique ){
prodstring = prodstring +';'+ st;
}
}
System.Debug('prodstring****'+prodstring);
updateAccountMap.put(a.AccountId,new Account(Id = a.AccountId, Product_Subscriptions__c = prodstring));
}
System.Debug('updateAccountMap ****'+updateAccountMap);
if(updateAccountMap.size() != 0){
Database.SaveResult[] srList = Database.update(updateAccountMap.values(), false);
for(Database.SaveResult sr : srList)
{
for(Database.Error e : sr.getErrors())
error += '\n ' + e.getMessage();
}
}
}
global void finish(Database.BatchableContext BC){
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { finish.Email(BC)});
}
}
===============================================================================================
test class
======================================================================
static testmethod void testBatchClass() {
/*List <Asset> ast= new List<Asset>();
Set<String> uniq=new Set<String>();
Account a = new Account();
a.Name = 'test account';
insert a;
for(integer i = 0; i<200; i++){
Asset a1 = new Asset(Name='testAsset'+i, RVBD_Product_Family__c= 'test'+i+';',Instance_Number__c='77777777XX'+i,AccountID=a.id);
ast.add(a1);
uniq.add(a1.RVBD_Product_Family__c);
}
insert ast;
String prodstring='';
for(String st : uniq){
prodstring = prodstring +';'+ st;
}
a.Product_Subscriptions__c=prodstring;
Test.startTest();
Batch1123 bc = new Batch123);
bc.query = 'select AccountId, Id, Product_Family__c FROM Asset where AccountID=a.id limit 200';
bc.error='TestError';
ID batchprocessid = Database.executeBatch(bc);
Test.stopTest();
global class Batch1234 implements Database.Batchable<sobject>, Database.Stateful
{
public String query;
global String error;
global database.queryLocator start(Database.BatchableContext BC)
{ String query = 'select AccountId,Account.Product_Subscriptions__c, Id,oracle_product_family__c, Product_Family__c FROM Asset where oracle_product_family__c!= \'\'';
return database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<sObject> scope)
{
List<asset> assetList =(List<Asset>)scope;
set<id>acIDs=new set<Id>();
Map<Id,Set<String>> actPsubs = new Map<Id,Set<String>>();
List<Account> updateAccountList = new List<Account>();
Map<Id,Account> updateAccountMap = new Map<Id,Account>();
System.Debug('assetList ****'+assetList);
for(Asset a :assetList){
String prodstring='';
set<string>unique=new set<String>();
for(String s:a.oracle_product_family__c.split(':')){
unique.add(s);
}
if(a.Account.Product_Subscriptions__c!=null && a.Account.Product_Subscriptions__c!=''){
System.Debug('at#1 ****');
for(String Key:a.Account.Product_Subscriptions__c.split(';')){
unique.add(key);
System.Debug('at#2 ****');
}
}
if(unique.size()!=0){
for(String st : unique ){
prodstring = prodstring +';'+ st;
}
}
System.Debug('prodstring****'+prodstring);
updateAccountMap.put(a.AccountId,new Account(Id = a.AccountId, Product_Subscriptions__c = prodstring));
}
System.Debug('updateAccountMap ****'+updateAccountMap);
if(updateAccountMap.size() != 0){
Database.SaveResult[] srList = Database.update(updateAccountMap.values(), false);
for(Database.SaveResult sr : srList)
{
for(Database.Error e : sr.getErrors())
error += '\n ' + e.getMessage();
}
}
}
global void finish(Database.BatchableContext BC){
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { finish.Email(BC)});
}
}
===============================================================================================
test class
======================================================================
static testmethod void testBatchClass() {
/*List <Asset> ast= new List<Asset>();
Set<String> uniq=new Set<String>();
Account a = new Account();
a.Name = 'test account';
insert a;
for(integer i = 0; i<200; i++){
Asset a1 = new Asset(Name='testAsset'+i, RVBD_Product_Family__c= 'test'+i+';',Instance_Number__c='77777777XX'+i,AccountID=a.id);
ast.add(a1);
uniq.add(a1.RVBD_Product_Family__c);
}
insert ast;
String prodstring='';
for(String st : uniq){
prodstring = prodstring +';'+ st;
}
a.Product_Subscriptions__c=prodstring;
Test.startTest();
Batch1123 bc = new Batch123);
bc.query = 'select AccountId, Id, Product_Family__c FROM Asset where AccountID=a.id limit 200';
bc.error='TestError';
ID batchprocessid = Database.executeBatch(bc);
Test.stopTest();
You don't need to proivde query in batct as you have already put in start method. And Start method will execute when you execute the batch.
Just provde relvent data to your records which your creating in test class which met the criteria of your query which is define in start method and also meet the condation which you have applied in execute method.
IF it helps you than please mark it as a solution and ENJOY APEX
All Answers
You don't need to proivde query in batct as you have already put in start method. And Start method will execute when you execute the batch.
Just provde relvent data to your records which your creating in test class which met the criteria of your query which is define in start method and also meet the condation which you have applied in execute method.
IF it helps you than please mark it as a solution and ENJOY APEX