给定代码:
public class TestExample {
@Rule
SlingContext slingContext = new SlingContext(ResourceResolverType.JCR_MOCK);
@Test
public void test() {
ResourceResolverFactory rrf = slingContext.getService(ResourceResolverFactory.class);
}
}
给我:
org.apache.sling.resourceresolver.impl.ResourceResolverFactoryActivator$1 cannot be cast to org.apache.sling.api.resource.ResourceResolverFactory
java.lang.ClassCastException
at example(ExampleTest.java:21)
我必须使用slingContext.resourceResolver()
方法吗?它不是我想要的灵活,因为一次只存在ResourceResolver
个实例(并且它在tearDown
上关闭,而我的类正在自行完成)。我可以以某种方式避免这个问题吗?
答案 0 :(得分:0)
以下代码适用于我
public class RRFTest {
@Rule
public final SlingContext slingContext = new SlingContext(ResourceResolverType.JCR_MOCK);
@Test
public void readRootWithRRF() throws LoginException {
ResourceResolverFactory rrf = slingContext.getService(ResourceResolverFactory.class);
ResourceResolver resolver = rrf.getAdministrativeResourceResolver(null);
Resource resource = resolver.getResource("/");
Assert.assertNotNull(resource);
}
}
使用以下依赖项
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.testing.osgi-mock</artifactId>
<version>1.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.testing.jcr-mock</artifactId>
<version>1.1.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.testing.sling-mock</artifactId>
<version>1.3.0</version>
<scope>test</scope>
</dependency>
您使用的是什么版本的Sling mocks?