Dear All,

Sorry if this has been already asked, I haven't found any clue how to solve
that.
Considering the following JUnit test class:

public class TestComponentTest {
private ProvidedClass mockProvided = mock(ProvidedClass.class);

@Rule
public SlingContext context = new SlingContext();
 @Before
public void setUp() {
context.addModelsForPackage("com.asd.models");
 context.registerService(new AdapterFactory() {
@Override
public <AdapterType> AdapterType getAdapter(Object adapter,
Class<AdapterType> type) {
if (adapter instanceof ResourceResolver &&
type.equals(ProvidedClass.class)) {
return (AdapterType)mockProvided;
}
return null;
}
});
}
 @Test
public void testAMethod() throws Exception {
when(mockProvided.someMethod("foo")).thenReturn("bar");
/*
...
*/
assertEquals(instance.aMethod(), "bar");
}
}

Having the adapterfactory implemented as anonymus, is there any possibility
to register and use that inside SlingContext? Eg by getting a reference to
the Adapter Manager somehow to call it's registerFactory?

I've tried multiple ways[1][2]  but inside the "instance" the current
resource never adapts to the desired ProvidedClass possibly because of the
lack of SCR metadata. The original AdapterFactory is provided by the
framework I use so I can't register it the way the docs suggests.

Thanks
Zsolt

[1]
http://sling.apache.org/documentation/development/sling-mock.html#adapter-factories
[2] http://sling.apache.org/documentation/development/osgi-mock.html#usage

Reply via email to