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
T.RossiT.Rossi 

testing code coverage for Interface

I have a simple class like this:

public class OuterClass {    


    public virtual interface MyInterface{
    
        Map<String, List<String>> method1(Blob b);
        
        Map<String, Map<String, List<String>>> method2(Map<String, Blob> m);   
        
        void method3(String external_id); 
    
    }
     
    
}

 I then have another class which implements this Interface. The implementation class can be tested correctly with code covarage, but this class (OuterClass) always remains with 0% code coverage.

 

2 questions:

A- how can I test my interface?

B- is it possible to create an Interface without the OuterClass?

 

Any help is welcome, thanks in advance,

T.

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

A) 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.

All Answers

sfdcfoxsfdcfox

A) 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.

This was selected as the best answer
T.RossiT.Rossi

thank you very much! I agree it wouldn't make a lot of sense to test an interface:)