1

Trigger:

Trigger copyEquipmentonsite on Unit_Placements_Sales__c(after insert, after update)
{
     List<Services__c> sub=new List<Services__c>();
     for(Unit_Placements_Sales__c u : Trigger.new)
     {
           if(u.Service_Month_is_Next_Month__c == TRUE)
           {
                   Services__c s=new Services__c();

                   s.Equipment_Onsite__c=u.ID;
                   s.Equipment_Type__c=u.Equipment__c;
                   s.Machine_Specific_Notes__c=u.Machine_Specific_Notes__c;   
                   s.Product__c=u.The_Product__c;  
                   s.Dell_Tag_Number__c=u.Dell_Tag_Number__c;
                   s.Serial_Number__c=u.Serial_Number__c; 
                   s.XD_Number__c=u.XD_Number__c;

                   sub.add(s);
            }
            if(sub.size()>0)
            insert sub;
     }
}

The trigger works fine in my sandbox and I have written the below test class which passes in sandbox also. However,when I upload the trigger to production and try to validate I get the following error

copyEquipmentonsite; null, Details: Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required; Average test coverage across all Apex Classes and Triggers is 0%, at least 75% test coverage is required.

Test Class:

@isTest
Public Class TestClass{
      public static testMethod void myTest(){
              Unit_Placements_Sales__c u= new Unit_Placements_Sales__c();

              u.Owner__c = '001g000000KLK4Q';
              u.Unit__c = 'a00g0000005nlPL';
              u.Service_Month_is_Next_Month__c = TRUE;
              insert u;
       }
}

Can some advise what I am doing wrong here? I am very new to apex so I am not sure if I need to upload my test class to production as well as the trigger?

Any help on getting the required 75% is appreciated.

2
  • When you deploy, you should be sending up the trigger and the test class at the same time. If you don't send the test code, you trigger will not be covered. Does this test class work if you Run Tests in the sandbox? Commented Aug 19, 2014 at 12:28
  • @mast0r Hi Jeremy, Yes the test class works in sandbox. When I "Run Test" from the test class page, It Passes.
    – GMan
    Commented Aug 19, 2014 at 12:38

1 Answer 1

4

My first suggestion is to slow down a bit and read up on some of the Trigger Best Practices to get an idea of some things you can do differently with the Trigger in the future. With that said, let's get the unit tests fixed first:

As a new developer, expect to be reading a ton of documentation:

Take your time and review all of that in detail. There are 3 main pieces to every unit test in Salesforce.

  1. Generate test data
  2. Perform test logic
  3. Assert logic

So, let's start with that:

@isTest
public Class TestUnitPlacementSalesTriggerClass{
    public static testMethod void testAfterInsert(){
        // Generate test data

        // Perform test logic

        // Assert logic
    }
}

From here, let's start adding the test data. You already have that done, but we need to change it because you should never hard code IDs in your code. They will change when you deploy and your unit tests will fail. Also, whenever you are testing a Trigger, you need to do so with more than one record in order to test for bulk scenarios.

@isTest
public Class TestUnitPlacementSalesTriggerClass{
    public static testMethod void testAfterInsert(){
        Unit__c unit = new Unit__c();
        insert unit;
        List<Unit_Placements_Sales__c> unitPlacementSales = new List<Unit_Placements_Sales__c>();
        for(Integer i = 0; i < 21; i++){
            unitPlacementSales.add(new Unit_Placements_Sales__c(
                Owner__c = UserInfo.getUserId(),
                Unit__c = unit.Id,
                Service_Month_is_Next_Month__c = true
            ));
        }

        // Perform test logic

        // Assert logic
    }
}

Now that we are ready to perform our insert logic, let's make sure we wrap the actual test code in Test.startTest(); and Test.stopTest();. This will allow us to properly test our data limits.

@isTest
public Class TestUnitPlacementSalesTriggerClass{
    public static testMethod void testAfterInsert(){
        Unit__c unit = new Unit__c();
        insert unit;
        List<Unit_Placements_Sales__c> unitPlacementSales = new List<Unit_Placements_Sales__c>();
        for(Integer i = 0; i < 21; i++){
            unitPlacementSales.add(new Unit_Placements_Sales__c(
                Owner__c = UserInfo.getUserId(),
                Unit__c = unit.Id,
                Service_Month_is_Next_Month__c = true
            ));
        }

        Test.startTest();
        insert unitPlacementSales;
        Test.stopTest();

        // Assert logic
    }
}

Finally, we need to assert something to verify our test worked. Each record we inserted should have generated a Services__c record. We should check as much information as possible to verify our code worked the way we expected, but I will go simplistic here for the sake of example.

@isTest
public Class TestUnitPlacementSalesTriggerClass{
    public static testMethod void testAfterInsert(){
        Unit__c unit = new Unit__c();
        insert unit;
        List<Unit_Placements_Sales__c> unitPlacementSales = new List<Unit_Placements_Sales__c>();
        for(Integer i = 0; i < 21; i++){
            unitPlacementSales.add(new Unit_Placements_Sales__c(
                Owner__c = UserInfo.getUserId(),
                Unit__c = unit.Id,
                Service_Month_is_Next_Month__c = true
            ));
        }

        Test.startTest();
        insert unitPlacementSales;
        Test.stopTest();

        List<Services__c> services = [SELECT Id FROM Services__c WHERE Equipment_Onsite__c IN :unitPlacementSales];
        System.assert(services.size > 0, 'There should be a Services__c record generated for every UnitPlacementSale');
    }
}

There you go. That tests half of your Triggers functionality. You will need to write an additional test for after insert. You will also want to consider refactoring your Trigger with the best practices I linked to above.


Note: All of the above code was written purely on SFSE. It may or may not compile. I highly suggest taking some time to rework it yourself. Please do not just copy and paste.


EDIT: As noted above, please make sure you include your test class in your deploy. If you don't, your code will never be able to be deployed to production as the unit tests need to be run on production before the code is finally committed.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .