Expand my Community achievements bar.

SOLVED

AEM mocks returns Null for @Reference objects

Avatar

Level 2

I am trying to mock Querybuilder and resolverfactory in my test class but I am getting null always.

Main class:

@Reference

QueryBuilder builder;

@Reference

ResourceResoloverFactory resolverFactory;

Test class:

@Mock

  QueryBuilder builder;

setup(){

context.registerAdapter(ResourceResolver.class, QueryBuilder.class, builder);

when(builder.createQuery(any(PredicateGroup.class), any(Session.class))).thenReturn(query);

}

Suggest me how to mock @Reference objects in mockito.

Note: I am using Junit4 and mokito and AEM 6.2.

1 Accepted Solution

Avatar

Correct answer by
Level 3

Inside setup() add below line first and then use when

Whitebox.setInternalState(mainClassObject, "builder", builder);

This will initialise builder with mock builder in that mainClassObject.

View solution in original post

3 Replies

Avatar

Correct answer by
Level 3

Inside setup() add below line first and then use when

Whitebox.setInternalState(mainClassObject, "builder", builder);

This will initialise builder with mock builder in that mainClassObject.

Avatar

Employee Advisor

It seems that you just register adapters, but not services.