This blog article will show code an example for JUnit 4, for how the Request Attribute is being mocked. This example will only cover mocking the @RequestAttribute with the use of @PostConstruct runtime phase, When you are in the code runtime phase of the constructor injection, this will not work (I spent a bit of time on this).
This article will not cover how to setup JUnit 4 for AEM, but however, more information can be found on this blog article.
Sling Model Class : ExampleSlingModel.class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | package com.sourcedcode.core.models; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.models.annotations.DefaultInjectionStrategy; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.injectorspecific.RequestAttribute; import javax.annotation.PostConstruct; @Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) public class ExampleSlingModel { @RequestAttribute(name = "websitename") private String websiteName; @RequestAttribute private int version; @PostConstruct public void init() { websiteName = websiteName.concat(":websitename"); version = version * 2; } public String getWebsiteName() { return websiteName; } public int getVersion() { return version; } } |
Sling Model Test Class : ExampleSlingModelTest.class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | package com.sourcedcode.core.models; import io.wcm.testing.mock.aem.junit.AemContext; import org.apache.sling.testing.mock.sling.ResourceResolverType; import org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest; import org.junit.Assert; import org.junit.Before; import org.junit.Rule; import org.junit.Test; public class ExampleSlingModelTest { @Rule public final AemContext aemContext = new AemContext(ResourceResolverType.JCR_MOCK); private MockSlingHttpServletRequest request; private ExampleSlingModel underTest; @Before public void setUp() throws Exception { aemContext.addModelsForPackage("com.finning.platform.core.models.utils"); request = aemContext.request(); request.setAttribute("websitename", "sourcedcode"); request.setAttribute("version", 5); } @Test public void test_getWebsiteName() { underTest = request.adaptTo(ExampleSlingModel.class); Assert.assertEquals("sourcedcode:websitename", underTest.getWebsiteName()); } @Test public void test_getVersion() { underTest = request.adaptTo(ExampleSlingModel.class); Assert.assertEquals(10, underTest.getVersion()); } } |
It was weird… but now I am able to get the unit test values! Thank you!
really easy-to-follow blog, thanks!
This really works! Thank you!
Mocking AEM Objects is a pain, but thank you for this post
Thank you for this blog post!
Thank you!
This works!
This fixed my problem! Thank you!
Thumbs up!
Cool, thanks!
Very good guide! I keep coming back and back to this post!
WOW cool!
Thanks for this one, Brian! Keep it up with the amazing blog articles!
!! thanks, bud
It’s a shame you don’t have a donate button! I’d most certainly donate to this outstanding blog! I guess for now i’ll settle for bookmarking and adding your RSS feed to my Google account. I look forward to brand new updates and will share this site with my Facebook group. Chat soon!
Gee, thanks lol.
You have truly earned the title of champion, and I am deeply appreciative of your efforts. Thank you 😉