I need some help with test class which is failing.Here is my code

trigger striptext on customobj__c (before insert, before update) {
for customobj__c atxt:trigger.new)
if (atxt.Record_Type_Name__c == 'Test')
  if (atxt.Status__c !='')
     atxt.Status_Text__c = atxt.Status__c.stripHtmlTags();

  public class teststriptext  {
    Id RecordTypeIdstatus = Schema.SObjectType.Status__c.getRecordTypeInfosByName().get('Test').getRecordTypeId();

    Status__c teststatus = new Status__c();
    teststatus.RecordTypeId =RecordTypeIdstatus;
    teststatus.Daily_Status__c='<p>This is a test record.</p><p><span style="color: rgb(112, 110, 107);">This is a test record.</span></p><p><br></p>';
    insert teststatus;

    teststatus.Status_Text__c = teststatus.Status__c.stripHtmlTags();
    update teststatus;

    System.assertEquals('This is a test record. This is a test record.',teststatus.Status_Text__c);


  • 3
    Well, how is it failing? What's the error and on which line? Do the assertions pass? If you debug the asserted values, what do you see? – David Reed Jul 11 '18 at 16:05
  • 3
    There is probably spurious trailing or leading space. Call trim() on the strings before you assert their values. – David Reed Jul 11 '18 at 16:27
  • Please edit your question with any clarifications. That's not what comments are for. – Adrian Larson Jul 11 '18 at 17:01

You have this substring:

'...record.</p><p><span style="color: rgb(112, 110, 107);">This...'

Note that there is no whitespace between. That will convert to:


But you set your expected value as:

 '...record. This...'

Just a note, your test would be a lot cleaner if you use constants instead of magic strings. Something like the following would be a lot easier to test:

final String A = 'Some content';
final String B = 'More content';
final String WITH_HTML = '<p>' + A + '</p><p>' + B + '</p>';

MyObject__c record = new MyObject__c(FieldWithHtml__c=WITH_HTML);

    insert record

system.assertEquals(A+B, record.FieldSansHtml__c, 'The html tags should be stripped out');

Additional notes:

  • You should have a separate testMethod for the insert and update case.
  • The above tests only insert. In your update case, make sure you do not set the Status__c field until after insert.
  • Your current code is vulnerable to a NullPointerException. Your if condition should check String.isNotBlank instead of looking only for the empty string.

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.