0

Overview:

Trigger:

I wrote trigger on account object when ever inserting or updating the account record the the values in the billing address field are copied to shipping address field if the shipping address field is null.

Trigger:

trigger Account_trigger on Account (before insert,before update) {
    if((trigger.isInsert || trigger.isUpdate)&&trigger.isBefore){
        for(Account acc:trigger.new){
        if(acc.ShippingAddress==null){
        acc.ShippingCity = acc.BillingCity;
        acc.ShippingCountry = acc.BillingCountry;
        acc.ShippingLatitude= acc.BillingLatitude;
        acc.ShippingLongitude = acc.BillingLongitude;
        acc.ShippingPostalCode =acc.BillingPostalCode;
        acc.ShippingState=acc.BillingState;
        acc.ShippingStreet = acc.BillingStreet;
        }   
    } 
    } 
}

This trigger is working correctly.

My Requirement:

I trying to write the test class for the above trigger but the test class above code is not covered any percentages. How do I write tests that cover the above code?

Test Class:

public class Trigger_TestCase {
     public static testmethod void myTriggerTestMeth(){

        Account acc1 = new Account();
        acc1.Name='acc1';
        acc1.BillingCity ='Chennai' ;
        acc1.BillingCountry='india';
        acc1.BillingLatitude=5657.577;
        acc1.BillingLongitude=7845.46;
        acc1.BillingPostalCode='600075';
        acc1.BillingState='tamil nadu';
        acc1.BillingStreet='water well street';  
        acc1.ShippingCity=null;
        acc1.ShippingCountry=null;
        acc1.ShippingLatitude=null;
        acc1.ShippingLongitude=null;
        acc1.ShippingPostalCode=null;
        acc1.ShippingState=null;
        acc1.ShippingStreet=null;
        insert acc1;

    }
}
  • 1. Check if the trigger is active or not. 2. Run the test from Developer Console and after the test completion, open the class from Overall Code Coverage window (right-bottom), it will show you which lines are covered. 3. If nothing else works, try to put some debug statements and check the flow. – Santanu Halder Sep 30 '15 at 12:59
  • 1
    Also annotate your test class with @isTest, – C0DEPirate Sep 30 '15 at 13:02
  • @User395 -From your comments below - Make sure you do not have "Store only aggregate code coverage" Setup->Develop->Apex Text Execution -> Option. If it is checked the coverage for individual class will not be stored and will always show 0 – Eric Sep 30 '15 at 17:45
0

From your comment

HI Nilesh, I tried this code but it is not still covering any percentage ,it shows "none" only.

It sounds like you are only storing aggregate code coverage.

Make sure you do not have "Store only aggregate code coverage" Setup->Develop->Apex Text Execution -> Option. If it is checked the coverage for individual class will not be stored and will always show 0

enter image description here

0

Update your test class code with following code - this might work.

public class Trigger_TestCase {
     public static testmethod void myTriggerTestMeth(){

        Account acc1 = new Account();
        acc1.Name='acc1';
        acc1.BillingCity ='Chennai' ;
        acc1.BillingCountry='india';
        acc1.BillingLatitude=5657.577;
        acc1.BillingLongitude=7845.46;
        acc1.BillingPostalCode='600075';
        acc1.BillingState='tamil nadu';
        acc1.BillingStreet='water well street';              
        insert acc1;

    }
}
  • HI Nilesh, I tried this code but it is not still covering any percentage ,it shows "none" only. – Deepan Murugan Sep 30 '15 at 13:03
  • Is it not covering first two line either? 0 %? Because both your and mine code should work. – Salesforce Steps Sep 30 '15 at 13:17
0

Your trigger says "for all accounts that are either being created or updated, if the Shipping Address is blank, then assign the Shipping Address to have the same values of the account Billing Address before they are respectively created or updated".

Your test class says "create an account and have some values for the Billing Address and keep the Shipping Address values as null"...and that's it. There is no test per se in this test class. You will need to add a System.Assert statement.

Try something like this:

private class Test_Class_for_Account_Trigger {

    private static testMethod void myUnitTest() {

    Account acc = new Account(Name = 'Test 123', BillingCity = 'Chennai', BillingCountry = 'India', BillingLatitude = 5657.577, BillingLongitude = 7845.46, PostalCode = '600075', BillingState = 'Tamil Nadu', BillingStreet = 'Water Well Street'); 
    insert acc;

    List<Account> testAccount = [SELECT Id FROM Account WHERE Id =: acc.Id]; 
    System.assert(testAccount.size() == 1); 

    System.assert(acc.ShippingCity == acc.BillingCity);
    System.assert(acc.ShippingCountry == acc.BillingCountry); 



    }
}
  • 1
    Your statement is not entirely true ('thats it'). The OP does insert the account which should activate the trigger and provide some coverage. The addition of system asserts does not help the OP answer their question. While is it certainly true that asserts should be used, it is not relevant to the issue and since that is all your answer added to the OP's test, that is the reason for the down vote. – Eric Sep 30 '15 at 17:41
0

For my own personal benefit, I was under the impression that you had to start and stop the test to get the trigger to cover? Something like

@isTest
    private class Trigger_TestCase{
        Account acc1 = new Account(Name='acc1', BillingCity ='Chennai', BillingCountry='india', BillingLatitude=5657.577, BillingLongitude=7845.46, BillingPostalCode='600075', acc1.BillingState='tamil nadu', BillingStreet='water well street');

    static testMethod void testAccountInsert() {
        init();
        Test.startTest();
        insert acc1;
        Test.stopTest();
        }

    }

I'm not sure about that tho, so I'm asking/answering?

  • 2
    Test.startTest and stopTest provide a new set of governor limits. Additionally you must use them to execute async function during your test. When it hits stopTest async methods are processed and then you can assert Results. Also used for mock interfaces. You do not need the to simply get coverage as in you example – Eric Sep 30 '15 at 17:33

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.