0

I am new to write test classes does any one give me idea for follwing trigger?

trigger AccountTrigger on Account(after update){  

      if(Trigger.isAfter && Trigger.isUpdate)
      {
        AccountTrigger.handleAfterUpdate(Trigger.New,Trigger.OldMap);
      }

 }

class

public class AccountTriggerHelper {

 public static void handleAfterUpdate(List<Account> accountSoLst, Map<Id,Account> oldMap) {

  List<BusinessHours> BusinessHoursId = new List<BusinessHours >();
        BusinessHoursId.add([Select Id from BusinessHours where Name ='austria']);
        BusinessHoursId.add([Select Id from BusinessHours where Name ='England']);
        BusinessHoursId.add([Select Id from BusinessHours where Name ='Africa']);
        BusinessHoursId.add([Select Id from BusinessHours where Name ='Canda']);
        BusinessHoursId.add([Select Id from BusinessHours where Name ='Australia']);
    list<Case> listCase= new list<Case>();

    try 
    {
      for(Account ac : accountSoLst) 
      {
         if(ac.Country_Name___c!=oldMap.get(ac.id).Country__c)
            {
                listCase=[Select BusinessHoursId from case where accountid=:ac.id];
                for(Case c:listCase){
                    if(acc.Country_Name__c =='England')
                    {
                        c.BusinessHoursId = BusinessHoursId[1].id;
                    } 
                    else if(ac.Country_Name__c =='austria')
                    {
                         c.BusinessHoursId = BusinessHoursId[0].id;
                    }
                    else if(ac.Country_Name__c =='canda')
                    {
                        c.BusinessHoursId =BusinessHoursId[3].id;
                    }
                    else if(ac.Country_Name__c =='Australia')
                    {
                        c.BusinessHoursId =BusinessHoursId[4].id;
                    }
                    else if(ac.Country_Name__c =='LAN')
                    {
                        c.BusinessHoursId =BusinessHoursId[5].id;
                    }                       
                 }
            update listCase;                             
           }
        }
    }
    catch(Exception e){
            system.debug ('error: ' + e.getMessage() );
    }   
  }
}

UPDATE: This is what I have tried:

@isTest public class Test_setCountry { 
  public static testMethod void testbusinesshours(){ 
    Profile p = [SELECT Id FROM Profile WHERE Name='Customer Service']; 
    user u = new user(Alias = 'customer', 
                      Email='customerser@testorg.com', 
                      LastName='Testing', 
                      LanguageLocaleKey='en_US',
                      ProfileId = p.Id, 
                      UserName='customerser@testorg.com'); 
    insert u;

    test.startTest();
    BusinessHours bh = [select Id from BusinessHours]; 
    Account acc = new Account(name='Test');
    insert acc;
  }
}

Update: ..but it covers only 48% can anyone please help me take to 80%...

@isTest 
public class TestSetcountry {
    Public static testMethod Void validatesetcountry() {
        Account acc = new Account();
        acc.name = 'test';
        acc.GC_Mobile_Phone__c = '900015155';
        acc.GC_Mailing_Country_Name_Txt__c = 'US';
        insert acc;
        list accountSoLst = new list();
        accountSoLst.add(acc);
        Map oldMap = new Map();
        oldmap.put(acc.id, acc);
        System.assertEquals(false, acc.Id == null);
        List BusinessHoursId = new List();
        BusinessHoursId.add([Select Id from BusinessHours where Name = 'US']);
        case cas = new
        case ();
        cas.AccountId = acc.Id;
        cas.First_Pass__c = 'No';
        cas.Status = 'New';
        insert cas;
        System.assertEquals(false, cas.Id == null);
        acc.GC_Mailing_Country_Name_Txt__c = 'US';
        update acc;
        upsetCountry.handleAfterUpdate(accountSoLst, oldMap);
    }
} // This is just a sample script. Paste your real code (javascript or HTML) here.

if ('this_is' == /an_example/) {
    of_beautifer();
} else {
    var a = b ? (c % d) : e[f];
}
2
  • 2
    What have you already tried? Did you start writing the test case? Perhaps, you should have a look at developer.salesforce.com/page/… and at least give it a try. Once you have a draft and face any issues with it, let us know and then we can probably help to fix. Apr 16 '15 at 10:01
  • Lets start at the basics: do you understand what you want to test in your code ? Apr 24 '15 at 9:23
3

Your trigger only fires after update, while in your test case you only insert an account. You will have to update it to make sure your class is tested.

Also, make sure you make use of the System.assert() and/or System.assertEquals functions. If you don't, you're not actually testing whether you get back the expected outputs.

Lastly, you will need to create a Case for your inserted Account as well. Your code makes use of Cases, so you will need to test this functionality.

1
  • 1
    Also test the scenario where Country_Name__c = 'LAN'. In the code shown in the question an "Index out of bounds exception" will be thrown because of the BusinessHoursId[5] call
    – BarCotter
    Apr 16 '15 at 11:27
1

See @KoenWesselman's answer for why your test isn't working.

There is another issue in your code which may cause you issues when you try to do bulk updates. You are doing a SOQL query inside a loop which means you will hit a SOQL limit very quickly. See below for an (untested) example of how you could bulkify your code. I also moved the BusinessHours into a Map to avoid the need for all the if statements, this assumes that the Account.Country_Name___c will match the BusinessHours.Name. I also changed the method signature so that I could use newMap in the code.

public class AccountTriggerHelper {

 public static void handleAfterUpdate(Map<Id,Account> newMap, Map<Id,Account> oldMap) {

   Map<String, BusinessHours> businessHoursMap = new Map<String, BusinessHours>();
   for (BusinessHours bh : ([Select Id from BusinessHours]) {
       businessHoursMap.put(bh.Name, bh);
   }


    try {
      List<Id> accountIds = new List<Id>();
      for(Account ac : newMap.values()) {
         if(ac.Country_Name___c != oldMap.get(ac.id).Country__c) {
             accountIds.add(ac.Id);
         }
      }
      List<Case> cases = new List<Case>();
      for (Case c : [Select BusinessHoursId from Case where accountId in :accountIds]) {
          BusinessHours bh = businessHoursMap.get(newMap.get(c.accountId).Country_Name___c);
          c.BusinessHoursId = bh.Id;
          cases.add(c);
      }
      update cases;                             
     } catch(Exception e){
        system.debug ('error: ' + e.getMessage() );
     } 
   }  
}
1
  • I am writing test class like follwing ..but it covers only 48% can anyone please help me take to 80%... Apr 24 '15 at 6:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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