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
Jose Boveda 6Jose Boveda 6 

Trigger won't deploy to production even though test coverage shows 100%

Hi folks,
I developed a trigger for my production environment in the Eclipse IDE. When I go to save it to my environment, it shows an error that code coverage is 0%. I've developed a test class for this trigger, and when I run the test from the IDE, it shows test coverage for my trigger at 100% (0 lines not tested).

What gives? Is there another way to get my trigger to save onto my environment? I tried using the Development Console but that didn't work due to the 'Metadata can't be changed on an active environment' bug.

Any help is appreciated.

Trigger:
trigger Account_UpdateAccountInterestsList on Interest__c (after delete, after insert, after undelete, 
after update) {
	Interest__c[] interests;
    if (Trigger.isDelete) 
        interests = Trigger.old;
    else
        interests = Trigger.new;
	Set<ID> affected_accounts = new Set<ID>();
	Map<ID, List<String>> account_interests = new Map<ID, List<String>>();
	for(Interest__c interest: interests){
		affected_accounts.add(interest.Account__c);
		account_interests.put(interest.Account__c, new List<String>());
	}

	List<Interest__c> all_interests = [
		SELECT Id, Account__c, Interest__r.Name
		FROM Interest__c
		WHERE Account__c IN :affected_accounts
		ORDER BY Interest__r.Name
	];
	List<Account> accounts_to_update = [
		SELECT Id, Account_Interests_List__c
		FROM Account
		WHERE Id IN :affected_accounts
	];
	
	for(Interest__c interest: all_interests){
		account_interests.get(interest.Account__c).add(interest.Interest__r.Name + '\n');
	}
	for(Account account: accounts_to_update){
		account.Account_Interests_List__c = Util_TMCR.combineList(account_interests.get(account.Id));
	}
	
	update accounts_to_update;
}


Test class:
@isTest
private class TMCR_testAccountInterestList {
  /*
    Testing Account_UpdateAccountInterestsList trigger
  */
  static Account getAccount(){
    Util_TMCR_DataFactory.accounts account = new Util_TMCR_DataFactory.accounts();
    Account acc = [
      SELECT Id, Account_Interests_List__c
      FROM Account WHERE Name = :account.get('name')
    ];
    return acc;
  }
  static Interests__c addInterest(String name){
    Interests__c new_interest = new Interests__c(Name = name);
    insert new_interest;
    return new_interest;
  }
  static Interest__c addToAccount(Account acc, Interests__c interest){
    Interest__c acc_interest = new Interest__c(Interest__c = interest.Id, Account__c = acc.Id);
    insert acc_interest;
    return acc_interest;
  }
  
  @testSetup
  static void setup(){
    List<Interests__c> interests = new List<Interests__c>();
    List<Interest__c> account_interests = new List<Interest__c>();
    for(Integer i=0; i<3;i++){
      interests.add(addInterest('Interest' + i));
    }
    Util_TMCR_DataFactory.accounts account = new Util_TMCR_DataFactory.accounts();
    Account acc = account.generate();
    for(Interests__c interest: interests){
      account_interests.add(addToAccount(acc, interest));
    }
  }
  
