+ Start a Discussion
ImagepathImagepath 

Apex Trigger Testing - Can't deploy!

Hello,

I'm trying to deploy a very simple trigger that will fire off an email when a contact is deleted from an Account.  it works fine in the sandbox, but whenever I try to deploy to production I get:

 "Average test coverage across all Apex Classes and Triggers is 38%, at least 75% test coverage is required"

I have created a tester class for the trigger - here is the code for the tester class:

 

@isTest

private class TesterClass {

 

    static testMethod void myUnitTest() {

        // TO DO: implement unit test

}

    public static String getGreeting(){

return ('Test Away!')

}

static testmethod void testGreeting(){ 

String gr = TesterClass.getGreeting();

System.assertEquals('Test Away!',gr);

}

public static Double getNum(Double x, Double y){

return (x+y);

}

static testmethod void testNum(){

Double z = TesterClass.getNum(1, 2);

System.assertEquals(3,z);

}

static testMethod void myTest() {

     Account a = new Account(name='foo');

     Contact c = new Contact(firstname='John', lastname='Doe');

     insert a;

     insert c;

     System.assertEquals('foo', [select name from Account where id=:a.id].name);

     System.assertEquals(1, [select count() from Account where id=:a.id]);

     System.assertEquals('John', [select firstname from Contact where id=:c.id].firstname);

     try {

       delete a;

       delete c;

       TesterClass.testNum(); //call some method

    

     catch (DmlException e) {

       System.assert(true);  // assert that we should never get here

     }

    }

 

and here is the code for the trigger:

 

trigger contactDelete on Contact (before delete) {

    try {

        for (Contact c : Trigger.old) {

            Account acct = [SELECT Name FROM Account WHERE Id = :c.AccountID];

            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

            String[] toAddresses = new String[] {'tim.andrews@evariant.com'};

            //String[] ccAddresses = new String[] {'smith@gmail.com'};

            mail.setToAddresses(toAddresses);

            mail.setReplyTo('support@evariant.com');

            mail.setSenderDisplayName('Salesforce Support');

            mail.setSubject('Contact Deleted: ' + c.FirstName + ' ' + c.LastName);

            mail.setUseSignature(false);

            mail.setPlainTextBody(

                'Contact ' + c.FirstName + ' ' + c.LastName + ' has been deleted!\n' + 

                'Contact ID: ' + c.Id + '\n\n' +

                'Contact Organization: ' + acct.Name + '\n\n' +

                'Organization ID: ' + acct.Id

                );

            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

        }

    }

    catch (NullPointerException e) {

        Exception e1 = e;

    }

 

 What do I need to do to get this working????  Any help would be appreciated! Thanks!

Message Edited by Imagepath on 02-04-2009 04:55 PM
Message Edited by Imagepath on 02-04-2009 04:58 PM
aalbertaalbert

When I ran your testes in my developer edition org, I got a Query Exception because the Contact you delete in your test method is not associated to an Account. Therefore, the query (Account acct = [SELECT Name FROM Account WHERE Id = :c.AccountID];) threw an exception since it returned 0 rows.

 

I saw this error in the Apex Test Runner output window within the Force.com IDE where I executed the test methods.

ImagepathImagepath

aalbert - I have changed my code now to this:

 

Account a = new Account(name='foo');
     Contact c = new Contact(AccountId=a.Id, firstname='John', lastname='Doe');
     insert a;
     insert c;
     System.assertEquals('foo', [select name from Account where id=:a.id].name);
     System.assertEquals(1, [select count() from Account where id=:a.id]);
     System.assertEquals('John', [select firstname from Contact where id=:c.id].firstname);
     try {
        delete c;
        delete a;
        TesterClass.testNum(); //call some method
     }

 

So that the delete trigger is called before the account is deleted.  However it's still not working.  How do you run the tests in the developer side?  Every time I try to validate in the Eclipse IDE I get the following:

 

Average test coverage across all Apex Classes and Triggers is 38%, at least 75% test coverages is required.

contactDelete (ApexTrigger) -- 11 lines not tested, 21% covered

    Line 5, Column 13 not covered

    Line 6, Column 13 not covered

    Line 7, Column 13 not covered

    Line 8, Column 13 not covered

    Line 9, Column 13 not covered

    Line 10, Column 13 not covered

    Line 11, Column 13 not covered

    Line 12, Column 13 not covered

    Line 18, Column 13 not covered

    Line 21, Column 36 not covered

    Line 22, Column 9 not covered

 

I don't know what this means - I'm sorry if I'm being obtuse, but the documentation for setting up and testing Apex is pretty slim.  I'm really  new to this.

aalbertaalbert

There are details on how to execute test methods from within the standard salesforce.com UI or Force.com IDE here.

 

And the output of the "Run Test" will output the debug information. So if might help to add more System.debug messages to identify why your code is not getting the proper coverage. 

ImagepathImagepath


I finally understand the testing process, but now I'm having a problem where the code to create
and send an email isn't being tested.  It's included in the trigger being called,
but for some reason the code is never tested and I can't figure out why.  



//from the tester class:
test.startTest();
delete c;
test.stoptest();
 
//the trigger being called: 
trigger contactDelete on Contact (before delete) {
        for (Contact c : Trigger.old) {
            Account acct = [SELECT Name, Id FROM Account WHERE Id = :c.AccountID];
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] tAddresses = new String[] {'tim.andrews@evariant.com'};
            mail.setToAddresses(tAddresses);
            mail.setReplyTo('support@evariant.com');
            mail.setSenderDisplayName('Salesforce Support');
            mail.setSubject('Contact Deleted: ' + c.FirstName + ' ' + c.LastName);
            mail.setUseSignature(false);
            mail.setPlainTextBody(
                'Contact ' + c.FirstName + ' ' + c.LastName + ' has been deleted!\n' + 
                'Contact ID: ' + c.Id + '\n\n' +
                'Contact Organization: ' + acct.Name + '\n\n' +
                'Organization ID: ' + acct.Id
                );
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}
Message Edited by Imagepath on 02-11-2009 02:18 PM