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
JesseAJesseA 

Test Method not testing trigger when trying to deploy to production, but tests 100% in sandbox

Keep getting the error when trying to deploy to production via eclipse: "Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required"

 

For some reason the test method I wrote today doesnt seem to run in production. At first I thought maybe it was the queries not returning anything, but I ran them in developer console and they all ran fine. 

 

Trigger:

trigger Contact on Contact (before delete) {
    if(trigger.isBefore && trigger.isDelete){
        Contact_Delete.beforeDelete(trigger.old);
    }
}

 Test Class:

@isTest(SeeAllData=true)
private class ContactMethodsTest {
	private static testmethod void beforeDeleteTEST(){
		User username = [SELECT Id, ProfileId FROM User WHERE Username LIKE 'editedUserName%' LIMIT 1];
		
		Account a = new Account(Name='beforeDeleteTEST');
		Contact c = new Contact(lastName='lastName', AccountId=a.Id);
		system.runAs(username){		
			insert a;
			insert c;
		}
		
		Profile sysAdminProfile = [SELECT Id FROM Profile WHERE Name = 'System Administrator' LIMIT 1];
		
		User notAdmin = [SELECT Id FROM User WHERE ProfileId !=: sysAdminProfile.Id AND ProfileId !=: username.ProfileId AND isActive = true LIMIT 1];
		
		Test.startTest();
			System.runAs(notAdmin){
				try{
					delete c;
				} catch(System.DMLException e){
					system.debug('THIS IS MESSAGE: ' + e.getMessage());
					 //System.assert( e.getMessage().contains('Cannot delete this Contact'));
				}
			}
		Test.stopTest();
	}
}

 

Best Answer chosen by Admin (Salesforce Developers) 
JesseAJesseA

Got it. It wasnt that my queries were not returning anything it was they were returning something I couldnt use. Specificlally it was my last query trying to find a user who's profile was anything but the two that I was concerned with. Since I was not specifing a profile I did want to use it was finding a profile which didn't have the right permissions to even call the delete. So the delete call was not even being made. 

 

I was able to finally see this by squeezing it into production by just including a "delete c;" call outside of the run as block. Just so it would run the trigger. Then once it was in production I ran the test from the test class and looked through the debug. And instead of seeing an error containing the message I was looking for I saw:

 

|EXCEPTION_THROWN|[20]|System.DmlException: Delete failed. First exception on row 0 with id 0035000001RXsdfAAD; first error: INSUFFICIENT_ACCESS_OR_READONLY, insufficient access rights on object id: []

All Answers

HariDineshHariDinesh

Hi,

 

This happens because your trigger is not called by the test method in production.

Here you have written test method based on some profile names and some conditions.

Those might not met in Production org. Like

Username LIKE 'editedUserName%

 

User WHERE ProfileId !=: sysAdminProfile.Id AND ProfileId !=: username.ProfileId AND isActive = true LIMIT 

 So before moving to the production org modify your code accordingly such that data exist in production also matches and then try to deploy.

JesseAJesseA

I thought that to but I ran the 3 queries in the developer console and each returned back 1 row. I ran it again just now to double check and was able to print out the Id from each result. Is there any reason why a query would work in the developer console and not in the apex test method?

JesseAJesseA

Got it. It wasnt that my queries were not returning anything it was they were returning something I couldnt use. Specificlally it was my last query trying to find a user who's profile was anything but the two that I was concerned with. Since I was not specifing a profile I did want to use it was finding a profile which didn't have the right permissions to even call the delete. So the delete call was not even being made. 

 

I was able to finally see this by squeezing it into production by just including a "delete c;" call outside of the run as block. Just so it would run the trigger. Then once it was in production I ran the test from the test class and looked through the debug. And instead of seeing an error containing the message I was looking for I saw:

 

|EXCEPTION_THROWN|[20]|System.DmlException: Delete failed. First exception on row 0 with id 0035000001RXsdfAAD; first error: INSUFFICIENT_ACCESS_OR_READONLY, insufficient access rights on object id: []

This was selected as the best answer