Code Coverage for OSGI Configuration
Published
osgi-mock.junit5
for mocking OSGi services and mockito
for dependency injection.pom.xml
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.testing.osgi-mock.junit5</artifactId>
<version>3.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.15.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.15.2</version>
<scope>test</scope>
</dependency>
OsgiContext
from org.apache.sling.testing.osgi-mock.junit5
to test our service implementation. The following unit test will ensure that OSGi configurations are correctly read and applied in AEM projects, providing proper test coverage.AppConfigServiceImplTest.java
public class AppConfigServiceImplTest {
public void testAppConfigService(OsgiContext osgiContext) {
Map<String, Object> properties = new HashMap<>();
properties.put("app.name", "aem-demo");
properties.put("api.endpoint", "https://www.google.com");
properties.put("client.id", "XXX");
properties.put("client.secret", "ZZZ");
AppConfigService appConfigService = osgiContext.registerInjectActivateService(
new AppConfigServiceImpl(), properties);
Assertions.assertEquals("aem-demo", appConfigService.getAppName());
Assertions.assertEquals("https://www.google.com", appConfigService.getAPIEndpoint());
Assertions.assertEquals("XXX", appConfigService.getClientId());
Assertions.assertEquals("ZZZ", appConfigService.getClientSecret());
}
}