dimanche 12 janvier 2020

How to unit test java code, which is using Reflections to populate subTypesOf a class?

This is my code:

protected void loadConverters() {
        // load all available converters
        Reflections reflections = new Reflections("com.mypackage.MyClass");

        Set<Class<? extends ContentConverter>> processors = reflections.getSubTypesOf(ContentConverter.class);
        for(Class<? extends ContentConverter> processorClass : processors) {
            try {
                if(Modifier.isAbstract(processorClass.getModifiers())) {
                    continue;
                }

                ContentConverter processor = processorClass.getDeclaredConstructor().newInstance();
                ReportOutputType type = processor.getOutputType();
                if(type == null) {
                    LOGGER.warn("Class {} does not return a valid ReportOutputType, is not being loaded for processing.", processorClass);
                    continue;
                }

                // register
                AVAILABLE_CONVERTERS.put(type, processor);
            } catch (Exception e) {
                LOGGER.error("Cannot instantiate step processor class", e);
            }
        }
    }

So I'm writing the output to a variable AVAILABLE_CONTAINERS. I want to know if there is a way I can mimic the sub classes & conduct unit testing of the above code.





Aucun commentaire:

Enregistrer un commentaire