+ Start a Discussion
JP Nikko AM SystemsJP Nikko AM Systems 

Apex Trigger Deployment Fails (Test Coverage 0%)

We created an Apex Trigger on our Sandbox (a before insert, before update trigger on Contact which just basically updates some custom field). Tested it with an Apex class and Code Coverage is 100% (12/12).
But when we deploy it to Production validation fails.

Below is our Apex Class to test.


@isTest
public class TriggerTest {
 static testMethod void verifyTrigger (){
 User u1 = [SELECT Id FROM User WHERE Alias='alias'];
 System.RunAs(u1)
 {
 
    System.debug('Current User: ' + UserInfo.getUserName());
    System.debug('Current Profile: ' + UserInfo.getProfileId());

    List<Contact> contacts = new List<Contact>{};
 
     for(Integer i = 0; i < 1000; i++) {
     Contact a = new Contact(LastName = 'Test'+i, AccountId = 'xxxxxxxxxxxxxxx',Owner=u1 );
     contacts.add(a);
     }

     test.startTest();
     insert contacts; 
      
     test.stopTest();
  }
  
 }

}

Any ideas why? I have tried Compiling all classes. And even Run all from Developer Console but no luck.
Best Answer chosen by JP Nikko AM Systems
BalajiRanganathanBalajiRanganathan
obviously. you have deploy your test class too. Also as i said before you have to make sure user record with 'alias' exist in prod. also you should try to insert a test account in your test code or use @isTest(SeeAllData=true) with account id from prod.

All Answers

BalajiRanganathanBalajiRanganathan
what are you seeing in the debug logs?
1) Do you have user where Alias='alias' in your prod org
2) do you have AccountId = 'xxxxxxxxxxxxxxx' in your prod org
3) check in the debug logs for any validation errors that might be preventing the insert operation in turn preventing your Trigger code.
 
JP Nikko AM SystemsJP Nikko AM Systems
yeah I just used those to mask our data. there were no validation errors too.
JP Nikko AM SystemsJP Nikko AM Systems
oh you were talking about Prod. When deploying I just deploy the trigger. should I deploy the Test Class too?
BalajiRanganathanBalajiRanganathan
obviously. you have deploy your test class too. Also as i said before you have to make sure user record with 'alias' exist in prod. also you should try to insert a test account in your test code or use @isTest(SeeAllData=true) with account id from prod.
This was selected as the best answer
JP Nikko AM SystemsJP Nikko AM Systems
Thanks works now!