【发布时间】:2018-11-05 01:53:30
【问题描述】:
我正在尝试测试使用声明服务注释 (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 的文档。我使用的版本符合这个标准。
【问题讨论】:
-
如果您将 BND 插件与 Gradle 一起使用,这可能会对您有所帮助(您必须针对 Gradle 进行调整):felix.apache.org/documentation/faqs/…
-
@Jens Gradle 插件确实会生成元数据。我的问题源于IDE在编译测试源时不会这样做。将编译委托给 Gradle 就可以了。
标签: gradle intellij-idea junit osgi aem