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
sfdc rocks 24sfdc rocks 24 

Need Help in test class for interface Method

Hi 
I am newbie in salesforce development. Can someone help me with the test class for the below interface method .

public with sharing class DebugMain {
    
    public interface IMethodEntry
    {
        void MainEntry(String mainObject);
        void InProgressEntry(String mainObject);
    }

    public static IMethodEntry activefunction = null;
    
    public static Boolean FakeException = false;

    public static void MainEntry(String mainObject)
    {   
        DebugHelper.Push('MainEntry mainObject: ' + mainObject);
        try
        {
            if(fakeexception && activefunction==null ) {
                activefunction.InProgressEntry(mainObject);
            }                        
            if(activefunction != null)  
            {
                activefunction.InProgressEntry(mainObject);
                return;
            }   
        
            DebugHelper.Pop();
        }
        catch(Exception ex)
        {
            DebugHelper.DebugException(ex);
            //DebugHelper.DebugException(ex, mainObject);
            DebugHelper.PopAll();   
        }
    }    
}

Some body help me to improve the code coverage and how to pass the activefunction value in to the test class
@isTest
public class DebugMain_Test
{
    
    static testmethod void DebugMain123()
    {          
        //DebugMain d = new DebugMain();
       // DebugMain.IMethodEntry inf = new DebugMain.IMethodEntry();
       // inf.activefunction != null ;        
        //DebugMain.activefunction = ; 
        DebugMain.MainEntry('Account');
        DebugHelper.push('Account');        
        
        LMNX_VF_ServiceReport lsv = new LMNX_VF_ServiceReport();
        lsv.MainEntry('LMNX_VF_ServiceReport');
        lsv.InProgressEntry('LMNX_VF_ServiceReport');
         DebugMain.MainEntry('LMNX_VF_ServiceReport');
         DebugHelper.Pop();          
    }
}
RazaRaza
Hi sfdcrock24
The interface itself will have 0% coverage. This is because there are no lines to cover; 
interfaces can't be directly executed, so they can't be directly tested. This doesn't affect the '75%' rule,
because it adds zero lines of covered AND uncovered code.
 
B) You can indeed create an interface without a class. This file is built the same as a class (e.g. "public interface MyInterfaceName"). Coverage for the interface will still appear as 0% (0 covered/0 total), but again, this won't affect your package's overall code coverage performance.
 
As a proof of concept, I wrote a simple class that tests itself:
 
  public class OC {
   public interface INTF {
       void asdf();
   }
         public class IC implements INTF {
     public void asdf() {
                }
   }
    
   @isTest
   public static void testOC() {
       OC o1 = new OC(); // This basically has no effect.
       OC.IC o2 = new OC.IC(); // Create a new implementation class.
       o2.asdf();
   }
}
This class, after testing, shows as "100% (1/1)" under code covered. 
The only line highlighted blue is line 7 (public void asdf() ... ). You may accept this as confirmation that the outer class did not have testing requirements, and testing just the implementation classes is sufficient.
Thanks 
Raza
sfdc rocks 24sfdc rocks 24
Hi Raza,
Thank you for your response. I saw you already provided this solution in other threads.

But , in my class , Interface is declared as static type and that type is deciding the interface method calling. Please see the below code where activefunction is deciding the method. Can you provide how to cover the below code.
how to pass the value to activefunction which is a interface method reference.

if(activefunction != null)  
            {
                activefunction.InProgressEntry(mainObject);
                return;
            } 
          

Thank you,