-1

i need to cover 75% but covering 22% only please help me to cover code in batch apex.

batch apex:

global class UpdateContactAcoount implements Database.Batchable<sObject> {  
     map<string,contact>  conmap = new map<string,contact>();
     global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(
            'SELECT ID,name,Accountnumber, (SELECT ID, lastname, accountid FROM Contacts) FROM Account Where Accountnumber = \'123\''
        );
    }
    global void execute(Database.BatchableContext bc, List<Account> scope){
        account a = [select id,name,(select id,firstname,lastname,accountid from contacts) from account where name=:'bicsnn'];
        map<id,contact> conacc = new map<id,contact>([select id,lastname,accountid from contact where id=: conmap.values()]);
        map<string,contact> newmap = new map<string,contact>();    
        list<contact> setupdate = new list<contact>();
            for(account acc : scope){
                for(contact c : acc.contacts){
                    if(!conmap.containskey(c.lastname)){
                        conmap.put(c.lastname,c);
                        system.debug('oldcontact:'+conmap);
                    }    
                }
            }
            for(contact con :a.contacts){
                newmap.put(con.lastname,con);
                system.debug('newcontact:'+newmap);
            }
            for(string key : conmap.keyset()){
                for(contact cc : conmap.values()){
                    if(!newmap.containskey(key)){
                        cc.accountid = a.id;
                        setupdate.add(cc);
                        system.debug('listcontact:'+setupdate);
                    }      
                }             
            }
        update setupdate;        
    }    
    global void finish(Database.BatchableContext bc){
    }    
}

Test Class:

@istest
public class UpdateContactAcoount_Test {
    static testmethod void testbatch(){
      list<account> acc = new list<account>();
      account a = new account();
        a.name = 'test';
        acc.add(a);
        insert acc;
       database.batchablecontext bc;
      UpdateContactAcoount db = new UpdateContactAcoount();
        db.start(bc);
        db.execute(bc, acc);
        db.finish(bc);               
    }  
}

enter image description here

  • Insert Contact also for more coverage. – Reshma Apr 4 '17 at 10:23
  • Did you check if your batch works? I don't think it will. There are a lot of problems of code in it. – Martin Lezer Apr 4 '17 at 10:26
0


Try the code below:

@istest
public class UpdateContactAcoount_Test 
{
  static testmethod void testbatch()
   {
     List<account> acc = new List<account>();
     Account a = new account();
     a.name = 'bicsnn';
     a.AccountNumber = '123';
     acc.add(a);
      insert acc;
     Contact con = new Contact();
     con.FirstName = 'Test';
     con.LastName = 'Contact';
     con.AccountId = acc[0].Id;
      insert con;
     Test.StartTest();
     UpdateContactAcoount  instancevar = new UpdateContactAcoount ();
     ID batchprocessid = Database.executeBatch(instancevar);
     Test.StopTest();
     }  
}

NOTE:-
Firstly when you are trying a test class then try to cover all the scenarios.Like in your code there is Account having Contact but you are just inserting Account not Contact.

Secondly if you are specifically mentioning the Name of the Account in your code then try to create an Account record with that name only.Like in code it was "bicsnn".
Regards,
Akshay

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.