    static testMethod void test_addInterest() {
    test.startTest();
    Account account = getAccount();
    Interests__c interest = addInterest('foo');
    Interest__c acc_interest = addToAccount(account, interest);
    test.stopTest();
    
    String concat = Util_TMCR.combineList(new List<String>{'foo', 'Interest0', 'Interest1', 'Interest2'});
    Account acc = getAccount();
    System.assertEquals(acc.Account_Interests_List__c, concat);
    }
    static testMethod void test_changeInterest() {
      test.startTest();
    Interests__c interest = addInterest('foo');
    Interest__c acc_interest = [SELECT Id, Interest__c from Interest__c WHERE Interest__r.Name = 'Interest0' LIMIT 1];
    acc_interest.Interest__c = interest.Id;
    update acc_interest;
      test.stopTest();
      List<String> mylist = new List<String>{'foo', 'Interest1', 'Interest2'};
      String concat = Util_TMCR.combineList(mylist);
      Account acc = getAccount();
      System.assertEquals(acc.Account_Interests_List__c, concat);
    }
    static testMethod void test_deleteInterest(){
      test.startTest();
    Interests__c interest = [SELECT Id, Name FROM Interests__c WHERE Name = 'Interest0' LIMIT 1];
    delete interest;
      test.stopTest();
      List<String> mylist = new List<String>{'Interest1', 'Interest2'};
      String concat = Util_TMCR.combineList(mylist);
      Account acc = getAccount();
      System.assertEquals(acc.Account_Interests_List__c, concat);
    }
    static testMethod void test_undeleteInterest(){
      test.startTest();
    Interests__c interest = [SELECT Id, Name FROM Interests__c WHERE Name = 'Interest0' LIMIT 1];
    delete interest;
    undelete interest;
      test.stopTest();
      List<String> mylist = new List<String>{'Interest0', 'Interest1', 'Interest2'};
      String concat = Util_TMCR.combineList(mylist);
      Account acc = getAccount();
      System.assertEquals(acc.Account_Interests_List__c, concat);
    }
}


 
Best Answer chosen by Jose Boveda 6
Jose Boveda 6Jose Boveda 6
I posted this in another question (since it's off-topic for this one) and got a working answer:
https://developer.salesforce.com/forums/ForumsMain?id=906F00000005FOqIAM

For reference: the issue was solved by deploying to a dev environment first. That revealed a bug in my code (code coverage was 100% but one of the tests was failing and wasn't output). It's been corrected.

All Answers

Beau Gordon 6Beau Gordon 6
Hi,

Try deploying the test class first on its own.  After you do that and verify that it is deployed to your production instance, then try deploying the trigger.

Another option is to try deploying using a change set.  Make a change set in your sandbox containing both files and then deploy it to production.

I know neither of those things directly tackles the issue that you describe but maybe they will help you work around it.
 
Ketankumar PatelKetankumar Patel
Add your trigger and test class in change set and try deploying using change set. You should also add Util_TMCR_DataFactory class in your deployment. 
Jose Boveda 6Jose Boveda 6
I managed to get them onto a test instance, and on that test instance I'm finding the assertions are failing even though they show the same information. Example:
Class.TMCR_testAccountInterestList.test_addInterest: line 51, column 1
14:18:05.697 (6697124228)|FATAL_ERROR|System.AssertException: Assertion Failed: Expected: foo
Interest0
Interest1
Interest2
, Actual: foo
Interest0
Interest1
Interest2

I converted both strings to hex values using this function:
EncodingUtil.convertToHex(Blob.valueOf(concat));
And find that the values are indeed different (they match except for the expected value (concat) has two extra characters at the end of the hex value.

Anyone know what gives here? I may post this separately

For reference, here is the function that is converting the list of strings to a concatenated string:
    public static String combineList(List<String> items){
        String out = '';
        String delimiter = '\n';
        for(String val : items){
        	out = out + val;
        	if(val.right(1) != delimiter){
        		out = out + delimiter;
        	}
        }
        return out;
    }


 
Beau Gordon 6Beau Gordon 6
In your code you have this line:

account_interests.get(interest.Account__c).add(interest.Interest__r.Name + '\n');

which ends with a trailing newline /n.

But the test class does this:

String concat = Util_TMCR.combineList(new List<String>{'foo', 'Interest0', 'Interest1','Interest2'});

which doesn't have a trailing newline.
Jose Boveda 6Jose Boveda 6
@beau gordon
I wish I could update the code in my original post. This has been removed. The problem is still current.
Jose Boveda 6Jose Boveda 6
I posted this in another question (since it's off-topic for this one) and got a working answer:
https://developer.salesforce.com/forums/ForumsMain?id=906F00000005FOqIAM

For reference: the issue was solved by deploying to a dev environment first. That revealed a bug in my code (code coverage was 100% but one of the tests was failing and wasn't output). It's been corrected.
This was selected as the best answer