我可以使用具有声明性服务注释的OSGi模拟

问题描述 投票:1回答:1

我正在尝试使用Declaratice Services注释(org.osgi.service.component.annotations)测试OSGi服务。我根据AEM Multi-Project Example生成了我的项目。

public class PostServiceTest {

  @Rule
  public AemContext context = new AemContext((AemContextCallback) context -> {
    context.registerInjectActivateService(new PostService());
  }, ResourceResolverType.RESOURCERESOLVER_MOCK);

  @Test
  public void shouldFetchRandomPosts() {
    final PostService postsService = context.getService(PostService.class);
    final List<Post> posts = postsService.randomPosts(100);

    assertEquals(100, posts.size());
  }

}

每当我在IntelliJ中运行此测试时,OSGi Mocks都会抱怨测试类中缺少SCR元数据。

org.apache.sling.testing.mock.osgi.NoScrMetadataException: No OSGi SCR metadata found for class com.example.PostServiceTest
  at org.apache.sling.testing.mock.osgi.OsgiServiceUtil.injectServices(OsgiServiceUtil.java:381)
  at org.apache.sling.testing.mock.osgi.MockOsgi.injectServices(MockOsgi.java:148)
  at org.apache.sling.testing.mock.osgi.context.OsgiContextImpl.registerInjectActivateService(OsgiContextImpl.java:153)
  at org.apache.sling.testing.mock.osgi.context.OsgiContextImpl.registerInjectActivateService(OsgiContextImpl.java:168)
  at com.example.PostServiceTest.shouldReturnTheEnpointNamesWithValidConfigurationAsTheListOfAcceptableKeys(PostServiceTest.java:23)

这是否意味着我只能测试使用Apache Felix附带的旧SCR注释注释的类? OSGi Mocks suggests that Declarative Services annotations is supported in version 2.0.0 and higher的文档。我使用的版本符合此标准。

gradle intellij-idea junit osgi aem
1个回答
2
投票

有趣的是,这只发生在我直接从IDE运行测试时。事实证明,在编译测试类时,IntelliJ没有生成SCR元数据。

当我用Gradle编译测试类时,'com.cognifide.aem.bundle'插件用于生成SCR描述符并将其放在生成的Java Archive中。这就是为什么Gradle执行的单元测试工作正常的原因。只需单击IntelliJ中的“运行”按钮,就会错过此步骤。

为了实现这一点,我最终设置了IntelliJ以允许我通过Gradle运行单元测试。

我转到设置>构建,执行,部署> Gradle> Runner并使用下拉菜单,以便我可以逐个测试地决定是否使用Gradle。

Settings dialog in IntelliJ with a checkbox that says 'Delegate IDE build/run actions to gradle' and a dropdown menu with the option 'Let me choos per test' selected

© www.soinside.com 2019 - 2024. All rights reserved.