PageManager is null in AemContext AEM mocks


PageManager is null in AemContext AEM mocks and this line breaks because of that:

context.create().page(PATH_TO_TEST_PAGE, PATH_TO_TEST_TEMPLATE, pageProps);

Here is how we fetch the context:

It fails only when running mvn clean install, when running just one test class locally using Intelij it succeeds, so the problem is only when multiple test classes are being runned.


Stefan Seifert
August 2, 2019, 11:38 AM

yes, an application-specific context is to load common JSON content, register OSGi services etc. that are required for all tests.

but no, it's not supported in a @BeforeAll style, but in a @BeforeEach style!

every test that your run tampers with the context, changes conten in the (mock) respository. so you need to make sure you have a clean start for each test run (not each test class), setting all common stuff new. AEM mock and the underlying mocks is designed to to this very fast.

Matija Kovacek
August 2, 2019, 12:07 PM

I have an example of a test class with application-specific context with 36 test methods and it takes ~25 seconds. In @BeforeEach method I’m loading the specific page, getting this resource and adapting to my sling model. Is it possible to this load and adapting only once? (junit 5)

Stefan Seifert
August 2, 2019, 1:14 PM

difficult to say without knowing your use cases or test code what's taking so long. in any case the AemContext object is currently not designed to be reused between test cases, and not planned to add support for it (to support it we would need to add some sort of "reset to safe point" logic in the underlying libraries osgi mock, jcr mock, resource resolver mock etc. - quite an effort).

check your resusable code in the application-specific context:

  • is really everything required for all tests - perhaps some parts can be made conditional

  • profile your common test setup code to check which part takes the longest time - and check if it can be simplified

  • are you using the most efficient resource resolver type? performance: oak >> jcr mock >> resource resolver mock

Marijan Hranj
August 6, 2019, 2:15 PM

Hi Stefan

Instead of opening a new ticket I will just ask here, hope that's not a problem.
Is there a way to test queries from QueryBuilder with AEM mocks?

queryBuilder.createQuery(PredicateGroup.create(predicateMap), session);


Stefan Seifert
August 6, 2019, 4:48 PM

best place to ask common question is on our mailing list:

concerning your question: no, currently there is no dedicated support for QueryBuilder in AEM mocks. use Mockito or other mocking tools to test code accessing query builder.




Marijan Hranj