How to test components tagged with @OpenmrsProfile(modules = {...})

Hi all, I’m currently working on this ticket. I’ve created component that wraps some methods from metadataMapingService only when metadataMapping module is available using the @OpenmrsProfile annotation. I’m wondering how could I load this component to test htmlFormEntry with metadataMapping module present ?

@OpenmrsProfile uses ModuleFactory.getStartedModules() to find out which modules are running. If you want to test conditional loading based on loaded modules, you can just call ModuleFactory.getStartedModulesMap().put(module.getModuleId(), module); passing a mocked module with the id and version you declared in OpenmrsProfile…

You need to call the put method before Spring context is initialized e.g. in a test constructor. Do not forget to clean up by removing the module after your test class calling ModuleFactory.getStartedModulesMap().remove(module.getModuleId()). You can use method annotated with @AfterClass for that purpose.

1 Like