模拟 OSGi 上下文中的服务激活异常

Cpt*_*der 3 osgi unit-testing sling aem

在我的 Maven 项目中,我创建了一个简单的 OSGi 服务,它只接受一个引用:

import org.apache.sling.api.resource.ResourceResolverFactory;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

@Component
public class MyFoo {
    @Reference
    private ResourceResolverFactory factory;
}
Run Code Online (Sandbox Code Playgroud)

然后,使用 osgi-mock 教程,我创建了以下测试类:

import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.testing.mock.osgi.junit.OsgiContext;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
public class MyFooTest {
    @Rule
    public OsgiContext mockContext = new OsgiContext();

    @Test
    public void test() {
        ResourceResolverFactory mockFactory = Mockito.mock(ResourceResolverFactory.class);

        mockContext.registerService(ResourceResolverFactory.class, mockFactory);
        mockContext.registerInjectActivateService(new MyFoo());
    }

}
Run Code Online (Sandbox Code Playgroud)

测试在最后一行崩溃,但有以下异常:

org.apache.sling.testing.mock.osgi.NoScrMetadataException: No OSGi SCR metadata found for class MyFoo

    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:141)
    at MyFooTest.testGetResolver(MyFooTest.java:22)
//snippet
Run Code Online (Sandbox Code Playgroud)

按照互联网上的建议,我已经找到了这个配置指南,并确保我的pom.xml配置完全正确maven-bundle-plugin——但这并没有解决问题。

知道我在哪里犯了错误吗?

Sub*_*Sul 5

我尝试复制您的问题,最初遇到了相同的异常,现在可以正常工作了。问题可能是由于maven-scr-plugin和之间的传递依赖导致类路径损坏maven-bundle-plugin。

确保您具备以下条件:

  • 使用与 OSGI R6 兼容的 osgi-mock 2.x 模拟依赖项。这是我用的。

        <dependency>
            <groupId>org.apache.sling</groupId>
            <artifactId>org.apache.sling.testing.osgi-mock</artifactId>
            <version>2.3.6</version>
            <scope>test</scope>
        </dependency>
    
    Run Code Online (Sandbox Code Playgroud)
  • 删除scrmaven 插件和依赖项(maven-scr-plugin和org.apache.felix.scr),因为这会在与 OSGi R6 注释结合使用时导致类路径冲突。这是maven-bundle-plugin我用过的版本。我从这里选择了所有必需的依赖项。

            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>3.2.0</version>
            </plugin>
    
    Run Code Online (Sandbox Code Playgroud)
  • 确保您的插件构建部分包含生成所需 DS 元数据所需的配置。您可以参考您链接的 felix 文档以获取更多信息。

        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <extensions>true</extensions>
            <executions>
                <execution>
                    <id>scr-metadata</id>
                    <goals>
                        <goal>manifest</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <exportScr>true</exportScr>
                <instructions>
                    <Bundle-SymbolicName>com.aem.chula.chula</Bundle-SymbolicName>
    
                    <Sling-Model-Packages>
                      com.aem.models
                    </Sling-Model-Packages>
    
                    <_dsannotations>*</_dsannotations>
                    <_metatypeannotations>*</_metatypeannotations>
                </instructions>
            </configuration>
        </plugin>
    
    Run Code Online (Sandbox Code Playgroud)
  • 做一个mvn clean install将在/OSG-INF.

  • 运行您的测试。我用过MockitoJunitRunner,应该也能正常工作PowerMock。

在此处输入图片说明