![](/img/trans.png)
[英]How can I improve/make changes to the orginal Gluon Charm Down Services and use it in my app?
[英]Can I use OSGi Mocks with Declarative Services Annotations
我想测试与Declaratice服务注释(annodated OSGi服务org.osgi.service.component.annotations
)。 我已经基于AEM多项目示例生成了我的项目。
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的文档建议在2.0.0及更高版本中支持声明式服务注释 。 我使用的版本符合此条件。
有趣的是,这仅在我直接从IDE运行测试时发生。 事实证明,在编译我的测试类时,IntelliJ不会生成SCR元数据。
当我使用Gradle编译要测试的类时,将使用'com.cognifide.aem.bundle'
插件生成SCR描述符并将其放置在生成的Java存档中。 这就是为什么Gradle执行的单元测试可以正常工作的原因。 只需单击IntelliJ中的“运行”按钮,就会错过此步骤。
为了使此工作正常进行,我最终设置了IntelliJ,以允许我通过Gradle运行单元测试。
我转到“设置”>“构建,执行,部署”>“ Gradle”>“ Runner”,并使用下拉菜单,以便可以逐项测试决定是否使用Gradle。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.