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
Abraham kumarAbraham kumar 

Test class not having enough code coverage

HI All,

Please help me for test class for the below Trigger im getting just 62% coverage
trigger ContactTrig on Account(after update) {
List<Contact> ListContact = new List<Contact>();
Set<ID> acctIds = new set<ID>();
    for(Account acct : trigger.new) {
        if(Trigger.oldMap.get(acct.id).Name != acct.Name){
             acctIds.add(acct.Id);
        }
    }
    if(acctIds.size() > 0){
        for(Contact con : [SELECT id,Email,FirstName,LastName,phone,Title__c,Account.Name,status__c, AccountId FROM Contact WHERE AccountId IN : acctIds AND RecordTypeid = '034D0000000CVERt'])
          WebServiceCallout.sendNotification(con.Id,con.Email,con.FirstName,con.LastName,con.phone,con.Title__c,con.Account.Name,con.status__c);
        }
    }
}
My Test class is as below please let me know what im missing 
@istest
public class ContactTrig2TestClass{
 static void Accountcreate(){
Account c =new Account();
c.Name = 'Testcon';
c.Type = 'Agency';
insert c;
c.Name = 'Testcontract';
update c;
}
 }



 
Best Answer chosen by Abraham kumar
Shyama B SShyama B S
Hi,
Please try after adding @istest for the method Accountcreate like this:
@istest
public class ContactTrig2TestClass{
    @istest
    static void Accountcreate(){
        Account c =new Account();
        c.Name = 'Testcon';
        c.Type = 'Agency';
        insert c;
        c.Name = 'Testcontract';
        update c;
	}
 }

Please let me know if you face any issues and mark this answer if it helped you.
Thanks
Shyama

All Answers

Shyama B SShyama B S
Hi,
Please try after adding @istest for the method Accountcreate like this:
@istest
public class ContactTrig2TestClass{
    @istest
    static void Accountcreate(){
        Account c =new Account();
        c.Name = 'Testcon';
        c.Type = 'Agency';
        insert c;
        c.Name = 'Testcontract';
        update c;
	}
 }

Please let me know if you face any issues and mark this answer if it helped you.
Thanks
Shyama
This was selected as the best answer
Abraham kumarAbraham kumar
Thanks soo much shyama Azesome.. :)
This worked
Just would like to know how by just adding @istest to method is it working? while other test classes work without it ...
@istest
public class ContactTrig2TestClass{
@istest
static void Accountcreate(){
Account a =new Account();
a.Name = 'abc';
a.Type = 'Agency';
a.ShippingStreet = 'test';
a.ShippingCity = 'test';
a.ShippingPostalCode = 'tw31aq';
insert a;
a.Name = 'abcd';
update a;
contact c =new contact();
c.Email = 'abc@wipro.com';
c.Salutation = 'Mr';
c.FirstName = 'Abraham';
c.LastName = 'Daniel';
c.type__c = 'Agency';
c.status__c= 'Active';
insert c;
    }
    }

Many Thanks
Abraham
Shyama B SShyama B S
Glad it worked. :)
You can declare test methods in two ways:
@isTest 
static void testName() {
    // code
}
or 
static testMethod void testName() {
    // code
}

So, if you are using the testMethod keyword, you dont require the @istest annotation. Hope this answered your query.
Thanks.