Writing a single unit test for multiple implementations of an interface

后端 未结 7 1702
感动是毒
感动是毒 2020-12-23 11:26

I have an interface List whose implementations include Singly Linked List, Doubly, Circular etc. The unit tests I wrote for Singly should do good for most of Do

7条回答
  •  滥情空心
    2020-12-23 11:58

    Based on the anwser of @dasblinkenlight and this anwser I came up with an implementation for my use case that I'd like to share.

    I use the ServiceProviderPattern (difference API and SPI) for classes that implement the interface IImporterService. If a new implementation of the interface is developed, only a configuration file in META-INF/services/ needs to be altered to register the implementation.

    The file in META-INF/services/ is named after the fully qualified class name of the service interface (IImporterService), e.g.

    de.myapp.importer.IImporterService

    This file contains a list of casses that implement IImporterService , e.g.

    de.myapp.importer.impl.OfficeOpenXMLImporter

    The factory class ImporterFactory provides clients with concrete implementations of the interface.


    The ImporterFactory returns a list of all implementations of the interface, registered via the ServiceProviderPattern. The setUp() method ensures that a new instance is used for each test case.

    @RunWith(Parameterized.class)
    public class IImporterServiceTest {
        public IImporterService service;
    
        public IImporterServiceTest(IImporterService service) {
            this.service = service;
        }
    
        @Parameters
        public static List instancesToTest() {
            return ImporterFactory.INSTANCE.getImplementations();
        }
    
        @Before
        public void setUp() throws Exception {
            this.service = this.service.getClass().newInstance();
        }
    
        @Test
        public void testRead() {
        }
    }
    

    The ImporterFactory.INSTANCE.getImplementations() method looks like the following:

    public List getImplementations() {
        return (List) GenericServiceLoader.INSTANCE.locateAll(IImporterService.class);
    }
    

提交回复
热议问题