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

Unit testing 模拟OSGi上下文中的服务激活异常,unit-testing,osgi,aem,sling,Unit Testing,Osgi,Aem,Sling,在我的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 {

在我的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;
}
然后,使用osgi模拟教程,我创建了以下测试类:

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());
    }

}
测试在最后一行崩溃,出现以下异常:

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
根据互联网上的建议,我已经联系到并确保我的
pom.xml
具有与
maven bundle plugin
完全相同的配置-但这并没有解决问题


知道我在哪里出错了吗?

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

确保您已准备好以下各项:

  • 使用osgi mock 2.x mock依赖项,它与osgi R6兼容。这是我用过的

        <dependency>
            <groupId>org.apache.sling</groupId>
            <artifactId>org.apache.sling.testing.osgi-mock</artifactId>
            <version>2.3.6</version>
            <scope>test</scope>
        </dependency>
    
  • 确保插件构建部分包含生成所需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>
    
    
    org.apache.felix
    

    在我的例子中,我必须添加ds注释依赖项

    <dependency>
        <groupId>org.apache.felix</groupId>
        <artifactId>org.apache.felix.scr.ds-annotations</artifactId>
        <version>1.2.10</version>
        <scope>provided</scope>
    </dependency>
    
    
    org.apache.felix
    

    添加对
    org.apache.sling.testing.osgi mock的依赖似乎已经解决了这个问题。谢谢:)
    
    <dependency>
        <groupId>org.apache.felix</groupId>
        <artifactId>org.apache.felix.scr.ds-annotations</artifactId>
        <version>1.2.10</version>
        <scope>provided</scope>
    </dependency>