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
rajesh kumar 10rajesh kumar 10 

I have a doubt on Test class can that i have written a trigger and i have writen test class for that but it was covering only 40% can any one please solve my problem???

My trigger is   : 

trigger accountApprovalSubmit on Account (after insert) {


  for (Account a : trigger.new) {
  if (a.RecordType_Name__c == 'Plant' && (a.Plant_Type__c == 'New Plant' || a.Plant_Type__c == 'Normal Plant'||
             a.plant_Type__c == 'potential plant') && (a.plant_bussiness_Type__c == 'Aftermarket'||a.plant_bussiness_Type__c == 'New construction')
      && (a.status__c!='active') && (a.super_region__c=='EAME')
      && (a.account_industry_type__c =='Powe-Fossil'||a.account_industry_type__c =='O&G'))
  {
   Approval.ProcessSubmitRequest app = new Approval.ProcessSubmitRequest();
         app.setObjectId(a.id);
         Approval.ProcessResult result = Approval.process(app);
  }
       

    }

}


Below is the TEST CLASS


@isTest
private class accountApprovalSubmit {
    static testMethod void accountApprovalSubmit() {
  Test.startTest();
  Account acc= new Account(name='intelsjr',RecordtypeId='012U0000000Tq1N',Plant_Type__c = 'Potential Plant' ,
                             plant_business_Type__c = 'New Construction' ,status__c = 'inactive',
                             super_region__c='EAME',account_industry_type__c ='O&G');
          insert acc;
     List<ProcessInstance> p = [SELECT Id, (SELECT Id, StepStatus, Comments FROM StepsAndWorkitems)
                                 FROM ProcessInstance where targetObjectId =:acc.id];
      system.assertNotEquals(null,p);
      Test.stopTest();
}
}

It was covering only 40% so please can any one solve my problem..

Thanks in Advance..
lkatneylkatney
Hey ,

I am have confusion with this field 'a.RecordType_Name__c'. I think it is not being inserted in test class.

can you please check that?

James LoghryJames Loghry
First off, there's something wrong with your if statement in your trigger.  I would break it down from there and debug the individual conditions to see which exact condition(s) are failing.  I see a few things that could cause this:
  1. Your trigger is comparing "Plant_Bussiness_Type__c", but you're specifying "Plant_Business_Type__c" in your unit test.  Note the 4 S's in the first field name.  I'm not sure if this was a typo in your trigger / test code, but could be one of the causes to your 40% code coverage.
  2. Record_Type_Name__c - Is this a custom text field or a formula field?  If it's a text field, then it's likely you're not setting this correctly in your unit test.
  3. It shouldn't matter, but to be safe, fix the inconsistencies in your capitalization.  For instance, you capitalize "New Construction" differently between the trigger and test class.
Other items you'll want to consider are:
  1. Query for your RecordTypeId in your test class instead of hard coding it to the Account RecordTypeId field.  It's likely this Id will change between production and sandbox instances at some point and you'll have a fire to put out.  You can query by developer name instead.  For example: 
    Id recordTypeId = [Select Id From RecordType Where DeveloperName='MyRecordTypeDevName'].Id;
  2. Add additional test cases for your negative tests.  For instance, add a test or two for when the logic in your If statement is NOT met.  What should the expected results be when that is the case?
Happy Unit Testing.
AshlekhAshlekh
Hi

Your test class when create account record  then trigger will fire, but tirgger is not able to pass if condation because in test class you have mentioned 

"Plant_Type__c = 'Potential Plant' " and in trigger you are checking "a.plant_Type__c == 'potential plant" so here case sensitive infomation are being testing.

@isTest
private class accountApprovalSubmit {
    static testMethod void accountApprovalSubmit() {
  Test.startTest();
  Account acc= new Account(name='Intelsjr',RecordtypeId='012U0000000Tq1N',Plant_Type__c = 'Potential Plant' ,
                             plant_business_Type__c = 'New Construction' ,status__c = 'Inactive',
                             super_region__c='EAME',account_industry_type__c ='O&G');
          insert acc;
     List<ProcessInstance> p = [SELECT Id, (SELECT Id, StepStatus, Comments FROM StepsAndWorkitems)
                                 FROM ProcessInstance where targetObjectId =:acc.id];
      system.assertNotEquals(null,p);
      Test.stopTest();
}
}
for (Account a : trigger.new) {
  if (a.RecordType_Name__c == 'Plant' && (a.Plant_Type__c == 'New Plant' || a.Plant_Type__c == 'Normal Plant'||
             a.plant_Type__c == 'Potential Plant') && (a.plant_bussiness_Type__c == 'Aftermarket'||a.plant_bussiness_Type__c == 'New Construction')
      && (a.status__c!='active') && (a.super_region__c=='EAME')
      && (a.account_industry_type__c =='Powe-Fossil'||a.account_industry_type__c =='O&G'))
  {
   Approval.ProcessSubmitRequest app = new Approval.ProcessSubmitRequest();
         app.setObjectId(a.id);
         Approval.ProcessResult result = Approval.process(app);
  }
       

    }

}

I didn't use equalsIgnoreCase but you can use this function to check the value irrespect of case sensective.


IF it helps you than please mark it as a solution and ENJOY APEX