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
csrsakcsrsak 

Correct me for my testclass for trigger, it is showing 0% test coverage

Hi Folks,  I am new into Salesforce.com; I need to write a test class for trigger, i wrote test class for the following trigger and when i run the test class,  It is showing 0% coverage and the following red color lines are not covered,  Trigger is: trigger insertIntoCustomerContactDetail on Contact (after update)
{
 
for (Contact a : Trigger.new)
     {
       
CustomerContactDetail__c ccd = new CustomerContactDetail__c
        (
           Contact__c = a.id,
           First_Name__c =a.firstName,
           Last_Name__c=a.lastName,
           Date_Of_Birth__c = a.Birthdate,
           Mobile_No__c = a.MobilePhone,
           Email__c = a.Email
         );
         insert ccd ;
      }
 }

 

Test class:

 

@isTestprivate class insertIntoCustomerContactDetailTest {     static testMethod void myUnitTest()     {        Contact a=new Contact();                        //a.id='a018000000McFcK';                        a.firstName='srinu';                        a.lastName='reddy';                        a.Birthdate=Date.newInstance(1984,10,11);                        a.MobilePhone='9711099217';                        a.Email='svreddych@gmail.com';                        try                        {                        update a;                                             CustomerContactDetail__c ccd=new CustomerContactDetail__c                        (                        Contact__c=a.id,                        First_Name__c=a.firstName,                        Last_Name__c=a.lastName,                        Date_Of_Birth__c=a.Birthdate,                        Mobile_No__c=a.MobilePhone,                        Email__c=a.Email                        );                        insert ccd;                        }                                                catch(system.DmlException e)                        {                                    System.debug('we caught a dml exception: ' + e.getDmlMessage(0));                            }    }

}

 

please help me, in this regards.... in correct way.

 

Thanks in Advance, 

 

Srinivas Chittela

IanRIanR

Hi,
 
If you create a new instance of an Account object, you need to insert it before you can update it...
 
 
try something like:
 
 

@isTestprivate class insertIntoCustomerContactDetailTest { static testMethod void myUnitTest() { Contact a=new Contact(); //a.id='a018000000McFcK'; a.firstName='srinu'; a.lastName='reddy'; a.Birthdate=Date.newInstance(1984,10,11); a.MobilePhone='9711099217'; a.Email='svreddych@gmail.com'; try { insert a; // Make a nominal change to the object a.Email='svreddych@gmail.com'; // Now update it to test the trigger... update a; CustomerContactDetail__c ccd=new CustomerContactDetail__c(Contact__c=a.id, First_Name__c=a.firstName, Last_Name__c=a.lastName, Date_Of_Birth__c=a.Birthdate, Mobile_No__c=a.MobilePhone, Email__c=a.Email); insert ccd; } catch(system.DmlException e) { System.debug('we caught a dml exception: ' + e.getDmlMessage(0)); } }}

 

HTH :)
csrsakcsrsak

Hi Ian,

 

Thank you very much for quick reply and the test class shows 100% coverage.

 

Thanks again for great help...

 

Thanks and Regards,

 

Srinivas Chittela

Message Edited by csrsak on 08-27-2009 02:06 AM