Hi ,
I am using MockitoJUnitRunner for Junit .
I having the below code in java :-
In the below TestUser class on the line
resolver.adaptTo(User.class) it fails and is always null how to resolve it
import org.apache.jackrabbit.api.security.user.User;
import org.apache.sling.api.resource.ResourceResolver;
public class TestUser {
private final ResourceResolver resolver;
private final User user;
public TestUser(final ResourceResolver resolver) {
this.resolver = resolver;
this.user= resolver.adaptTo(User.class);
}
}
Sling mode class:-
@Model(adaptables = {SlingHttpServletRequest.class, Resource.class},
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class TestClass {
@ScriptVariable
private ValueMap properties;
@ScriptVariable
private Node currentNode;
@SlingObject
private SlingHttpServletRequest request;
@Deleted Account
private Resource resource;
private String appId;
@PostConstruct
public void initModel() {
//It is used below
TestUser user = new TestUser(request.getResourceResolver());
String appId=user.getAppId();
}
}
My Test class:-
I want to test the appId value that we get from slingmodel class as
resolver.adaptTo(User.class) fails so cannot test
@RunWith(MockitoJUnitRunner.Silent.class)
public class Testing {
private TestClass testClass;
private Resource resource;
@Rule
public final AemContext context = new AemContext(ResourceResolverType.JCR_MOCK);
protected MockSlingHttpServletRequest request;
protected MockSlingHttpServletResponse response;
@Mock
protected ResourceResolver resourceResolver;
@Mock
protected Page page;
@Mock
private TestUser testUser;
@Before
public void initialize() throws RepositoryException {
context.load().json("/resource/abc.json,
"/content/abc");
resource = context.create().
resource("/content/abc/jcr:content/test1");
page = context.create().page("/content/abc");
context.addModelsForClasses(TestClass.class);
request = context.request();
response = context.response();
context.request().setResource(resource);
slingBindings.put(SlingBindings.REQUEST, request);
slingBindings.put(SlingBindings.RESPONSE, response);
slingBindings.put(WCMBindings.CURRENT_PAGE, page);
slingBindings.put(WCMBindings.RESOURCE_PAGE, page);
slingBindings.put(WCMBindings.PAGE_PROPERTIES, resource.getValueMap());
slingBindings.put(WCMBindings.PROPERTIES, resource.getValueMap());
slingBindings.put(SlingBindings.RESOURCE, resource);
slingBindings.put(SlingBindings.SLING, context.slingScriptHelper());
slingBindings.put(WCMBindings.INHERITED_PAGE_PROPERTIES, resource.getValueMap());
slingBindings.put(SlingBindings.RESOLVER, resource.getResourceResolver());
request.setResource(resource);
request.setAttribute(SlingBindings.class.getName(), slingBindings);
TestClass = context.request().adaptTo(TestClass.class);
}
@Test
public void testMethod() {
}
}
Solved! Go to Solution.
Views
Replies
Total Likes
It looks like you are attempting to write tests for constructor injection, please refer to this code snippet and example for how it can be achieved, https://sourcedcode.com/blog/aem/junit-4-aem-sling-models-unit-test-constructor-injection-example.
Secondly, Why are you adapting the resolver to a UserClass.class
TestUser user = new TestUser(request.getResourceResolver()); should be
TestUser user = request.adaptTo(TestUser.class);
Checkout the code snippet and example for Unit Test for a Sling Model, https://sourcedcode.com/blog/aem/aem-sling-models-unit-test-junit-4-with-examples.
It looks like you are attempting to write tests for constructor injection, please refer to this code snippet and example for how it can be achieved, https://sourcedcode.com/blog/aem/junit-4-aem-sling-models-unit-test-constructor-injection-example.
Secondly, Why are you adapting the resolver to a UserClass.class
TestUser user = new TestUser(request.getResourceResolver()); should be
TestUser user = request.adaptTo(TestUser.class);
Checkout the code snippet and example for Unit Test for a Sling Model, https://sourcedcode.com/blog/aem/aem-sling-models-unit-test-junit-4-with-examples.
Views
Like
Replies