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
Zen Newman 16Zen Newman 16 

Test Class Producting Zero Code Coverage

Hello, I have a fairly simple apex class that seems to working correctly to remove the html from a rich text field and populate a plain text field with the content. The test class, however, is producing zero code coverage and I'm not sure why. I appreciate any help on where I'm going wrong.

The main class:
public class HTMLStripper {
	@InvocableMethod(label = 'HTML Stripper' description = 'Strips HTML from rich text fields and returns text value') 
    public static void Stripper(List<Id> Ids){
    	List<Opportunity> lstOpp = new List<Opportunity>();
        for(Opportunity opp : [SELECT Id, XDR_Notes__c FROM Opportunity WHERE Id IN : Ids]) { 
            opp.XDR_Notes_Text__c = opp.XDR_Notes__c.replaceAll('<[/a-zAZ0-9&]*>',' ');
            lstOpp.add(opp); 
        }
        if(lstOpp.size()>0){
            update lstOpp;
        }	
    } 
}
And the Test Class:
@isTest
public class HTMLStripperTest {
    static testMethod void HTMLRemoval() {
       //insert test lead
       Account a = new Account(Name = 'Test');
       insert a;  
       Opportunity o = new Opportunity(Name = 'Test', AccountId = a.Id, CloseDate = Date.today(), 
                                       XDR_Notes__c = 'These are test lead notes'); 
       insert o;
        
       //retrieve and test 
       List<Id>lstOppId = new list<Id>{o.Id}; 
       HTMLStripper.Stripper(lstOppId);  
       Boolean result = o.XDR_Notes_Text__c.contains('>'); 
       System.assertEquals(False, result);
       
    }
}


 
Best Answer chosen by Zen Newman 16
ravi soniravi soni
Hi Zen,
Try following test class with 100% code coverage.
@isTest
public class HTMLStripperTest {
    static testMethod void HTMLRemoval() {
       //insert test lead
       Account a = new Account(Name = 'Test');
       insert a;  
       Opportunity o = new Opportunity(Name = 'Test', AccountId = a.Id, CloseDate = Date.today(),
                                       XDR_Notes_Text__c = 'put your notes',
                                       StageName = 'Prospecting',
                                       XDR_Notes__c = 'These are test lead notes'); 
       insert o;
        
       //retrieve and test 
       List<Id>lstOppId = new list<Id>{o.Id}; 
       HTMLStripper.Stripper(lstOppId);  
        Boolean result = o.XDR_Notes_Text__c.contains('>'); 
       System.assertEquals(False, result);
       
    }
}
let me know if it helps you and marking it as best.
Thank you
 

All Answers

AbhinavAbhinav (Salesforce Developers) 
Hi Zen,

Wanted to check if its throwing any error?

Thanks!
Suraj Tripathi 47Suraj Tripathi 47
Hi Zen,

Try this test class.
@isTest
public class HTMLStripperTest {
    static testMethod void HTMLRemoval() {
       //insert test lead
       Account a = new Account(Name = 'Test');
       insert a;  
       Opportunity o = new Opportunity(Name = 'Test', AccountId = a.Id, CloseDate = Date.today(), StageName='Closed Won',
                                       XDR_Notes__c = 'These are test lead notes'); 
       insert o;
        
       //retrieve and test 
       List<Id>lstOppId = new list<Id>{o.Id}; 
       HTMLStripper.Stripper(lstOppId);
       
    }
}

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 

Thanks and Regards
Suraj Tripathi.
ravi soniravi soni
Hi Zen,
Try following test class with 100% code coverage.
@isTest
public class HTMLStripperTest {
    static testMethod void HTMLRemoval() {
       //insert test lead
       Account a = new Account(Name = 'Test');
       insert a;  
       Opportunity o = new Opportunity(Name = 'Test', AccountId = a.Id, CloseDate = Date.today(),
                                       XDR_Notes_Text__c = 'put your notes',
                                       StageName = 'Prospecting',
                                       XDR_Notes__c = 'These are test lead notes'); 
       insert o;
        
       //retrieve and test 
       List<Id>lstOppId = new list<Id>{o.Id}; 
       HTMLStripper.Stripper(lstOppId);  
        Boolean result = o.XDR_Notes_Text__c.contains('>'); 
       System.assertEquals(False, result);
       
    }
}
let me know if it helps you and marking it as best.
Thank you
 
This was selected as the best answer