<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JUNIT Archives - Sourced Code</title>
	<atom:link href="https://sourcedcode.com/blog/category/aem/junit/feed" rel="self" type="application/rss+xml" />
	<link>https://sourcedcode.com</link>
	<description>AEM Blog Made Just for You.</description>
	<lastBuildDate>Tue, 09 Sep 2025 21:41:45 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.4.8</generator>

<image>
	<url>https://sourcedcode.com/wp-content/uploads/2019/09/cropped-favicon-32x32.png</url>
	<title>JUNIT Archives - Sourced Code</title>
	<link>https://sourcedcode.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to Speed Up Unit Tests in JUnit 5 AEM Projects with SLF4J-NOP</title>
		<link>https://sourcedcode.com/blog/aem/how-to-speed-up-unit-tests-in-junit-5-aem-projects-with-slf4j-nop</link>
					<comments>https://sourcedcode.com/blog/aem/how-to-speed-up-unit-tests-in-junit-5-aem-projects-with-slf4j-nop#respond</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Wed, 07 Aug 2024 12:12:18 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[AEMaaCS]]></category>
		<category><![CDATA[JUNIT]]></category>
		<category><![CDATA[JUNIT4]]></category>
		<category><![CDATA[JUNIT5]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=7304</guid>

					<description><![CDATA[<p>Optimizing the performance of unit tests in AEM projects is crucial for maintaining efficient development cycles. One effective method to achieve this is by disabling logging during test execution using the slf4j-nop library. Here&#8217;s why and how you can implement it, along with some performance benchmarks. Quick Links Why Disable Logs During Unit Tests? Adding [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/how-to-speed-up-unit-tests-in-junit-5-aem-projects-with-slf4j-nop">How to Speed Up Unit Tests in JUnit 5 AEM Projects with SLF4J-NOP</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Optimizing the performance of unit tests in AEM projects is crucial for maintaining efficient development cycles. One effective method to achieve this is by disabling logging during test execution using the <strong>slf4j-nop</strong> library. Here&#8217;s why and how you can implement it, along with some performance benchmarks.</p>
<div class="mention-block">
    <strong>Quick Links</strong></p>
<ol>
<li><a href="#why_disable_logs_during_unit_tests">Why Disable Logs During Unit Tests?</a></li>
<li><a href="#adding_slf4j_nop_to_your_project">Adding slf4j-nop to Your Project</a></li>
<li><a href="#benchmarks_and_examples">Benchmarks and Examples</a></li>
<li><a href="#further_optimizing_aem_tests">Further Optimizing AEM Tests</a></li>
</ol>
</div>
<hr class="spacer-large"/>
<h2 id="why_disable_logs_during_unit_tests">1. Why Disable Logs During Unit Tests?</h2>
<p>Logging during unit tests can add unnecessary overhead, especially in large projects where tests generate extensive logs. While logging is essential in production and development environments for debugging, it offers little value during unit tests where the focus is on verifying the correctness of isolated components. Disabling logs during tests provides several benefits:</p>
<ul>
<li><span class="code-highlight-primary">Reduced I/O Overhead:</span> Logging operations require I/O, which can slow down test execution. Disabling logging eliminates this overhead, leading to faster tests.</li>
<li><span class="code-highlight-primary">Cleaner Test Output:</span> With logs disabled, test outputs are cleaner, making it easier to focus on test results without the distraction of log entries.</li>
<li><span class="code-highlight-primary">Improved Test Performance:</span> Benchmarks have shown that disabling logs can significantly speed up test execution.</li>
</ul>
<hr class="spacer-large"/>
<h2 id="adding_slf4j_nop_to_your_project">2. Adding slf4j-nop to Your Project</h2>
<p>To disable logging, you can add the <span class="code-highlight-primary">slf4j-nop</span> dependency to your <span class="code-highlight-primary">pom.xml</span>:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #339933;">&lt;</span>dependency<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>groupId<span style="color: #339933;">&gt;</span>org.<span style="color: #006633;">slf4j</span><span style="color: #339933;">&lt;/</span>groupId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>artifactId<span style="color: #339933;">&gt;</span>slf4j<span style="color: #339933;">-</span>nop<span style="color: #339933;">&lt;/</span>artifactId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>version<span style="color: #339933;">&gt;</span>20.0.16<span style="color: #339933;">&lt;/</span>version<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>scope<span style="color: #339933;">&gt;</span>test<span style="color: #339933;">&lt;/</span>scope<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;/</span>dependency<span style="color: #339933;">&gt;</span></div></td></tr></tbody></table></div>
<p>For multi-module AEM projects, ensure this dependency is also included in the <span class="code-highlight-primary">core/pom.xml</span> file.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #339933;">&lt;</span>dependency<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>groupId<span style="color: #339933;">&gt;</span>org.<span style="color: #006633;">slf4j</span><span style="color: #339933;">&lt;/</span>groupId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>artifactId<span style="color: #339933;">&gt;</span>slf4j<span style="color: #339933;">-</span>nop<span style="color: #339933;">&lt;/</span>artifactId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>scope<span style="color: #339933;">&gt;</span>test<span style="color: #339933;">&lt;/</span>scope<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;/</span>dependency<span style="color: #339933;">&gt;</span></div></td></tr></tbody></table></div>
<p><a href="https://mvnrepository.com/artifact/org.slf4j/slf4j-nop" rel="noopener" target="_blank">https://mvnrepository.com/artifact/org.slf4j/slf4j-nop</a></p>
<hr class="spacer-large"/>
<h2 id="benchmarks_and_examples">3. Benchmarks and Examples</h2>
<p>Here’s a comparison of test execution times to illustrate the impact of disabling logs:</p>
<ul>
<li>JUnit 5 (with logging):
<ul>
<li>Run from IntelliJ IDEA: 9.276 seconds</li>
<li>Run with mvn clean test: 8.696 seconds</li>
</ul>
</li>
<li>JUnit 5 (with slf4j-nop):
<ul>
<li>Run from IntelliJ IDEA: <span class="code-highlight-primary">2.116 seconds</span></li>
<li>Run with mvn clean test: <span class="code-highlight-primary">1.892 seconds</span></li>
</ul>
</li>
</ul>
<p>These results clearly demonstrate that disabling logging can drastically reduce test execution times, allowing for faster feedback during development.</p>
<hr class="spacer-large"/>
<h2 id="further_optimizing_aem_tests">4. Further Optimizing AEM Tests</h2>
<p>In addition to disabling logging, further optimization can be achieved by selecting the appropriate <span class="code-highlight-primary">AemContext</span>:</p>
<ul>
<li><span class="code-highlight-primary">NoResourceResolverTypeAemContext (Fastest):</span> Ideal for tests that don&#8217;t require resource resolution.</li>
<li><span class="code-highlight-primary">ResourceResolverMockAemContext (Default):</span> Best for tests that need a mocked resource resolver.</li>
<li><span class="code-highlight-primary">JcrMockAemContext:</span> Useful for tests requiring JCR features like indexing.</li>
<li><span class="code-highlight-primary">JcrOakAemContext (Slowest):</span> Necessary for simulating a full JCR, though it’s the slowest option.</li>
</ul>
<p>By combining these strategies—disabling unnecessary logging and selecting the appropriate context—you can achieve significantly faster and more efficient unit tests.</p>
<p>Implementing these strategies ensures your unit tests are optimized, allowing you to maintain high development velocity without being slowed down by lengthy test runs.</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/how-to-speed-up-unit-tests-in-junit-5-aem-projects-with-slf4j-nop">How to Speed Up Unit Tests in JUnit 5 AEM Projects with SLF4J-NOP</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/how-to-speed-up-unit-tests-in-junit-5-aem-projects-with-slf4j-nop/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>JUNIT 5: ArgumentCaptor with Mockito</title>
		<link>https://sourcedcode.com/blog/aem/junit/junit-5-argumentcaptor-with-mockito</link>
					<comments>https://sourcedcode.com/blog/aem/junit/junit-5-argumentcaptor-with-mockito#respond</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Fri, 11 Aug 2023 07:08:32 +0000</pubDate>
				<category><![CDATA[JUNIT]]></category>
		<category><![CDATA[JUNIT5]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=5884</guid>

					<description><![CDATA[<p>Unit testing isn&#8217;t just about confirming your code works; it&#8217;s about ensuring it excels. Mockito, a trusted mock framework, introduces the ArgumentCaptor, a potent tool for honing your unit tests. This feature empowers you to capture and assert method arguments, leading to highly targeted tests. In this article, we&#8217;ll delve into the realm of ArgumentCaptor [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/junit/junit-5-argumentcaptor-with-mockito">JUNIT 5: ArgumentCaptor with Mockito</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Unit testing isn&#8217;t just about confirming your code works; it&#8217;s about ensuring it excels. Mockito, a trusted mock framework, introduces the <span class="code-highlight-secondary">ArgumentCaptor</span>, a potent tool for honing your unit tests. This feature empowers you to capture and assert method arguments, leading to highly targeted tests. </p>
<p><p>In this article, we&#8217;ll delve into the realm of <span class="code-highlight-secondary">ArgumentCaptor</span> within the JUnit 5 framework. And provide some code examples of a Java Class with it&#8217;s Java unit test Class</p>
<hr class="spacer-larger"/>
<h2>Putting ArgumentCaptor to Work: A Real-world Scenario</h2>
<p>Imagine you’re testing a search service that interacts with a search engine API. Your goal is to ensure the service accurately constructs and sends search queries to the API. This is where ArgumentCaptor proves invaluable. It lets you capture the search terms and other parameters, enabling you to assert their correctness with surgical precision.</p>
<p>Let&#8217;s dive into a hands-on example to demonstrate how <span class="code-highlight-secondary">ArgumentCaptor</span> shines in a practical context. For our illustration, we&#8217;ll focus on a <span class="code-highlight-secondary">SearchService</span> that interacts with a search engine to fetch relevant results based on user input.</p>
<div style="height: 50px;"></div>
<h3>SearchService.java</h3>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SearchService <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> SearchEngine searchEngine<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> SearchService<span style="color: #009900;">&#40;</span>SearchEngine searchEngine<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">searchEngine</span> <span style="color: #339933;">=</span> searchEngine<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> search<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> searchTerm<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> searchEngine.<span style="color: #006633;">performSearch</span><span style="color: #009900;">&#40;</span>searchTerm<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Our mission: to write a JUnit 5 test that verifies whether the <span class="code-highlight-secondary">search</span> method constructs the search query correctly and calls the search engine API with the expected search term. Enter <span class="code-highlight-secondary">ArgumentCaptor</span>.</p>
<div style="height: 50px;"></div>
<h3>SearchServiceTest.java</h3>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.jupiter.api.Test</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.mockito.ArgumentCaptor</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">mockito</span>.<span style="color: #006633;">Mockito</span>.<span style="color: #339933;">*;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SearchServiceTest <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">void</span> testSearchService<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SearchEngine searchEngine <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>SearchEngine.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SearchService searchService <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SearchService<span style="color: #009900;">&#40;</span>searchEngine<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; ArgumentCaptor<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> searchTermCaptor <span style="color: #339933;">=</span> ArgumentCaptor.<span style="color: #006633;">forClass</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> searchTerm <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;AEM JUNIT5 test examples&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>searchEngine.<span style="color: #006633;">performSearch</span><span style="color: #009900;">&#40;</span>searchTermCaptor.<span style="color: #006633;">capture</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">List</span>.<span style="color: #006633;">of</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;result1&quot;</span>, <span style="color: #0000ff;">&quot;result2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> searchResults <span style="color: #339933;">=</span> searchService.<span style="color: #006633;">search</span><span style="color: #009900;">&#40;</span>searchTerm<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>searchEngine<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">performSearch</span><span style="color: #009900;">&#40;</span>searchTermCaptor.<span style="color: #006633;">capture</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>searchTerm, searchTermCaptor.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span>, searchResults.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>In this example, we utilize <span class="code-highlight-secondary">ArgumentCaptor</span> to capture the search term passed to the <span class="code-highlight-secondary">performSearch</span> method of the <span class="code-highlight-secondary">SearchEngine</span>. This allows us to assert that the correct search term is used when interacting with the API.</p>
<hr class="spacer-larger"/>
<h2>Benefits and Best Practices</h2>
<ul>
<li><strong>Focused Testing:</strong> <span class="code-highlight-secondary">ArgumentCaptor</span> lets you zoom in on specific method arguments, enhancing the precision of your tests.</li>
<li><strong>Readable Assertions:</strong> Your test assertions become highly readable and directly tied to real-world scenarios, improving code comprehension.</li>
<li><strong>Use Cases:</strong> While particularly useful for testing interactions with external systems like APIs, remember to employ <span class="code-highlight-secondary">ArgumentCaptor</span> judiciously, focusing on crucial scenarios.</li>
</ul>
<hr class="spacer-larger"/>
<h2>Wrapping Up</h2>
<p>Incorporating the <span class="code-highlight-secondary">ArgumentCaptor</span> into your testing toolbox elevates your unit tests from good to exceptional. By capturing and asserting method arguments, you&#8217;re crafting tests that pinpoint potential issues. As you integrate this feature into your testing routine, you&#8217;ll discover the power of precision-driven tests that ensure your code operates flawlessly, especially in scenarios involving external APIs and intricate parameters. Embrace <span class="code-highlight-secondary">ArgumentCaptor</span>, and watch your unit tests shine with accuracy and insight.</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/junit/junit-5-argumentcaptor-with-mockito">JUNIT 5: ArgumentCaptor with Mockito</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/junit/junit-5-argumentcaptor-with-mockito/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>JUNIT 5: JCR SQL 2 Unit Tests w/ Sling Servlet</title>
		<link>https://sourcedcode.com/blog/aem/junit-5-jcr-sql-2-unit-tests-w-sling-servlet</link>
					<comments>https://sourcedcode.com/blog/aem/junit-5-jcr-sql-2-unit-tests-w-sling-servlet#respond</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Wed, 09 Aug 2023 12:30:05 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[JUNIT]]></category>
		<category><![CDATA[JUNIT5]]></category>
		<category><![CDATA[Premium Content]]></category>
		<category><![CDATA[Search]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=5855</guid>

					<description><![CDATA[<p>I&#8217;ve dedicated quite a chunk of my personal time to crafting this blog post, and yes, I&#8217;ve rigorously tested all useful test cases that you can learn from. Why, you ask? It&#8217;s all for you, my awesome readers! I want to ensure that you not only find value in this post but also gain useful [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/junit-5-jcr-sql-2-unit-tests-w-sling-servlet">JUNIT 5: JCR SQL 2 Unit Tests w/ Sling Servlet</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>I&#8217;ve dedicated quite a chunk of my personal time to crafting this blog post, and yes, I&#8217;ve rigorously tested all useful test cases that you can learn from. Why, you ask? It&#8217;s all for you, my awesome readers! I want to ensure that you not only find value in this post but also gain useful knowledge that you can easily share with your fellow devs.</p>
<p>I&#8217;m here to make it super simple for you. This JCR SQL 2 Unit Tests w/ Sling Servlet blog post will only cover the modern approach to unit testing using JUnit 5 and the io.wcm.testing.aem-mock.junit5 library. I&#8217;m excited to show you a live example of a Servlet utilizing the Java Search, JCR SQL 2 API. By the time we&#8217;re done, you&#8217;ll be equipped with the tools to write tests that not only work but also rock!</p>
<div class="mention-block">
<strong>Quick Links</strong></p>
<ol>
<li><a href="#what-to-test-for-when-testing-a-servlet-or-osgi-service-that-utilizes-the-java-jcr-sql-2-api">What to test for when testing a Servlet or OSGI Service that utilizes the Java, JCR SQL 2 API?</a>
<ol>
<li>a. <a href="#query-string">Query String</a></li>
<li>b. <a href="#results-of-the-query-nodes">Results of the Query, Nodes</a></li>
</ol>
</li>
<li><a href="#code-examples">Code Examples</a>
<ol>
<li>a. <a href="#get-paged-by-path-servlet-java">GetPagedByPathServlet.java</a></li>
<li>b. <a href="#get-paged-by-path-servlet-test-java">GetPagedByPathServletTest.java</a></li>
</ol>
</li>
</ol>
</div>
<div class="mention-block--gold">
<strong>Pre-requirement</strong><br />
&#8211; JUnit 5<br />
&#8211; io.wcm.testing.aem-mock.junit5 library.</p>
<p>Additionally, if you want to learn more about JCR SQL 2, <a href="/blog/aem/aem-jcr-sql2-tutorial-and-examples-and-cheatsheet" target="_blank" rel="noopener">click here</a> to view the blog. Or if you want to explore and learn all other existing AEM Java Search APIs, <a href="/blog/aem/a-simple-guide-to-aem-search-apis-exploring-your-options-for-powerful-search-functionality" rel="noopener" target="_blank">click here</a>.
</div>
<hr class="spacer-larger"/>
<h2 id="what-to-test-for-when-testing-a-servlet-or-osgi-service-that-utilizes-the-java-jcr-sql-2-api">1. What to test for when testing a Servlet or OSGI Service that utilizes the Java, JCR SQL 2 API?</h2>
<div style="height:50px;"></div>
<h4 id="query-string">1a. Query String</h4>
<p>In my opinion, it&#8217;s crucial to focus on two main areas. First, you should thoroughly test the <strong>query string</strong> that gets executed. This query string is like the roadmap guiding your code&#8217;s behavior. Ensuring its accuracy is essential since it forms the backbone of your code&#8217;s functionality.</p>
<h4 id="results-of-the-query-nodes">2b. Results of the Query, Nodes</h4>
<p>The second area of emphasis lies in the <strong>results of the query</strong>, which are obtained through the nodeIterator. This is where the real action happens – think of it as the hub where your code processes data. Testing this part of your code involves ensuring that the data processing via the nodeIterator is in line with your intended modifications.</p>
<p>To sum up, when testing a Servlet or OSGi Service, your primary focus should revolve around two core elements: meticulously verifying the accuracy of the query string and confirming that the data processing through the nodeIterator meets your expectations. By honing in on these aspects, you&#8217;re building a solid foundation of trust in your code.</p>
<hr class="spacer-larger"/>
<h2 id="code-examples">2. Code Examples</h2>
<div style="height:50px;"></div>
<h3 id="get-paged-by-path-servlet-java">2a. GetPagedByPathServlet.java</h3>
<p>Here in this example, you&#8217;d see a AEM Sling Servlet being created, and within doGet you&#8217;d see us utilizing the JCR SQL2 API, which captures and transforms the results into a JSON object.</p>
<div class="code-1200">
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.PrintWriter</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.Node</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.NodeIterator</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.RepositoryException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.Session</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.query.Query</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.query.QueryManager</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.query.QueryResult</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletResponse</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.servlets.SlingSafeMethodsServlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.component.annotations.Component</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gson.JsonArray</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gson.JsonObject</span><span style="color: #339933;">;</span><br />
<br />
@<span style="color: #003399;">Component</span><span style="color: #009900;">&#40;</span>service <span style="color: #339933;">=</span> javax.<span style="color: #006633;">servlet</span>.<span style="color: #006633;">Servlet</span>.<span style="color: #000000; font-weight: bold;">class</span>, property <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">&quot;sling.servlet.paths=/bin/getpagedbypath&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> GetPagedByPathServlet <span style="color: #000000; font-weight: bold;">extends</span> SlingSafeMethodsServlet <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> doGet<span style="color: #009900;">&#40;</span>SlingHttpServletRequest request, SlingHttpServletResponse response<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Get the JCR session</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Session session <span style="color: #339933;">=</span> request.<span style="color: #006633;">getResourceResolver</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">adaptTo</span><span style="color: #009900;">&#40;</span>Session.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Query statement</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> queryString <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT page.* FROM [cq:Page] AS page &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;INNER JOIN [cq:PageContent] AS jcrContentNode ON ISCHILDNODE(jcrContentNode, page) &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;WHERE ISDESCENDANTNODE(page, '/content/we-retail') &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;AND jcrContentNode.[cq:lastModified] &lt;= CAST('2023-01-01T00:00:00.000+00:00' AS DATE)&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Create the query object</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; QueryManager queryManager <span style="color: #339933;">=</span> session.<span style="color: #006633;">getWorkspace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getQueryManager</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Query query <span style="color: #339933;">=</span> queryManager.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span>queryString, Query.<span style="color: #006633;">JCR_SQL2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Execute the query</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; QueryResult result <span style="color: #339933;">=</span> query.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Get the nodes from the query result</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NodeIterator nodeIterator <span style="color: #339933;">=</span> result.<span style="color: #006633;">getNodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Collect the paths of the first ten pages in a list</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> pagePaths <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>nodeIterator.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Node pageNode <span style="color: #339933;">=</span> nodeIterator.<span style="color: #006633;">nextNode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> pagePath <span style="color: #339933;">=</span> pageNode.<span style="color: #006633;">getPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pagePaths.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>pagePath<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Create a JSON object to hold the results</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JsonObject jsonResponse <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JsonObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JsonArray resultsArray <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JsonArray<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Add the paths to the JSON array</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> path <span style="color: #339933;">:</span> pagePaths<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resultsArray.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>path<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Add the JSON array to the response object</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonResponse.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;results&quot;</span>, resultsArray<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Set the response content type</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;application/json&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Write the JSON response to the output</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">PrintWriter</span> writer <span style="color: #339933;">=</span> response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span>jsonResponse.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #006633;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>RepositoryException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Handle exception</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setStatus</span><span style="color: #009900;">&#40;</span>SlingHttpServletResponse.<span style="color: #006633;">SC_INTERNAL_SERVER_ERROR</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error executing query: &quot;</span> <span style="color: #339933;">+</span> e.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</div>
<p><strong>JSON response</strong></p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0000ff;">&quot;results&quot;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page1&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page2&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page3&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page4&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page5&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page6&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page7&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page8&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page9&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page10&quot;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#93;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p><strong>Curl call</strong></p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">curl -X GET http://localhost:4502/bin/getpagedbypath</div></td></tr></tbody></table></div>
<hr class="spacer-larger"/>
<h3 id="get-paged-by-path-servlet-test-java">2b. GetPagedByPathServletTest.java</h3>
<p>Here in this unit test examples. In my opinion, what is mostly effective unit tested for the JCR SQL 2 API, and to test the query string, and next, test the query result. This example does just it. I have tested the code, and it is working as expected.</p>
<div class="code-2000">
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br />94<br />95<br />96<br />97<br />98<br />99<br />100<br />101<br />102<br />103<br />104<br />105<br />106<br />107<br />108<br />109<br />110<br />111<br />112<br />113<br />114<br />115<br />116<br />117<br />118<br />119<br />120<br />121<br />122<br />123<br />124<br />125<br />126<br />127<br />128<br />129<br />130<br />131<br />132<br />133<br />134<br />135<br />136<br />137<br />138<br />139<br />140<br />141<br />142<br />143<br />144<br />145<br />146<br />147<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">io.wcm.testing.mock.aem.junit5.AemContext</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">io.wcm.testing.mock.aem.junit5.AemContextExtension</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletResponse</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.resource.ResourceResolver</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.jupiter.api.BeforeEach</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.jupiter.api.Test</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.jupiter.api.extension.ExtendWith</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.mockito.ArgumentCaptor</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.mockito.Captor</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.mockito.Mock</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.mockito.MockitoAnnotations</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.NodeIterator</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.RepositoryException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.Session</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.Workspace</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.query.Query</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.query.QueryManager</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.query.QueryResult</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.PrintWriter</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.StringWriter</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">junit</span>.<span style="color: #006633;">jupiter</span>.<span style="color: #006633;">api</span>.<span style="color: #006633;">Assertions</span>.<span style="color: #006633;">assertEquals</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">mockito</span>.<span style="color: #006633;">Mockito</span>.<span style="color: #339933;">*;</span><br />
<br />
@ExtendWith<span style="color: #009900;">&#40;</span>AemContextExtension.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">class</span> GetPagedByPathServletTest <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> AemContext context <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AemContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> GetPagedByPathServlet underTest <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> GetPagedByPathServlet<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Mock<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> SlingHttpServletRequest request<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Mock<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> SlingHttpServletResponse response<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Mock<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> QueryManager queryManager<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Mock<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> Query query<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Mock<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> QueryResult queryResult<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Mock<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> Session session<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Mock<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> ResourceResolver resourceResolver<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Captor<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> ArgumentCaptor<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> queryCaptor<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Mock<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">PrintWriter</span> printWriter<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @BeforeEach<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; MockitoAnnotations.<span style="color: #006633;">openMocks</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #006633;">registerService</span><span style="color: #009900;">&#40;</span>QueryManager.<span style="color: #000000; font-weight: bold;">class</span>, queryManager<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #006633;">registerService</span><span style="color: #009900;">&#40;</span>Query.<span style="color: #000000; font-weight: bold;">class</span>, query<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #006633;">registerService</span><span style="color: #009900;">&#40;</span>QueryResult.<span style="color: #000000; font-weight: bold;">class</span>, queryResult<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>request.<span style="color: #006633;">getResourceResolver</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>resourceResolver<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>resourceResolver.<span style="color: #006633;">adaptTo</span><span style="color: #009900;">&#40;</span>Session.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>session<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Workspace workspace <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>Workspace.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>session.<span style="color: #006633;">getWorkspace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>workspace<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>workspace.<span style="color: #006633;">getQueryManager</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>queryManager<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>queryManager.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span>anyString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, eq<span style="color: #009900;">&#40;</span>Query.<span style="color: #006633;">JCR_SQL2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>query<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>query.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>queryResult<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>printWriter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* In this test, we will only focus on the query string that is executed. This is a particularly<br />
&nbsp; &nbsp; &nbsp;* important step, because we would like to ensure that our query string is correct before<br />
&nbsp; &nbsp; &nbsp;* query.execute() is called.<br />
&nbsp; &nbsp; &nbsp;* @throws Exception<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">void</span> testDoGet_executedQuery<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; NodeIterator nodeIterator <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>NodeIterator.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>queryResult.<span style="color: #006633;">getNodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>nodeIterator<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; underTest.<span style="color: #006633;">doGet</span><span style="color: #009900;">&#40;</span>request, response<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Verify that the correct query string was executed</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>query<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Capture the argument passed to queryManager.createQuery</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>queryManager<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span>queryCaptor.<span style="color: #006633;">capture</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, eq<span style="color: #009900;">&#40;</span>Query.<span style="color: #006633;">JCR_SQL2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Compare the captured query string with the expected query string</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> executedQueryString <span style="color: #339933;">=</span> queryCaptor.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> expectedQueryString <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT page.* FROM [cq:Page] AS page &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;INNER JOIN [cq:PageContent] AS jcrContentNode ON ISCHILDNODE(jcrContentNode, page) &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;WHERE ISDESCENDANTNODE(page, '/content/we-retail') &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;AND jcrContentNode.[cq:lastModified] &lt;= CAST('2023-01-01T00:00:00.000+00:00' AS DATE)&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>expectedQueryString, executedQueryString<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* In this test, we are actually going to mock the response from query.execute() and verify<br />
&nbsp; &nbsp; &nbsp;* that the results has been processed, and the output of the servlet is correct. Here you<br />
&nbsp; &nbsp; &nbsp;* can see us mocking the NodeIterator, and then mocking the next node calls.<br />
&nbsp; &nbsp; &nbsp;* @throws Exception<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">void</span> testDoGet_successfulJsonResponse<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; NodeIterator nodeIterator <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>NodeIterator.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>nodeIterator.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span>, <span style="color: #000066; font-weight: bold;">true</span>, <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>nodeIterator.<span style="color: #006633;">nextNode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>mock<span style="color: #009900;">&#40;</span>javax.<span style="color: #006633;">jcr</span>.<span style="color: #006633;">Node</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>nodeIterator.<span style="color: #006633;">nextNode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/content/we-retail/page1&quot;</span>, <span style="color: #0000ff;">&quot;/content/we-retail/page2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>queryResult.<span style="color: #006633;">getNodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>nodeIterator<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; underTest.<span style="color: #006633;">doGet</span><span style="color: #009900;">&#40;</span>request, response<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> expectedOutput <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;{&quot;</span>results<span style="color: #0000ff;">&quot;:[&quot;</span><span style="color: #339933;">/</span>content<span style="color: #339933;">/</span>we<span style="color: #339933;">-</span>retail<span style="color: #339933;">/</span>page1<span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">/</span>content<span style="color: #339933;">/</span>we<span style="color: #339933;">-</span>retail<span style="color: #339933;">/</span>page2<span style="color: #0000ff;">&quot;]}&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; printWriter.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span>expectedOutput<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* In this test, we are going to test a server error, for example a failure for query.execute(),<br />
&nbsp; &nbsp; &nbsp;* and verify that the response status is set to 500, and the error message is printed.<br />
&nbsp; &nbsp; &nbsp;* @throws Exception<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">void</span> testDoGet_serverError<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>queryManager.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span>anyString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, eq<span style="color: #009900;">&#40;</span>Query.<span style="color: #006633;">JCR_SQL2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>query<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>query.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenThrow</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> RepositoryException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Test exception&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">StringWriter</span> stringWriter <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">StringWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">PrintWriter</span> printWriter <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">PrintWriter</span><span style="color: #009900;">&#40;</span>stringWriter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>printWriter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; underTest.<span style="color: #006633;">doGet</span><span style="color: #009900;">&#40;</span>request, response<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Verify that the response status is set to 500</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setStatus</span><span style="color: #009900;">&#40;</span>SlingHttpServletResponse.<span style="color: #006633;">SC_INTERNAL_SERVER_ERROR</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Verify that the error message is printed</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> expectedErrorMessage <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Error executing query: Test exception<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>expectedErrorMessage, stringWriter.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</div>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/junit-5-jcr-sql-2-unit-tests-w-sling-servlet">JUNIT 5: JCR SQL 2 Unit Tests w/ Sling Servlet</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/junit-5-jcr-sql-2-unit-tests-w-sling-servlet/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>JUNIT5: What Exactly is org.mockito.junit.jupiter.MockitoExtension and Sling Model Example</title>
		<link>https://sourcedcode.com/blog/aem/junit/junit5-what-exactly-is-org-mockito-junit-jupiter-mockitoextension-and-sling-model-examplejunit5-what-exactly-is-org-mockito-junit-jupiter-mockitoextension-and-examples</link>
					<comments>https://sourcedcode.com/blog/aem/junit/junit5-what-exactly-is-org-mockito-junit-jupiter-mockitoextension-and-sling-model-examplejunit5-what-exactly-is-org-mockito-junit-jupiter-mockitoextension-and-examples#comments</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Sat, 28 Jan 2023 06:56:04 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[JUNIT]]></category>
		<category><![CDATA[JUNIT5]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=4266</guid>

					<description><![CDATA[<p>org.mockito.junit.jupiter.MockitoExtension is a JUnit 5 extension provided by the Mockito library. It allows you to use the Mockito framework to create and inject mocked objects into your JUnit 5 test classes. This extension provides several features such as annotation-based mock creation, automatic detection of unused stubs, and support for the Mockito API. It enables you [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/junit/junit5-what-exactly-is-org-mockito-junit-jupiter-mockitoextension-and-sling-model-examplejunit5-what-exactly-is-org-mockito-junit-jupiter-mockitoextension-and-examples">JUNIT5: What Exactly is org.mockito.junit.jupiter.MockitoExtension and Sling Model Example</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>org.mockito.junit.jupiter.MockitoExtension is a JUnit 5 extension provided by the Mockito library. It allows you to use the Mockito framework to create and inject mocked objects into your JUnit 5 test classes. This extension provides several features such as annotation-based mock creation, automatic detection of unused stubs, and support for the Mockito API. It enables you to write clean and readable tests by allowing you to use the familiar Mockito API directly in your JUnit 5 tests, without the need to manually instantiate mock objects or use a separate mocking framework. This can make it easier to write and maintain unit tests that use mocked dependencies.</p>
<p>In additional, this post will be relating more to a Sling Model. In this example, we will create a sling model utilizing dependencies of 2 services.</p>
<hr class="spacer-large"/>
<p><strong>Example Implementation for a Sling Model</strong></p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">Here is an example implementation of the UnderTestSlingModel <span style="color: #000000; font-weight: bold;">class</span> that can be used in the provided JUnit5 test<span style="color: #339933;">:</span><br />
<br />
java<br />
Copy code<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.models.annotations.Model</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.models.annotations.injectorspecific.SlingObject</span><span style="color: #339933;">;</span><br />
<br />
@Model<span style="color: #009900;">&#40;</span>adaptables <span style="color: #339933;">=</span> SlingHttpServletRequest.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UnderTestSlingModel <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; @SlingObject<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> Service1 service1<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @SlingObject<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> Service2 service2<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getData<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> service1.<span style="color: #006633;">getData</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> service2.<span style="color: #006633;">getData</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<hr class="spacer-large" />
<p><strong>Example JUNIT5:</strong></p>
<div class="code-750">
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.jupiter.api.Test</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.jupiter.api.extension.ExtendWith</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.mockito.Mock</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.mockito.InjectMocks</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.mockito.junit.jupiter.MockitoExtension</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">io.wcm.testing.mock.aem.junit5.AemContext</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">io.wcm.testing.mock.aem.junit5.AemContextExtension</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">junit</span>.<span style="color: #006633;">jupiter</span>.<span style="color: #006633;">api</span>.<span style="color: #006633;">Assertions</span>.<span style="color: #006633;">assertEquals</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">mockito</span>.<span style="color: #006633;">ArgumentMatchers</span>.<span style="color: #006633;">any</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">mockito</span>.<span style="color: #006633;">Mockito</span>.<span style="color: #006633;">when</span><span style="color: #339933;">;</span><br />
<br />
@ExtendWith<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>AemContextExtension.<span style="color: #000000; font-weight: bold;">class</span>, MockitoExtension.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">class</span> UnderTestSlingModelTest <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; @Mock<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> Service1 service1<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Mock<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> Service2 service2<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @InjectMocks<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> UnderTestSlingModel underTest<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> AemContext context <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AemContext<span style="color: #009900;">&#40;</span>ResourceResolverType.<span style="color: #006633;">JCR_MOCK</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">void</span> test1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Setting up the mocked services</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #006633;">registerService</span><span style="color: #009900;">&#40;</span>service1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #006633;">registerService</span><span style="color: #009900;">&#40;</span>service2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Creating the underTest sling model</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; underTest <span style="color: #339933;">=</span> context.<span style="color: #006633;">request</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">adaptTo</span><span style="color: #009900;">&#40;</span>UnderTestSlingModel.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Testing the mocked services</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>service1.<span style="color: #006633;">getData</span><span style="color: #009900;">&#40;</span>any<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>service2.<span style="color: #006633;">getData</span><span style="color: #009900;">&#40;</span>any<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test1test2&quot;</span>, underTest.<span style="color: #006633;">getData</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">void</span> test2<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Setting up the mocked services</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #006633;">registerService</span><span style="color: #009900;">&#40;</span>service1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #006633;">registerService</span><span style="color: #009900;">&#40;</span>service2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Creating the underTest sling model</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; underTest <span style="color: #339933;">=</span> context.<span style="color: #006633;">request</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">adaptTo</span><span style="color: #009900;">&#40;</span>UnderTestSlingModel.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Testing the mocked services</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>service1.<span style="color: #006633;">getData</span><span style="color: #009900;">&#40;</span>any<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test3&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>service2.<span style="color: #006633;">getData</span><span style="color: #009900;">&#40;</span>any<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test4&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test3test4&quot;</span>, underTest.<span style="color: #006633;">getData</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</div>
<hr class="spacer-large" />
<p><strong>pom.xml</strong></p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #339933;">&lt;</span>dependency<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>groupId<span style="color: #339933;">&gt;</span>org.<span style="color: #006633;">junit</span>.<span style="color: #006633;">jupiter</span><span style="color: #339933;">&lt;/</span>groupId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>artifactId<span style="color: #339933;">&gt;</span>junit<span style="color: #339933;">-</span>jupiter<span style="color: #339933;">-</span>api<span style="color: #339933;">&lt;/</span>artifactId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>version<span style="color: #339933;">&gt;</span>5.7.0<span style="color: #339933;">&lt;/</span>version<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>scope<span style="color: #339933;">&gt;</span>test<span style="color: #339933;">&lt;/</span>scope<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;/</span>dependency<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;</span>dependency<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>groupId<span style="color: #339933;">&gt;</span>org.<span style="color: #006633;">junit</span>.<span style="color: #006633;">jupiter</span><span style="color: #339933;">&lt;/</span>groupId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>artifactId<span style="color: #339933;">&gt;</span>junit<span style="color: #339933;">-</span>jupiter<span style="color: #339933;">-</span>engine<span style="color: #339933;">&lt;/</span>artifactId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>version<span style="color: #339933;">&gt;</span>5.7.0<span style="color: #339933;">&lt;/</span>version<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>scope<span style="color: #339933;">&gt;</span>test<span style="color: #339933;">&lt;/</span>scope<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;/</span>dependency<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;</span>dependency<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>groupId<span style="color: #339933;">&gt;</span>io.<span style="color: #006633;">wcm</span><span style="color: #339933;">&lt;/</span>groupId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>artifactId<span style="color: #339933;">&gt;</span>io.<span style="color: #006633;">wcm</span>.<span style="color: #006633;">testing</span>.<span style="color: #006633;">mock</span>.<span style="color: #006633;">aem</span><span style="color: #339933;">&lt;/</span>artifactId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>version<span style="color: #339933;">&gt;</span>7.4.0<span style="color: #339933;">&lt;/</span>version<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>scope<span style="color: #339933;">&gt;</span>test<span style="color: #339933;">&lt;/</span>scope<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;/</span>dependency<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;</span>dependency<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>groupId<span style="color: #339933;">&gt;</span>org.<span style="color: #006633;">mockito</span><span style="color: #339933;">&lt;/</span>groupId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>artifactId<span style="color: #339933;">&gt;</span>mockito<span style="color: #339933;">-</span>junit<span style="color: #339933;">-</span>jupiter<span style="color: #339933;">&lt;/</span>artifactId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>version<span style="color: #339933;">&gt;</span>3.6.28<span style="color: #339933;">&lt;/</span>version<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>scope<span style="color: #339933;">&gt;</span>test<span style="color: #339933;">&lt;/</span>scope<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;/</span>dependency<span style="color: #339933;">&gt;</span></div></td></tr></tbody></table></div>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/junit/junit5-what-exactly-is-org-mockito-junit-jupiter-mockitoextension-and-sling-model-examplejunit5-what-exactly-is-org-mockito-junit-jupiter-mockitoextension-and-examples">JUNIT5: What Exactly is org.mockito.junit.jupiter.MockitoExtension and Sling Model Example</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/junit/junit5-what-exactly-is-org-mockito-junit-jupiter-mockitoextension-and-sling-model-examplejunit5-what-exactly-is-org-mockito-junit-jupiter-mockitoextension-and-examples/feed</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>AEM JUNIT5 with io.wcm.testing.mock.aem.junit5.AemContextExtension</title>
		<link>https://sourcedcode.com/blog/aem/junit/aem-junit5-with-io-wcm-testing-mock-aem-junit5-aemcontextextension</link>
					<comments>https://sourcedcode.com/blog/aem/junit/aem-junit5-with-io-wcm-testing-mock-aem-junit5-aemcontextextension#respond</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Sat, 28 Jan 2023 05:35:44 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[JUNIT]]></category>
		<category><![CDATA[JUNIT5]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=4240</guid>

					<description><![CDATA[<p>io.wcm.testing.mock.aem.junit5.AemContextExtension is an extension for JUnit 5 that provides a way to run AEM (Adobe Experience Manager) unit tests with mocked AEM objects. This extension allows you to set up an AemContext object in your JUnit 5 test class, which provides access to mocked AEM objects such as the ResourceResolver, SlingSettings, and more. This makes [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/junit/aem-junit5-with-io-wcm-testing-mock-aem-junit5-aemcontextextension">AEM JUNIT5 with io.wcm.testing.mock.aem.junit5.AemContextExtension</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>io.wcm.testing.mock.aem.junit5.AemContextExtension is an extension for JUnit 5 that provides a way to run AEM (Adobe Experience Manager) unit tests with mocked AEM objects. This extension allows you to set up an AemContext object in your JUnit 5 test class, which provides access to mocked AEM objects such as the ResourceResolver, SlingSettings, and more. This makes it easier to test your code that interacts with AEM without actually requiring a running AEM instance. You can imagine, when using io.wcm.testing.mock.aem.junit5.AemContextExtension imported class, the JCR is virtually created in memory, you can think that this is a mock AEM JCR.</p>
<div class="mention-block">
<strong>Quick Links</strong></p>
<ol>
<li><a href="#exactly-what-is-io-wcm-testing-mock-aem-junit5-aemcontextextension">Exactly what is io.wcm.testing.mock.aem.junit5.AemContextExtension?</a></li>
<li><a href="#what-are-the-use-cases-for-io-wcm-testing-mock-aem-junit5-aemcontextextension">What are the use cases for io.wcm.testing.mock.aem.junit5.AemContextExtension</a>
<ul>
<li><a href="#pom-xml">a. pom.xml</a></li>
</ul>
</li>
<li><a href="#choosing-the-right-aemcontext-to-speed-up-tests">Choosing the Right AemContext to Speed Up Tests</a></li>
</ol>
</div>
<hr class="spacer-larger" />
<h3 id="exactly-what-is-io-wcm-testing-mock-aem-junit5-aemcontextextension">1. Exactly what is io.wcm.testing.mock.aem.junit5.AemContextExtension?</h3>
<li>io.wcm.testing.mock.aem.junit5.AemContextExtension enables you to mock a in-memory AEM JCR, exactly like the AEM instance.</li>
<li>When mocking the JCR instance, you can imagine that this is an empty JCR, with no nodes inside of it, so you will need to use Java to add resources in the mock JCR repository.</li>
<li>Services like the pageManger, tagManger, resourceResolver, etc&#8230; will be available in the mock JCR instance.</li>
<li>io.wcm.testing.mock.aem.junit5.AemContextExtension is an extension for JUnit 5 that allows for running AEM (Adobe Experience Manager) unit tests with mocked AEM objects.</li>
<li>It provides a way to set up an AemContext object in a JUnit 5 test class, which gives access to mocked AEM objects such as the ResourceResolver, SlingSettings, and more.</li>
<li>This makes it easier to test code that interacts with AEM without requiring a running AEM instance.</li>
<li>It allows developers to write unit tests that can test their AEM-based code in isolation, without relying on a running AEM instance or a test environment set up with a specific version of AEM.</li>
<li>This extension can be used for testing AEM components, AEM service, AEM workflows and more.</li>
<li>It reduces the time and cost of testing by eliminating the need for a running AEM instance, and allows for faster test execution and more efficient development.</li>
<hr class="spacer-larger"/>
<h3 id="what-are-the-use-cases-for-io-wcm-testing-mock-aem-junit5-aemcontextextension">2. What are the use cases for io.wcm.testing.mock.aem.junit5.AemContextExtension</h3>
<ul>
<li>Unit testing AEM-based projects without the need for a running AEM instance</li>
<li>Isolating your tests from the dependencies on a specific AEM version or environment</li>
<li>Mocking specific AEM objects, such as the ResourceResolver or SlingSettings, for more fine-grained control over the test environment</li>
<li>Simplifying the setup and tear-down of AEM-related test objects</li>
<li>Improving the reliability and repeatability of your tests by reducing external dependencies.</li>
</ul>
<hr class="spacer"/>
<p>Here&#8217;s an example of how to use AemContextExtension in a JUnit 5 test class:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">junit</span>.<span style="color: #006633;">jupiter</span>.<span style="color: #006633;">api</span>.<span style="color: #006633;">Assertions</span>.<span style="color: #006633;">assertEquals</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">junit</span>.<span style="color: #006633;">jupiter</span>.<span style="color: #006633;">api</span>.<span style="color: #006633;">Assertions</span>.<span style="color: #006633;">assertNotNull</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.jupiter.api.Test</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.jupiter.api.extension.ExtendWith</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">io.wcm.testing.mock.aem.junit5.AemContext</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">io.wcm.testing.mock.aem.junit5.AemContextExtension</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.resource.Resource</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.resource.ResourceResolver</span><span style="color: #339933;">;</span><br />
<br />
@ExtendWith<span style="color: #009900;">&#40;</span>AemContextExtension.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">class</span> MyTest <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> AemContext context <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AemContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">void</span> testMockResourceObjects<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// create mock resource objects</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">resource</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/content/object1&quot;</span>, <span style="color: #0000ff;">&quot;prop1&quot;</span>, <span style="color: #0000ff;">&quot;value1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">resource</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/content/object2&quot;</span>, <span style="color: #0000ff;">&quot;prop1&quot;</span>, <span style="color: #0000ff;">&quot;value2&quot;</span>, <span style="color: #0000ff;">&quot;prop2&quot;</span>, <span style="color: #0000ff;">&quot;value3&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">resource</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/content/object3&quot;</span>, <span style="color: #0000ff;">&quot;prop1&quot;</span>, <span style="color: #0000ff;">&quot;value4&quot;</span>, <span style="color: #0000ff;">&quot;prop2&quot;</span>, <span style="color: #0000ff;">&quot;value5&quot;</span>, <span style="color: #0000ff;">&quot;prop3&quot;</span>, <span style="color: #0000ff;">&quot;value6&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// get resource resolver</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ResourceResolver resourceResolver <span style="color: #339933;">=</span> context.<span style="color: #006633;">resourceResolver</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// test object 1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Resource resource1 <span style="color: #339933;">=</span> resourceResolver.<span style="color: #006633;">getResource</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/content/object1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertNotNull<span style="color: #009900;">&#40;</span>resource1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;value1&quot;</span>, resource1.<span style="color: #006633;">getValueMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;prop1&quot;</span>, <span style="color: #003399;">String</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// test object 2</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Resource resource2 <span style="color: #339933;">=</span> resourceResolver.<span style="color: #006633;">getResource</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/content/object2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertNotNull<span style="color: #009900;">&#40;</span>resource2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;value2&quot;</span>, resource2.<span style="color: #006633;">getValueMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;prop1&quot;</span>, <span style="color: #003399;">String</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;value3&quot;</span>, resource2.<span style="color: #006633;">getValueMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;prop2&quot;</span>, <span style="color: #003399;">String</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// test object 3</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Resource resource3 <span style="color: #339933;">=</span> resourceResolver.<span style="color: #006633;">getResource</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/content/object3&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertNotNull<span style="color: #009900;">&#40;</span>resource3<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;value4&quot;</span>, resource3.<span style="color: #006633;">getValueMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;prop1&quot;</span>, <span style="color: #003399;">String</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;value5&quot;</span>, resource3.<span style="color: #006633;">getValueMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;prop2&quot;</span>, <span style="color: #003399;">String</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;value6&quot;</span>, resource3.<span style="color: #006633;">getValueMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;prop3&quot;</span>, <span style="color: #003399;">String</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<hr class="spacer-large"/>
<h3 id="pom-xml">2a. pom.xml</h3>
<p>These dependencies are what is being used in my pom.xml file for the example above. </p>
<p><strong>3 include dependencies:</strong></p>
<ol>
<li>The first dependency is the &#8220;wcm-io-test-context-aem&#8221; library which provides the AemContext extension and utility classes for unit testing AEM-based Java code.</li>
<li>The second dependency is &#8220;junit-jupiter-api&#8221; which provides the JUnit 5 API for writing tests.</li>
<li>The third dependency is &#8220;junit-jupiter-engine&#8221; which provides the JUnit 5 TestEngine implementation for running tests.</li>
</ol>
<p>Keep in mind that these versions could be different based on the latest version of the dependencies, you should check the documentation for the latest versions before use&#8230; these are my settings.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #339933;">&lt;</span>dependency<span style="color: #339933;">&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;</span>groupId<span style="color: #339933;">&gt;</span>io.<span style="color: #006633;">wcm</span>.<span style="color: #006633;">testing</span><span style="color: #339933;">&lt;/</span>groupId<span style="color: #339933;">&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;</span>artifactId<span style="color: #339933;">&gt;</span>wcm<span style="color: #339933;">-</span>io<span style="color: #339933;">-</span>test<span style="color: #339933;">-</span>context<span style="color: #339933;">-</span>aem<span style="color: #339933;">&lt;/</span>artifactId<span style="color: #339933;">&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;</span>version<span style="color: #339933;">&gt;</span>3.3.0<span style="color: #339933;">&lt;/</span>version<span style="color: #339933;">&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;</span>scope<span style="color: #339933;">&gt;</span>test<span style="color: #339933;">&lt;/</span>scope<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;/</span>dependency<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;</span>dependency<span style="color: #339933;">&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;</span>groupId<span style="color: #339933;">&gt;</span>org.<span style="color: #006633;">junit</span>.<span style="color: #006633;">jupiter</span><span style="color: #339933;">&lt;/</span>groupId<span style="color: #339933;">&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;</span>artifactId<span style="color: #339933;">&gt;</span>junit<span style="color: #339933;">-</span>jupiter<span style="color: #339933;">-</span>api<span style="color: #339933;">&lt;/</span>artifactId<span style="color: #339933;">&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;</span>version<span style="color: #339933;">&gt;</span>5.7.0<span style="color: #339933;">&lt;/</span>version<span style="color: #339933;">&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;</span>scope<span style="color: #339933;">&gt;</span>test<span style="color: #339933;">&lt;/</span>scope<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;/</span>dependency<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;</span>dependency<span style="color: #339933;">&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;</span>groupId<span style="color: #339933;">&gt;</span>org.<span style="color: #006633;">junit</span>.<span style="color: #006633;">jupiter</span><span style="color: #339933;">&lt;/</span>groupId<span style="color: #339933;">&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;</span>artifactId<span style="color: #339933;">&gt;</span>junit<span style="color: #339933;">-</span>jupiter<span style="color: #339933;">-</span>engine<span style="color: #339933;">&lt;/</span>artifactId<span style="color: #339933;">&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;</span>version<span style="color: #339933;">&gt;</span>5.7.0<span style="color: #339933;">&lt;/</span>version<span style="color: #339933;">&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;</span>scope<span style="color: #339933;">&gt;</span>test<span style="color: #339933;">&lt;/</span>scope<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;/</span>dependency<span style="color: #339933;">&gt;</span></div></td></tr></tbody></table></div>
<hr class="spacer-larger"/>
<h3 id="choosing-the-right-aemcontext-to-speed-up-tests">3. Choosing the Right AemContext to Speed Up Tests</h3>
<p>The AemContextExtension allows for <strong>direct injection of the context</strong> into test methods, this can speed up test suite execution in cases where certain tests require the use of real JCR due to the use of indexes or other JCR features, while others do not. The more &#8220;mocked&#8221; the underlying resource resolver is, the faster the context will be. Therefore, it is recommended to use the following contexts in this order if possible:</p>
<ol>
<li>
<h4>org.apache.sling.testing.mock.sling.junit5.NoResourceResolverTypeAemContext (fastest)</h4>
<ul>
<li>
            NoResourceResolverTypeAemContext is a type of AemContext that is used for tests that do not require a resource resolver.</p>
<li>When this type of AemContext is injected into a test method, it means that the test will not have access to any resource resolving functionality, such as resolving resources by path or querying the JCR.</li>
<li>This can be useful for tests that do not rely on any resources and only test pure Java logic.</li>
<li>This AemContext is the fastest of all the AemContexts because it does not have to instantiate a resource resolver and therefore it is lightweight, and can be useful when running test suites that requires a lot of test cases.</li>
</ul>
</li>
<li>
<h4>org.apache.sling.testing.mock.sling.junit5.ResourceResolverMockAemContext (default when just AemContext is used)</h4>
<ul>
<li>ResourceResolverMockAemContext is an extension of AemContext which mocks the AEM resource resolver.</li>
<li>When you inject an AemContext of this type, it means that the resource resolver used within your test will be a mocked version, rather than the real resource resolver. </li>
<li>This can be useful for faster test execution and for isolating your tests from external dependencies. </li>
<li>It is the default context when using AemContext alone.</li>
</ul>
</li>
<li>
<h4>org.apache.sling.testing.mock.sling.junit5.JcrMockAemContext (slower)</h4>
<ul>
<li>JcrMockAemContext is an extension of AemContext which mocks the AEM resource resolver.</li>
<li>Injecting AEMContext of JcrMockAemContext means that the test class is using a mocked version of the JCR for its AEM context; this means that the JCR functionality is being simulated in order to test the functionality of the code being tested, rather than utilizing a real JCR instance.
<li>This can be useful for testing as it allows for faster execution and a more controlled environment.</li>
<li>The JcrMockAemContext also allows you to use JCR specific features, such as indexes, in your tests. This is useful when you need to test the functionality of your code that uses JCR indexes.</li>
<li>
            This is slower to compared to other AemContext because it simulates a full JCR repository, including the persistence layer. This means that it has to perform more operations and calculations in order to simulate the JCR repository, which can lead to slower test execution times. Additionally, if your tests are using features that require a real JCR repository, such as indexing or searching, this context may be slower as it is still a mocked version of the repository.
         </li>
</ul>
</li>
<li>
<h4>org.apache.sling.testing.mock.sling.junit5.JcrOakAemContext (slowest)</h4>
<ul>
<li>Injecting AEMContext of JcrOakAemContext simulates a real JCR (Java Content Repository) with OakJCR.</li>
<li>This context is the slowest of all the AemContext options because it uses a real JCR and therefore takes more resources and time to run the test.</li>
</li>
</ul>
</li>
</ol>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> AemContext noResourceResolverTypeAemContext <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AemContext<span style="color: #009900;">&#40;</span>ResourceResolverType.<span style="color: #006633;">NONE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// fastest</span><br />
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> AemContext resourceResolverMockAemContext <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AemContext<span style="color: #009900;">&#40;</span>ResourceResolverType.<span style="color: #006633;">RESOURCERESOLVER_MOCK</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// default</span><br />
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> AemContext jcrMockAemContext <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AemContext<span style="color: #009900;">&#40;</span>ResourceResolverType.<span style="color: #006633;">JCR_MOCK</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// slower</span><br />
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> AemContext jcrOakAemContext <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AemContext<span style="color: #009900;">&#40;</span>ResourceResolverType.<span style="color: #006633;">JCR_OAK</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// slowest</span></div></td></tr></tbody></table></div>
<p>It is common to see tests where AemContext is stored in a field in a test class. However, if a single test requires JCR, this can slow down all other tests. With the ability to inject context as a test method parameter, previous tests will be as fast as before.</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/junit/aem-junit5-with-io-wcm-testing-mock-aem-junit5-aemcontextextension">AEM JUNIT5 with io.wcm.testing.mock.aem.junit5.AemContextExtension</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/junit/aem-junit5-with-io-wcm-testing-mock-aem-junit5-aemcontextextension/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Mocking Static Methods in JUNIT5</title>
		<link>https://sourcedcode.com/blog/aem/junit/junit5/mocking-static-methods-in-junit5</link>
					<comments>https://sourcedcode.com/blog/aem/junit/junit5/mocking-static-methods-in-junit5#comments</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Sat, 28 Jan 2023 05:12:08 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[JUNIT]]></category>
		<category><![CDATA[JUNIT5]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=4223</guid>

					<description><![CDATA[<p>PowerMock is a Java framework that allows you to unit test code that uses static methods, constructors, and private methods. PowerMock uses a combination of bytecode manipulation and reflection to enable the mocking of these types of methods. PowerMock is not officially supported by JUnit 5. PowerMock is built on top of EasyMock and provides [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/junit/junit5/mocking-static-methods-in-junit5">Mocking Static Methods in JUNIT5</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>PowerMock is a Java framework that allows you to unit test code that uses static methods, constructors, and private methods. PowerMock uses a combination of bytecode manipulation and reflection to enable the mocking of these types of methods.</p>
<p>PowerMock is not officially supported by JUnit 5. PowerMock is built on top of EasyMock and provides additional functionality such as the ability to mock static methods, constructors, and private methods. PowerMock uses a different test runner than the one provided by JUnit 5, so it is not directly compatible with JUnit 5.</p>
<p>Alternatively, you can use mockito-inline for inline mocking, it allows you to mock static methods, final classes, and methods, and more.</p>
<p>JUnit 5 provides its own extension model that allows for the creation of custom test runners and the registration of extensions. You could write your own test runner for PowerMock that works with JUnit 5 or use a different mocking framework that is compatible with JUnit 5.</p>
<div class="mention-block">
<strong>PowerMock is not officially supported by JUnit 5</strong><br />
If you are using JUnit5 and you really need to use Powermock, you could use the powermock-module-junit5 and powermock-api-mockito2 which have support for JUnit5, but it&#8217;s not recommended as it&#8217;s not officially supported by JUnit5, and to use mockito-inline as suggested.
</div>
<hr class="spacer-larger"/>
<h2>JUNIT5 Mock Static Method Code Examples</h2>
<p>Here&#8217;s an example of how to use mockito-inline to mock a static method in JUnit 5. Starting with a Utils class, we displaying an example of us initializing a Utils class with a static method of getString().</p>
<p><strong>Utils.java class</strong><br />
This is a simple utils class used by JAVA, where it has a static method of getString().</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Utils <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> getString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;Original string&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p><strong>MyTest.java class</strong><br />
This is an example of a class utilizing the utils class. As you can see in the code, line:10 &#038;&#038; line:11 is performing the mocking mechanism. It is important to note that we are using mockito-inine which introduced Mockito.mockStatic, where we can mock the static method for the Utils class.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.jupiter.api.Test</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">junit</span>.<span style="color: #006633;">jupiter</span>.<span style="color: #006633;">api</span>.<span style="color: #006633;">Assertions</span>.<span style="color: #006633;">assertEquals</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.mockito.Mockito</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyTest <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testStaticMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// mock the static method of the Utils class</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Mockito.<span style="color: #006633;">mockStatic</span><span style="color: #009900;">&#40;</span>Utils.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Mockito.<span style="color: #006633;">when</span><span style="color: #009900;">&#40;</span>Utils.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Mocked string&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// create an instance of the class that calls the static method</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; MyClass myClass <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Mocked string&quot;</span>, myClass.<span style="color: #006633;">getStringFromUtils</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p><strong>pom.xml</strong><br />
These are the maven dependencies being used to get the code working as expected for the JUNIT5 examples above.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #339933;">&lt;</span>dependency<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>groupId<span style="color: #339933;">&gt;</span>org.<span style="color: #006633;">junit</span>.<span style="color: #006633;">jupiter</span><span style="color: #339933;">&lt;/</span>groupId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>artifactId<span style="color: #339933;">&gt;</span>junit<span style="color: #339933;">-</span>jupiter<span style="color: #339933;">-</span>api<span style="color: #339933;">&lt;/</span>artifactId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>version<span style="color: #339933;">&gt;</span>5.11.0<span style="color: #339933;">&lt;/</span>version<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>scope<span style="color: #339933;">&gt;</span>test<span style="color: #339933;">&lt;/</span>scope<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;/</span>dependency<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;</span>dependency<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>groupId<span style="color: #339933;">&gt;</span>org.<span style="color: #006633;">junit</span>.<span style="color: #006633;">jupiter</span><span style="color: #339933;">&lt;/</span>groupId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>artifactId<span style="color: #339933;">&gt;</span>junit<span style="color: #339933;">-</span>jupiter<span style="color: #339933;">-</span>engine<span style="color: #339933;">&lt;/</span>artifactId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>version<span style="color: #339933;">&gt;</span>5.11.0<span style="color: #339933;">&lt;/</span>version<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>scope<span style="color: #339933;">&gt;</span>test<span style="color: #339933;">&lt;/</span>scope<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;/</span>dependency<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;</span>dependency<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>groupId<span style="color: #339933;">&gt;</span>org.<span style="color: #006633;">mockito</span><span style="color: #339933;">&lt;/</span>groupId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>artifactId<span style="color: #339933;">&gt;</span>mockito<span style="color: #339933;">-</span>core<span style="color: #339933;">&lt;/</span>artifactId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>version<span style="color: #339933;">&gt;</span>3.6.0<span style="color: #339933;">&lt;/</span>version<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>scope<span style="color: #339933;">&gt;</span>test<span style="color: #339933;">&lt;/</span>scope<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;/</span>dependency<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;</span>dependency<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>groupId<span style="color: #339933;">&gt;</span>org.<span style="color: #006633;">mockito</span><span style="color: #339933;">&lt;/</span>groupId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>artifactId<span style="color: #339933;">&gt;</span>mockito<span style="color: #339933;">-</span>inline<span style="color: #339933;">&lt;/</span>artifactId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>version<span style="color: #339933;">&gt;</span>3.6.0<span style="color: #339933;">&lt;/</span>version<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>scope<span style="color: #339933;">&gt;</span>test<span style="color: #339933;">&lt;/</span>scope<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;/</span>dependency<span style="color: #339933;">&gt;</span></div></td></tr></tbody></table></div>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/junit/junit5/mocking-static-methods-in-junit5">Mocking Static Methods in JUNIT5</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/junit/junit5/mocking-static-methods-in-junit5/feed</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>JUnit 5: Mocking the HttpClient in Java</title>
		<link>https://sourcedcode.com/blog/aem/junit-5-mocking-the-httpclient-in-java</link>
					<comments>https://sourcedcode.com/blog/aem/junit-5-mocking-the-httpclient-in-java#comments</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Sat, 31 Dec 2022 07:01:38 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[JUNIT]]></category>
		<category><![CDATA[JUNIT5]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=4115</guid>

					<description><![CDATA[<p>Unfortunately when building AEM backend logic, the org.apache.http.client.HttpClient is non-trivial to write unit tests. In my case, I scavenged across the net to find a solution where you can make an GET and POST request via HttpClient in particular for AEM, and unit test is performing as expected. In order for me write successful to [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/junit-5-mocking-the-httpclient-in-java">JUnit 5: Mocking the HttpClient in Java</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Unfortunately when building AEM backend logic, the org.apache.http.client.HttpClient is non-trivial to write unit tests. In my case, I scavenged across the net to find a solution where you can make an GET and POST request via HttpClient in particular for AEM, and unit test is performing as expected. In order for me write successful to my servlet, I had to refactor the code. Forcefully, I had to change the org.apache.http.client.HttpClient to <strong>org.apache.http.osgi.services.HttpClientBuilderFactory</strong> OSGI service; the HttpClientBuilderFactor giving me the HttpClient that I need.</p>
<p>This article will showcase a successful unit test with the doGet and doPost example code. The result of the methods will return a JSON string, which we will mock.</p>
<hr class="spacer-larger" />
<h3>1. Dependencies</h3>
<p>Before we begin, I would like to mention the dependencies that I am using to get this code to work <strong>(please make sure you are using JUNIT5)</strong>:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #339933;">&lt;</span>dependency<span style="color: #339933;">&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;</span>groupId<span style="color: #339933;">&gt;</span>io.<span style="color: #006633;">wcm</span><span style="color: #339933;">&lt;/</span>groupId<span style="color: #339933;">&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;</span>artifactId<span style="color: #339933;">&gt;</span>io.<span style="color: #006633;">wcm</span>.<span style="color: #006633;">testing</span>.<span style="color: #006633;">aem</span><span style="color: #339933;">-</span>mock.<span style="color: #006633;">junit5</span><span style="color: #339933;">&lt;/</span>artifactId<span style="color: #339933;">&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;</span>exclusions<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>exclusion<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>groupId<span style="color: #339933;">&gt;</span>org.<span style="color: #006633;">apache</span>.<span style="color: #006633;">sling</span><span style="color: #339933;">&lt;/</span>groupId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>artifactId<span style="color: #339933;">&gt;</span>org.<span style="color: #006633;">apache</span>.<span style="color: #006633;">sling</span>.<span style="color: #006633;">models</span>.<span style="color: #006633;">impl</span><span style="color: #339933;">&lt;/</span>artifactId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>exclusion<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>exclusion<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>groupId<span style="color: #339933;">&gt;</span>org.<span style="color: #006633;">slf4j</span><span style="color: #339933;">&lt;/</span>groupId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>artifactId<span style="color: #339933;">&gt;</span>slf4j<span style="color: #339933;">-</span>simple<span style="color: #339933;">&lt;/</span>artifactId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>exclusion<span style="color: #339933;">&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;/</span>exclusions<span style="color: #339933;">&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;</span>scope<span style="color: #339933;">&gt;</span>test<span style="color: #339933;">&lt;/</span>scope<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;/</span>dependency<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;</span>dependency<span style="color: #339933;">&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;</span>groupId<span style="color: #339933;">&gt;</span>org.<span style="color: #006633;">mockito</span><span style="color: #339933;">&lt;/</span>groupId<span style="color: #339933;">&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;</span>artifactId<span style="color: #339933;">&gt;</span>mockito<span style="color: #339933;">-</span>core<span style="color: #339933;">&lt;/</span>artifactId<span style="color: #339933;">&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;</span>version<span style="color: #339933;">&gt;</span>4.1.0<span style="color: #339933;">&lt;/</span>version<span style="color: #339933;">&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;</span>scope<span style="color: #339933;">&gt;</span>test<span style="color: #339933;">&lt;/</span>scope<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;/</span>dependency<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;</span>dependency<span style="color: #339933;">&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;</span>groupId<span style="color: #339933;">&gt;</span>org.<span style="color: #006633;">mockito</span><span style="color: #339933;">&lt;/</span>groupId<span style="color: #339933;">&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;</span>artifactId<span style="color: #339933;">&gt;</span>mockito<span style="color: #339933;">-</span>junit<span style="color: #339933;">-</span>jupiter<span style="color: #339933;">&lt;/</span>artifactId<span style="color: #339933;">&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;</span>version<span style="color: #339933;">&gt;</span>4.1.0<span style="color: #339933;">&lt;/</span>version<span style="color: #339933;">&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;</span>scope<span style="color: #339933;">&gt;</span>test<span style="color: #339933;">&lt;/</span>scope<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;/</span>dependency<span style="color: #339933;">&gt;</span></div></td></tr></tbody></table></div>
<div class="spacer-large"></div>
<h3>2. HttpClientServlet.java</h3>
<div class="code-1000">
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourcedcode.core.servlets</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.adobe.granite.rest.Constants</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.drew.lang.annotations.NotNull</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.fasterxml.jackson.databind.ObjectMapper</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gson.Gson</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gson.JsonObject</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.http.client.methods.CloseableHttpResponse</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.http.client.methods.HttpGet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.http.client.methods.HttpPost</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.http.entity.ContentType</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.http.entity.StringEntity</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.http.impl.client.CloseableHttpClient</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.http.osgi.services.HttpClientBuilderFactory</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.http.util.EntityUtils</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletResponse</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.servlets.HttpConstants</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.servlets.SlingAllMethodsServlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.servlets.annotations.SlingServletResourceTypes</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.component.annotations.Component</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.component.annotations.Reference</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.Servlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.ServletException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletResponse</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.nio.charset.StandardCharsets</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.HashMap</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Map</span><span style="color: #339933;">;</span><br />
<br />
@<span style="color: #003399;">Component</span><span style="color: #009900;">&#40;</span>service <span style="color: #339933;">=</span> Servlet.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
@SlingServletResourceTypes<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; methods <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>HttpConstants.<span style="color: #006633;">METHOD_GET</span>, HttpConstants.<span style="color: #006633;">METHOD_POST</span><span style="color: #009900;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; resourceTypes <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;sourcedcode/component&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; selectors <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;example&quot;</span><span style="color: #009900;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; extensions <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;json&quot;</span><span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HttpClientServlet <span style="color: #000000; font-weight: bold;">extends</span> SlingAllMethodsServlet <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; @<span style="color: #003399;">Reference</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> HttpClientBuilderFactory clientBuilderFactory<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> doGet<span style="color: #009900;">&#40;</span>@NotNull SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ServletException, <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; CloseableHttpClient client <span style="color: #339933;">=</span> clientBuilderFactory.<span style="color: #006633;">newBuilder</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; HttpGet getMethod <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HttpGet<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;https://dummy.restapiexample.com/api/v1/employee/1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; CloseableHttpResponse httpClientResponse <span style="color: #339933;">=</span> client.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span>getMethod<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> responseBody <span style="color: #339933;">=</span> EntityUtils.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span>httpClientResponse.<span style="color: #006633;">getEntity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; JsonObject jsonObject <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Gson<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">fromJson</span><span style="color: #009900;">&#40;</span>responseBody, JsonObject.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setStatus</span><span style="color: #009900;">&#40;</span>HttpServletResponse.<span style="color: #006633;">SC_OK</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span>Constants.<span style="color: #006633;">CT_JSON</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setCharacterEncoding</span><span style="color: #009900;">&#40;</span>StandardCharsets.<span style="color: #006633;">UTF_8</span>.<span style="color: #006633;">name</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>jsonObject.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> doPost<span style="color: #009900;">&#40;</span>@NotNull SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ServletException, <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; CloseableHttpClient client <span style="color: #339933;">=</span> clientBuilderFactory.<span style="color: #006633;">newBuilder</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ObjectMapper mapper <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ObjectMapper<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Map<span style="color: #339933;">&lt;</span><span style="color: #003399;">String</span>, String<span style="color: #339933;">&gt;</span> object <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; object.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test&quot;</span>, <span style="color: #0000ff;">&quot;value&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; StringEntity requestData <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StringEntity<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mapper.<span style="color: #006633;">writeValueAsString</span><span style="color: #009900;">&#40;</span>object<span style="color: #009900;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ContentType.<span style="color: #006633;">APPLICATION_JSON</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; HttpPost postMethod <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HttpPost<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;https://freemeposting.free.beeceptor.com/my/api/path&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; postMethod.<span style="color: #006633;">setEntity</span><span style="color: #009900;">&#40;</span>requestData<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; postMethod.<span style="color: #006633;">setHeader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Test&quot;</span>, <span style="color: #0000ff;">&quot;Test&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; CloseableHttpResponse preRenderedResponse <span style="color: #339933;">=</span> client.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span>postMethod<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> responseBody <span style="color: #339933;">=</span> EntityUtils.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span>preRenderedResponse.<span style="color: #006633;">getEntity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; JsonObject jsonObject <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Gson<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">fromJson</span><span style="color: #009900;">&#40;</span>responseBody, JsonObject.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setStatus</span><span style="color: #009900;">&#40;</span>HttpServletResponse.<span style="color: #006633;">SC_OK</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span>Constants.<span style="color: #006633;">CT_JSON</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setCharacterEncoding</span><span style="color: #009900;">&#40;</span>StandardCharsets.<span style="color: #006633;">UTF_8</span>.<span style="color: #006633;">name</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>jsonObject.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</div>
<hr class="spacer-larger" />
<div class="code-1250">
<h3>3. HttpClientServletTest.java</h3>
<p>In all, the most important line here that you should take a look at is line:60. On this line you can see that I am injecting mocks into the HttpClientBuilderFactory osgi service, which when clientBuilderFactory.newBuilder().build() is being called, the HttpClientBuilderFactory is bring returned in lin:47, which will be the HttpClient itself.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourcedcode.core.servlets</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">io.wcm.testing.mock.aem.junit5.AemContext</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">io.wcm.testing.mock.aem.junit5.AemContextExtension</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.http.HttpEntity</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.http.client.methods.CloseableHttpResponse</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.http.impl.client.CloseableHttpClient</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.http.impl.client.HttpClientBuilder</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.http.osgi.services.HttpClientBuilderFactory</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletResponse</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.servlets.SlingAllMethodsServlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.testing.mock.sling.ResourceResolverType</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.jupiter.api.Test</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.jupiter.api.extension.ExtendWith</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.mockito.InjectMocks</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.mockito.Mock</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.mockito.junit.jupiter.MockitoExtension</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.ServletException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.ByteArrayInputStream</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.InputStream</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.PrintWriter</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">mockito</span>.<span style="color: #006633;">ArgumentMatchers</span>.<span style="color: #006633;">any</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">mockito</span>.<span style="color: #006633;">Mockito</span>.<span style="color: #339933;">*;</span><br />
<br />
@ExtendWith<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>AemContextExtension.<span style="color: #000000; font-weight: bold;">class</span>, MockitoExtension.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HttpClientServletTest <span style="color: #000000; font-weight: bold;">extends</span> SlingAllMethodsServlet <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> AemContext aemContext <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AemContext<span style="color: #009900;">&#40;</span>ResourceResolverType.<span style="color: #006633;">JCR_MOCK</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Mock<br />
&nbsp; &nbsp; HttpClientBuilderFactory clientBuilderFactory<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Mock<br />
&nbsp; &nbsp; CloseableHttpClient closeableHttpClientMock<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Mock<br />
&nbsp; &nbsp; CloseableHttpResponse closeableHttpResponseMock<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @InjectMocks<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> HttpClientServlet unitTest <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HttpClientServlet<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testServletDoGet<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ServletException, <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> responseData <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;{status: &quot;</span>success<span style="color: #0000ff;">&quot;}&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">InputStream</span> anyInputStream <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ByteArrayInputStream</span><span style="color: #009900;">&#40;</span>responseData.<span style="color: #006633;">getBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; MockSlingHttpServletRequest request <span style="color: #339933;">=</span> aemContext.<span style="color: #006633;">request</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SlingHttpServletResponse response <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>SlingHttpServletResponse.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; HttpEntity httpEntityMock <span style="color: #339933;">=</span> &nbsp;mock<span style="color: #009900;">&#40;</span>HttpEntity.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>clientBuilderFactory.<span style="color: #006633;">newBuilder</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>mock<span style="color: #009900;">&#40;</span>HttpClientBuilder.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>clientBuilderFactory.<span style="color: #006633;">newBuilder</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>closeableHttpClientMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>closeableHttpClientMock.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span>any<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>closeableHttpResponseMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>closeableHttpResponseMock.<span style="color: #006633;">getEntity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>httpEntityMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>httpEntityMock.<span style="color: #006633;">getContent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>anyInputStream<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">PrintWriter</span> printWriterMock <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span><span style="color: #003399;">PrintWriter</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>printWriterMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; aemContext.<span style="color: #006633;">registerService</span><span style="color: #009900;">&#40;</span>HttpClientBuilderFactory.<span style="color: #000000; font-weight: bold;">class</span>, clientBuilderFactory<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; unitTest.<span style="color: #006633;">doGet</span><span style="color: #009900;">&#40;</span>request, response<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>printWriterMock<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>eq<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;{&quot;</span>status<span style="color: #0000ff;">&quot;:&quot;</span>success<span style="color: #0000ff;">&quot;}&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testServletDoPost<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ServletException, <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> responseData <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;{status: &quot;</span>success<span style="color: #0000ff;">&quot;}&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">InputStream</span> anyInputStream <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ByteArrayInputStream</span><span style="color: #009900;">&#40;</span>responseData.<span style="color: #006633;">getBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; MockSlingHttpServletRequest request <span style="color: #339933;">=</span> aemContext.<span style="color: #006633;">request</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SlingHttpServletResponse response <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>SlingHttpServletResponse.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; HttpEntity httpEntityMock <span style="color: #339933;">=</span> &nbsp;mock<span style="color: #009900;">&#40;</span>HttpEntity.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>clientBuilderFactory.<span style="color: #006633;">newBuilder</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>mock<span style="color: #009900;">&#40;</span>HttpClientBuilder.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>clientBuilderFactory.<span style="color: #006633;">newBuilder</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>closeableHttpClientMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>closeableHttpClientMock.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span>any<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>closeableHttpResponseMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>closeableHttpResponseMock.<span style="color: #006633;">getEntity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>httpEntityMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>httpEntityMock.<span style="color: #006633;">getContent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>anyInputStream<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">PrintWriter</span> printWriterMock <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span><span style="color: #003399;">PrintWriter</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span>printWriterMock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; aemContext.<span style="color: #006633;">registerService</span><span style="color: #009900;">&#40;</span>HttpClientBuilderFactory.<span style="color: #000000; font-weight: bold;">class</span>, clientBuilderFactory<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; unitTest.<span style="color: #006633;">doPost</span><span style="color: #009900;">&#40;</span>request, response<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; verify<span style="color: #009900;">&#40;</span>printWriterMock<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>eq<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;{&quot;</span>status<span style="color: #0000ff;">&quot;:&quot;</span>success<span style="color: #0000ff;">&quot;}&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</div>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/junit-5-mocking-the-httpclient-in-java">JUnit 5: Mocking the HttpClient in Java</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/junit-5-mocking-the-httpclient-in-java/feed</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
		<item>
		<title>JUnit 4: AEM How to Mock @RequestAttribute value in Sling Model</title>
		<link>https://sourcedcode.com/blog/aem/junit-4-aem-how-to-mock-requestattribute-value-in-sling-model</link>
					<comments>https://sourcedcode.com/blog/aem/junit-4-aem-how-to-mock-requestattribute-value-in-sling-model#comments</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Thu, 20 Jan 2022 03:28:56 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[JUNIT4]]></category>
		<category><![CDATA[Sling Models]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=3300</guid>

					<description><![CDATA[<p>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 [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/junit-4-aem-how-to-mock-requestattribute-value-in-sling-model">JUnit 4: AEM How to Mock @RequestAttribute value in Sling Model</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>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 <span class="code-highlight-primary">@RequestAttribute</span> with the use of <span class="code-highlight-primary">@PostConstruct</span> 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).</p>
<div class="mention-block">
<strong>Attention</strong><br />
This article will not cover how to setup JUnit 4 for AEM, but however, more information can be found on <a href="https://sourcedcode.com/blog/aem/aem-sling-models-unit-test-junit-4-with-examples">this blog article</a>.</div>
<hr class="spacer-large"/>
<h2>Sling Model Class : ExampleSlingModel.class</h2>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourcedcode.core.models</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.models.annotations.DefaultInjectionStrategy</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.models.annotations.Model</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.models.annotations.injectorspecific.RequestAttribute</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.annotation.PostConstruct</span><span style="color: #339933;">;</span><br />
<br />
@Model<span style="color: #009900;">&#40;</span>adaptables <span style="color: #339933;">=</span> SlingHttpServletRequest.<span style="color: #000000; font-weight: bold;">class</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; defaultInjectionStrategy <span style="color: #339933;">=</span> DefaultInjectionStrategy.<span style="color: #006633;">OPTIONAL</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ExampleSlingModel <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; @RequestAttribute<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;websitename&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> websiteName<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @RequestAttribute<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> version<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @PostConstruct<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; websiteName <span style="color: #339933;">=</span> websiteName.<span style="color: #006633;">concat</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;:websitename&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; version <span style="color: #339933;">=</span> version <span style="color: #339933;">*</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getWebsiteName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> websiteName<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getVersion<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> version<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<hr class="spacer-large"/>
<h2>Sling Model Test Class : ExampleSlingModelTest.class</h2>
<div class="code-500">
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourcedcode.core.models</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">io.wcm.testing.mock.aem.junit.AemContext</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.testing.mock.sling.ResourceResolverType</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.Assert</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.Before</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.Rule</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.Test</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ExampleSlingModelTest <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; @Rule<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> AemContext aemContext <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AemContext<span style="color: #009900;">&#40;</span>ResourceResolverType.<span style="color: #006633;">JCR_MOCK</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> MockSlingHttpServletRequest request<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> ExampleSlingModel underTest<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Before<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; aemContext.<span style="color: #006633;">addModelsForPackage</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;com.sourcedcode.utils&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; request <span style="color: #339933;">=</span> aemContext.<span style="color: #006633;">request</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; request.<span style="color: #006633;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;websitename&quot;</span>, <span style="color: #0000ff;">&quot;sourcedcode&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; request.<span style="color: #006633;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;version&quot;</span>, <span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> test_getWebsiteName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; underTest <span style="color: #339933;">=</span> request.<span style="color: #006633;">adaptTo</span><span style="color: #009900;">&#40;</span>ExampleSlingModel.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">Assert</span>.<span style="color: #006633;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;sourcedcode:websitename&quot;</span>, underTest.<span style="color: #006633;">getWebsiteName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> test_getVersion<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; underTest <span style="color: #339933;">=</span> request.<span style="color: #006633;">adaptTo</span><span style="color: #009900;">&#40;</span>ExampleSlingModel.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">Assert</span>.<span style="color: #006633;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span>, underTest.<span style="color: #006633;">getVersion</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</div>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/junit-4-aem-how-to-mock-requestattribute-value-in-sling-model">JUnit 4: AEM How to Mock @RequestAttribute value in Sling Model</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/junit-4-aem-how-to-mock-requestattribute-value-in-sling-model/feed</wfw:commentRss>
			<slash:comments>17</slash:comments>
		
		
			</item>
		<item>
		<title>JUnit 4: AEM Sling Models Unit Test Constructor Injection Example</title>
		<link>https://sourcedcode.com/blog/aem/junit-4-aem-sling-models-unit-test-constructor-injection-example</link>
					<comments>https://sourcedcode.com/blog/aem/junit-4-aem-sling-models-unit-test-constructor-injection-example#comments</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Sun, 05 Jul 2020 11:14:23 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[JUNIT4]]></category>
		<category><![CDATA[Sling Models]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=1993</guid>

					<description><![CDATA[<p>In this example, we will take a look at how we can mock Sling Model Constructor Injection dependencies for a JUnit 4 Unit test. Scenario: The Sling Model must expose either the PROD_URL or the DEAFULT_URL endpoint based on the run mode or request parameters; this is the requirement. The example below will demonstrate the [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/junit-4-aem-sling-models-unit-test-constructor-injection-example">JUnit 4: AEM Sling Models Unit Test Constructor Injection Example</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In this example, we will take a look at how we can mock Sling Model Constructor Injection dependencies for a JUnit 4 Unit test.</p>
<p><strong>Scenario</strong>:<br />
The Sling Model must expose either the <em>PROD_URL</em> or the <em>DEAFULT_URL</em> endpoint based on the run mode or request parameters; this is the requirement. The example below will demonstrate the implementation of logic utilising Sling Model Constructor injection, show Unit test examples, and how mocked dependencies can be Sling Model Constructor injected during the test phase. This completes the example.</p>
<div class="mention-block">
Technologies here used are:</p>
<ul>
<li>AEM project archetype 19 (<a href="https://github.com/adobe/aem-project-archetype/tree/aem-project-archetype-19" target="_BLANK" rel="noopener noreferrer">link</a>)</li>
<li>Mockito 2.27.0 (<a href="https://mvnrepository.com/artifact/org.mockito/mockito-core/2.27.0" target="_BLANK" rel="noopener noreferrer">link</a>)</li>
<li>AEM Mocks JUnit 4 2.7.2 (<a href="https://mvnrepository.com/artifact/io.wcm/io.wcm.testing.aem-mock.junit4/2.7.2" target="_BLANK" rel="noopener noreferrer">link</a>)</li>
</ul>
</div>
<p>This example uses the AEM project archetype 19 to generate a new AEM project, Junit 4 will be used as the testing framework, Mockito 2.27.0 will be used as the mocking framework, and AEM Mocks will be used to mock AEM objects and AEM objects.</p>
<p>What&#8217;s really great about the latest versions of AEM mocks, is that the setup is very minimal. After spinning up a new AEM project from the AEM project archetype 19, you simply need to include the AEM Mocks dependency, and you are ready to go!</p>
<h3>Dependencies</h3>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #666666; font-style: italic;">// pom.xml</span><br />
<span style="color: #339933;">&lt;!--</span> Maven Surefire Plugin <span style="color: #339933;">--&gt;</span><br />
<span style="color: #339933;">&lt;</span>plugin<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>groupId<span style="color: #339933;">&gt;</span>org.<span style="color: #006633;">apache</span>.<span style="color: #006633;">maven</span>.<span style="color: #006633;">plugins</span><span style="color: #339933;">&lt;/</span>groupId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>artifactId<span style="color: #339933;">&gt;</span>maven<span style="color: #339933;">-</span>surefire<span style="color: #339933;">-</span>plugin<span style="color: #339933;">&lt;/</span>artifactId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>version<span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">2.20</span><span style="color: #339933;">&lt;/</span>version<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>configuration<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>junitArtifactName<span style="color: #339933;">&gt;</span>junit<span style="color: #339933;">:</span>junit<span style="color: #339933;">:</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">&lt;/</span>junitArtifactName<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>configuration<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;/</span>plugin<span style="color: #339933;">&gt;</span><br />
...<br />
<span style="color: #339933;">&lt;</span>dependencies<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; ...<br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>dependency<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>groupId<span style="color: #339933;">&gt;</span>io.<span style="color: #006633;">wcm</span><span style="color: #339933;">&lt;/</span>groupId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>artifactId<span style="color: #339933;">&gt;</span>io.<span style="color: #006633;">wcm</span>.<span style="color: #006633;">testing</span>.<span style="color: #006633;">aem</span><span style="color: #339933;">-</span>mock<span style="color: #339933;">&lt;/</span>artifactId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>version<span style="color: #339933;">&gt;</span>2.7.2<span style="color: #339933;">&lt;/</span>version<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>scope<span style="color: #339933;">&gt;</span>test<span style="color: #339933;">&lt;/</span>scope<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>dependency<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; ...<br />
<span style="color: #339933;">&lt;/</span>dependencies<span style="color: #339933;">&gt;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// core/pom.xml</span><br />
<span style="color: #339933;">&lt;</span>dependencies<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; ...<br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>dependency<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>groupId<span style="color: #339933;">&gt;</span>io.<span style="color: #006633;">wcm</span><span style="color: #339933;">&lt;/</span>groupId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>artifactId<span style="color: #339933;">&gt;</span>io.<span style="color: #006633;">wcm</span>.<span style="color: #006633;">testing</span>.<span style="color: #006633;">aem</span><span style="color: #339933;">-</span>mock<span style="color: #339933;">&lt;/</span>artifactId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>dependency<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; ...<br />
<span style="color: #339933;">&lt;/</span>dependencies<span style="color: #339933;">&gt;</span></div></td></tr></tbody></table></div>
<h3>Sling Model Test Class : ConstructorInjection.class</h3>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourcedcode.core.models</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.adobe.cq.export.json.ExporterConstants</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">lombok.Getter</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.resource.Resource</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.models.annotations.DefaultInjectionStrategy</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.models.annotations.Exporter</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.models.annotations.Model</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.models.annotations.injectorspecific.OSGiService</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.models.annotations.injectorspecific.SlingObject</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.settings.SlingSettingsService</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.inject.Inject</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.inject.Named</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">apache</span>.<span style="color: #006633;">commons</span>.<span style="color: #006633;">lang3</span>.<span style="color: #006633;">StringUtils</span>.<span style="color: #006633;">EMPTY</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">apache</span>.<span style="color: #006633;">commons</span>.<span style="color: #006633;">lang3</span>.<span style="color: #006633;">StringUtils</span>.<span style="color: #006633;">isNotEmpty</span><span style="color: #339933;">;</span><br />
<br />
@Model<span style="color: #009900;">&#40;</span>adaptables <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>SlingHttpServletRequest.<span style="color: #000000; font-weight: bold;">class</span>, Resource.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#125;</span>, defaultInjectionStrategy <span style="color: #339933;">=</span> DefaultInjectionStrategy.<span style="color: #006633;">OPTIONAL</span><span style="color: #009900;">&#41;</span><br />
@Exporter<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> ExporterConstants.<span style="color: #006633;">SLING_MODEL_EXPORTER_NAME</span>, extensions <span style="color: #339933;">=</span> ExporterConstants.<span style="color: #006633;">SLING_MODEL_EXTENSION</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ConstructorInjection <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> API_URL_PROD <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;https://api.sourcedcode.com/v1/constructors&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> API_URL_DEFAULT <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;https://uat-api.sourcedcode.com/v1/constructors&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Getter<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> apiUrl <span style="color: #339933;">=</span> EMPTY<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Inject<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> ConstructorInjection<span style="color: #009900;">&#40;</span>@OSGiService @Named<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;slingSettingsService&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">final</span> SlingSettingsService slingSettingsService,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @SlingObject @Named<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;slingHttpServletRequest&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">final</span> SlingHttpServletRequest request<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">boolean</span> isProd <span style="color: #339933;">=</span> slingSettingsService.<span style="color: #006633;">getRunModes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;prod&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">boolean</span> isParamProd <span style="color: #339933;">=</span> isNotEmpty<span style="color: #009900;">&#40;</span>request.<span style="color: #006633;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;prod&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> request.<span style="color: #006633;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;prod&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;true&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isProd <span style="color: #339933;">||</span> isParamProd<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; apiUrl <span style="color: #339933;">=</span> API_URL_PROD<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; apiUrl <span style="color: #339933;">=</span> API_URL_DEFAULT<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<h3>Sling Model Test Class : ConstructorInjectionTest.class</h3>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourcedcode.core.models</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">io.wcm.testing.mock.aem.junit.AemContext</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.settings.SlingSettingsService</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.testing.mock.sling.ResourceResolverType</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.Rule</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.Test</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.runner.RunWith</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.mockito.InjectMocks</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.mockito.Mock</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.mockito.junit.MockitoJUnitRunner</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Collections</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">junit</span>.<span style="color: #000000; font-weight: bold;">Assert</span>.<span style="color: #006633;">assertEquals</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> uk.<span style="color: #006633;">co</span>.<span style="color: #006633;">whitbread</span>.<span style="color: #006633;">wlrestaurants</span>.<span style="color: #006633;">core</span>.<span style="color: #006633;">models</span>.<span style="color: #006633;">ConstructorInjection</span>.<span style="color: #006633;">API_URL_DEFAULT</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> uk.<span style="color: #006633;">co</span>.<span style="color: #006633;">whitbread</span>.<span style="color: #006633;">wlrestaurants</span>.<span style="color: #006633;">core</span>.<span style="color: #006633;">models</span>.<span style="color: #006633;">ConstructorInjection</span>.<span style="color: #006633;">API_URL_PROD</span><span style="color: #339933;">;</span><br />
<br />
@RunWith<span style="color: #009900;">&#40;</span>MockitoJUnitRunner.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ConstructorInjectionTest <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; @Rule<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> AemContext context <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AemContext<span style="color: #009900;">&#40;</span>ResourceResolverType.<span style="color: #006633;">JCR_MOCK</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Mock<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> SlingSettingsService slingSettingsService<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Mock<br />
&nbsp; &nbsp; SlingHttpServletRequest request<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @InjectMocks<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> ConstructorInjection underTest<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> apiUrl_should_return_PROD_url_when_runmode_prod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #006633;">runMode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;prod&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; underTest <span style="color: #339933;">=</span> context.<span style="color: #006633;">request</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">adaptTo</span><span style="color: #009900;">&#40;</span>ConstructorInjection.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">assert</span> underTest <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>API_URL_PROD, underTest.<span style="color: #006633;">getApiUrl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> apiUrl_should_return_DEFAULT_url_when_runmode_not_prod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #006633;">runMode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;staging&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; underTest <span style="color: #339933;">=</span> context.<span style="color: #006633;">request</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">adaptTo</span><span style="color: #009900;">&#40;</span>ConstructorInjection.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">assert</span> underTest <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>API_URL_DEFAULT, underTest.<span style="color: #006633;">getApiUrl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> apiUrl_should_return_PROD_url_when_param_prod_equals_true<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #006633;">request</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setParameterMap</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Collections</span>.<span style="color: #006633;">singletonMap</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;prod&quot;</span>, <span style="color: #0000ff;">&quot;true&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; underTest <span style="color: #339933;">=</span> context.<span style="color: #006633;">request</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">adaptTo</span><span style="color: #009900;">&#40;</span>ConstructorInjection.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">assert</span> underTest <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>API_URL_PROD, underTest.<span style="color: #006633;">getApiUrl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> apiUrl_should_return_DEFAULT_url_when_param_prod_equals_not_true<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #006633;">request</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setParameterMap</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Collections</span>.<span style="color: #006633;">singletonMap</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;prod&quot;</span>, <span style="color: #0000ff;">&quot;test&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; underTest <span style="color: #339933;">=</span> context.<span style="color: #006633;">request</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">adaptTo</span><span style="color: #009900;">&#40;</span>ConstructorInjection.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">assert</span> underTest <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>API_URL_DEFAULT, underTest.<span style="color: #006633;">getApiUrl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<div class="mention-block">
<strong>More&#8230;</strong><br />
<a href="https://sourcedcode.com/blog/category/aem/junit4">Click here</a> to view more examples of AEM JUnit 4 Unit Test Examples.
</div>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/junit-4-aem-sling-models-unit-test-constructor-injection-example">JUnit 4: AEM Sling Models Unit Test Constructor Injection Example</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/junit-4-aem-sling-models-unit-test-constructor-injection-example/feed</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>JUnit 4: AEM Sling Servlet Unit Test Example Using wcm.io AEM Mocks, Servlet by Resource Type</title>
		<link>https://sourcedcode.com/blog/aem/aem-sling-servlet-osgi-r7-by-resource-type-unit-test-junit-4-with-examples</link>
					<comments>https://sourcedcode.com/blog/aem/aem-sling-servlet-osgi-r7-by-resource-type-unit-test-junit-4-with-examples#comments</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Sat, 21 Dec 2019 17:34:23 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[JUNIT]]></category>
		<category><![CDATA[JUNIT4]]></category>
		<category><![CDATA[Sling Models]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=1117</guid>

					<description><![CDATA[<p>This article will demonstrate how to write AEM Unit tests for @SlingServletResourceTypes (OSGi DS 1.4 (R7) component property type annotations) using the Junit4 testing framework. With developers being more visual, the source code is posted below. Technologies here used are: AEM project archetype 19 (link) Mockito 2.27.0 (link) AEM Mocks JUnit 4 2.7.2 (link) This [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/aem-sling-servlet-osgi-r7-by-resource-type-unit-test-junit-4-with-examples">JUnit 4: AEM Sling Servlet Unit Test Example Using wcm.io AEM Mocks, Servlet by Resource Type</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>This article will demonstrate how to write AEM Unit tests for @SlingServletResourceTypes (<a href="https://github.com/apache/sling-org-apache-sling-servlets-annotations" rel="noopener noreferrer" target="_blank">OSGi DS 1.4 (R7) component property type annotations</a>) using the Junit4 testing framework. With developers being more visual, the source code is posted below.</p>
<div class="mention-block">
Technologies here used are:</p>
<ul>
<li>AEM project archetype 19 (<a href="https://github.com/adobe/aem-project-archetype/tree/aem-project-archetype-19" target="_BLANK" rel="noopener noreferrer">link</a>)</li>
<li>Mockito 2.27.0 (<a href="https://mvnrepository.com/artifact/org.mockito/mockito-core/2.27.0" target="_BLANK" rel="noopener noreferrer">link</a>)</li>
<li>AEM Mocks JUnit 4 2.7.2 (<a href="https://mvnrepository.com/artifact/io.wcm/io.wcm.testing.aem-mock.junit4/2.7.2" target="_BLANK" rel="noopener noreferrer">link</a>)</li>
</ul>
</div>
<p>This example uses the AEM project archetype 19 to generate a new AEM project, Junit 4 will be used as the testing framework, Mockito 2.27.0 will be used as the mocking framework, and AEM Mocks will be used to mock AEM objects and AEM API.</p>
<p>What’s really great about the latest versions of AEM mocks, is that the setup is very minimal. After spinning up a new AEM project from the AEM project archetype 19, you simply need to include the AEM Mocks dependency, and you are ready to go!</p>
<p>Using OSGI R7 annotations for your AEM project requires some additional dependencies. Check out Adobe&#8217;s guide to <a href="https://blogs.adobe.com/experiencedelivers/experience-management/osgi/using-osgi-r7-annotations-aem" target="_blank" rel="noopener noreferrer">enable OSGI R7 annotations.</a></p>
<h3>Test Framework Dependencies</h3>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #666666; font-style: italic;">// pom.xml</span><br />
<span style="color: #339933;">&lt;!--</span> Maven Surefire Plugin <span style="color: #339933;">--&gt;</span><br />
<span style="color: #339933;">&lt;</span>plugin<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>groupId<span style="color: #339933;">&gt;</span>org.<span style="color: #006633;">apache</span>.<span style="color: #006633;">maven</span>.<span style="color: #006633;">plugins</span><span style="color: #339933;">&lt;/</span>groupId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>artifactId<span style="color: #339933;">&gt;</span>maven<span style="color: #339933;">-</span>surefire<span style="color: #339933;">-</span>plugin<span style="color: #339933;">&lt;/</span>artifactId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>version<span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">2.20</span><span style="color: #339933;">&lt;/</span>version<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>configuration<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>junitArtifactName<span style="color: #339933;">&gt;</span>junit<span style="color: #339933;">:</span>junit<span style="color: #339933;">:</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">&lt;/</span>junitArtifactName<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>configuration<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;/</span>plugin<span style="color: #339933;">&gt;</span><br />
...<br />
<span style="color: #339933;">&lt;</span>dependencies<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; ...<br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>dependency<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>groupId<span style="color: #339933;">&gt;</span>io.<span style="color: #006633;">wcm</span><span style="color: #339933;">&lt;/</span>groupId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>artifactId<span style="color: #339933;">&gt;</span>io.<span style="color: #006633;">wcm</span>.<span style="color: #006633;">testing</span>.<span style="color: #006633;">aem</span><span style="color: #339933;">-</span>mock<span style="color: #339933;">&lt;/</span>artifactId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>version<span style="color: #339933;">&gt;</span>2.7.2<span style="color: #339933;">&lt;/</span>version<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>scope<span style="color: #339933;">&gt;</span>test<span style="color: #339933;">&lt;/</span>scope<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>dependency<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; ...<br />
<span style="color: #339933;">&lt;/</span>dependencies<span style="color: #339933;">&gt;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// core/pom.xml</span><br />
<span style="color: #339933;">&lt;</span>dependencies<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; ...<br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>dependency<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>groupId<span style="color: #339933;">&gt;</span>io.<span style="color: #006633;">wcm</span><span style="color: #339933;">&lt;/</span>groupId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>artifactId<span style="color: #339933;">&gt;</span>io.<span style="color: #006633;">wcm</span>.<span style="color: #006633;">testing</span>.<span style="color: #006633;">aem</span><span style="color: #339933;">-</span>mock<span style="color: #339933;">&lt;/</span>artifactId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>dependency<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; ...<br />
<span style="color: #339933;">&lt;/</span>dependencies<span style="color: #339933;">&gt;</span></div></td></tr></tbody></table></div>
<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Horizontal Preview Ad --><br />
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-9053803095882933"
     data-ad-slot="8658919803"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins><br />
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<h3>Sling Servlet Resource Types Class : SlingServletResourceTypes.class</h3>
<div class="code-large">
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourcedcode.core.servlets</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.day.cq.wcm.api.Page</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.fasterxml.jackson.databind.ObjectMapper</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletResponse</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.resource.Resource</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.resource.ResourceResolver</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.servlets.SlingSafeMethodsServlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.servlets.annotations.SlingServletResourceTypes</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.component.annotations.Component</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.Servlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Iterator</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> com.<span style="color: #006633;">day</span>.<span style="color: #006633;">crx</span>.<span style="color: #006633;">packaging</span>.<span style="color: #006633;">JSONResponse</span>.<span style="color: #006633;">APPLICATION_JSON_UTF8</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">apache</span>.<span style="color: #006633;">sling</span>.<span style="color: #006633;">api</span>.<span style="color: #006633;">servlets</span>.<span style="color: #006633;">HttpConstants</span>.<span style="color: #006633;">METHOD_GET</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* A demonstration sling servlet resource type class to demonstrate unit testing.<br />
&nbsp;* Appending &quot;.example.json&quot; on any resource will activate the doGet() method below, and return Json data.<br />
&nbsp;* Json Data returned is data from the immediate children of the &quot;/content/we-retail', parent resource node.<br />
&nbsp;*/</span><br />
@<span style="color: #003399;">Component</span><span style="color: #009900;">&#40;</span>service <span style="color: #339933;">=</span> Servlet.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
@SlingServletResourceTypes<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; resourceTypes <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;sling/servlet/default&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; methods <span style="color: #339933;">=</span> METHOD_GET,<br />
&nbsp; &nbsp; &nbsp; &nbsp; extensions <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;json&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; selectors <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;example&quot;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SlingServletResourceTypesExampleDS14Servlet <span style="color: #000000; font-weight: bold;">extends</span> SlingSafeMethodsServlet <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> doGet<span style="color: #009900;">&#40;</span>SlingHttpServletRequest req, SlingHttpServletResponse res<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; res.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span>APPLICATION_JSON_UTF8<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; res.<span style="color: #006633;">setStatus</span><span style="color: #009900;">&#40;</span>SlingHttpServletResponse.<span style="color: #006633;">SC_OK</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>PageItem<span style="color: #339933;">&gt;</span> pageItems <span style="color: #339933;">=</span> getPageItems<span style="color: #009900;">&#40;</span>req<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> json <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ObjectMapper<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">writeValueAsString</span><span style="color: #009900;">&#40;</span>pageItems<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; res.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>json<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>PageItem<span style="color: #339933;">&gt;</span> getPageItems<span style="color: #009900;">&#40;</span>SlingHttpServletRequest req<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>PageItem<span style="color: #339933;">&gt;</span> pageItems <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ResourceResolver resolver <span style="color: #339933;">=</span> req.<span style="color: #006633;">getResourceResolver</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Resource weRetail <span style="color: #339933;">=</span> resolver.<span style="color: #006633;">getResource</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/content/we-retail&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>weRetail <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Iterator<span style="color: #339933;">&lt;</span>Resource<span style="color: #339933;">&gt;</span> it <span style="color: #339933;">=</span> weRetail.<span style="color: #006633;">listChildren</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>it.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Resource resource <span style="color: #339933;">=</span> it.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>resource.<span style="color: #006633;">getResourceType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equalsIgnoreCase</span><span style="color: #009900;">&#40;</span>com.<span style="color: #006633;">day</span>.<span style="color: #006633;">cq</span>.<span style="color: #006633;">wcm</span>.<span style="color: #006633;">api</span>.<span style="color: #006633;">NameConstants</span>.<span style="color: #006633;">NT_PAGE</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Page page <span style="color: #339933;">=</span> resource.<span style="color: #006633;">adaptTo</span><span style="color: #009900;">&#40;</span>Page.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>page <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pageItems.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> PageItem<span style="color: #009900;">&#40;</span>page.<span style="color: #006633;">getTitle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, resource.<span style="color: #006633;">getPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> pageItems<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Inner class PageItem, for creating PageItem objects.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PageItem <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> pageTitle<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> pagePath<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; PageItem<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> pageTitle, <span style="color: #003399;">String</span> pagePath<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pageTitle</span> <span style="color: #339933;">=</span> pageTitle<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pagePath</span> <span style="color: #339933;">=</span> pagePath<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getPageTitle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> pageTitle<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getPagePath<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> pagePath<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</div>
<p>Results of the output from the working as expected SlingServletResourceTypesTest.class.<br />
<a href="https://sourcedcode.com/wp-content/uploads/2019/12/sling-servlet-resource-type-unit-test-example-results-json.png"><img decoding="async" src="https://sourcedcode.com/wp-content/uploads/2019/12/sling-servlet-resource-type-unit-test-example-results-json-1024x933.png" alt="Sling Servlet Resource TypeResults Example" width="200" /></a></p>
<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Horizontal Preview Ad #6 --><br />
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-9053803095882933"
     data-ad-slot="2559251592"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins><br />
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<hr class="spacer-large" />
<h3>Sling Servlet Resource Types Test Class : SlingServletResourceTypesTest.class</h3>
<div class="code-large">
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourcedcode.core.servlets</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.fasterxml.jackson.databind.JsonNode</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.fasterxml.jackson.databind.ObjectMapper</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">io.wcm.testing.mock.aem.junit.AemContext</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.testing.mock.sling.ResourceResolverType</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.Before</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.Rule</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.Test</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.runner.RunWith</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.mockito.InjectMocks</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.mockito.Mock</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.mockito.junit.MockitoJUnitRunner</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> com.<span style="color: #006633;">day</span>.<span style="color: #006633;">crx</span>.<span style="color: #006633;">packaging</span>.<span style="color: #006633;">JSONResponse</span>.<span style="color: #006633;">APPLICATION_JSON_UTF8</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> junitx.<span style="color: #006633;">framework</span>.<span style="color: #000000; font-weight: bold;">Assert</span>.<span style="color: #006633;">assertEquals</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">junit</span>.<span style="color: #000000; font-weight: bold;">Assert</span>.<span style="color: #006633;">assertTrue</span><span style="color: #339933;">;</span><br />
<br />
@RunWith<span style="color: #009900;">&#40;</span>MockitoJUnitRunner.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SlingServletResourceTypesExampleDS14ServletTest <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; @Rule<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> AemContext context <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AemContext<span style="color: #009900;">&#40;</span>ResourceResolverType.<span style="color: #006633;">JCR_MOCK</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Mock<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> MockSlingHttpServletRequest req<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Mock<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> MockSlingHttpServletResponse res<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @InjectMocks<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> SlingServletResourceTypesExampleDS14Servlet underTest<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Before<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; underTest <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SlingServletResourceTypesExampleDS14Servlet<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; req <span style="color: #339933;">=</span> context.<span style="color: #006633;">request</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; res <span style="color: #339933;">=</span> context.<span style="color: #006633;">response</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doGet_shouldReturnHeaderAsExpected<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; underTest.<span style="color: #006633;">doGet</span><span style="color: #009900;">&#40;</span>req, res<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span>res.<span style="color: #006633;">getContentType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, APPLICATION_JSON_UTF8<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doGet_shouldReturnPageItemListJsonAsExpected_0<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; underTest.<span style="color: #006633;">doGet</span><span style="color: #009900;">&#40;</span>req, res<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> jsonString <span style="color: #339933;">=</span> res.<span style="color: #006633;">getOutputAsString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ObjectMapper mapper <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ObjectMapper<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; JsonNode actualObj <span style="color: #339933;">=</span> mapper.<span style="color: #006633;">readTree</span><span style="color: #009900;">&#40;</span>jsonString<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, actualObj.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doGet_shouldReturnPageItemJsonAsExpected_properties_values<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; createPagesInJcrMock<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; underTest.<span style="color: #006633;">doGet</span><span style="color: #009900;">&#40;</span>req, res<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> jsonString <span style="color: #339933;">=</span> res.<span style="color: #006633;">getOutputAsString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ObjectMapper mapper <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ObjectMapper<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; JsonNode actualObj <span style="color: #339933;">=</span> mapper.<span style="color: #006633;">readTree</span><span style="color: #009900;">&#40;</span>jsonString<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; JsonNode firstItem <span style="color: #339933;">=</span> actualObj.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertTrue<span style="color: #009900;">&#40;</span>firstItem.<span style="color: #006633;">has</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;pageTitle&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertTrue<span style="color: #009900;">&#40;</span>firstItem.<span style="color: #006633;">has</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;pagePath&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;United states&quot;</span>, firstItem.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;pageTitle&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">textValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/content/we-retail/us&quot;</span>, firstItem.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;pagePath&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">textValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Test helper method to create 3 pages in the on-memory JCR Mock instance.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> createPagesInJcrMock<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">page</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/content/we-retail&quot;</span>, <span style="color: #0000ff;">&quot;/&quot;</span>, <span style="color: #0000ff;">&quot;We Retail&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">page</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/content/we-retail/us&quot;</span>, <span style="color: #0000ff;">&quot;/&quot;</span>, <span style="color: #0000ff;">&quot;United states&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">page</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/content/we-retail/ca&quot;</span>, <span style="color: #0000ff;">&quot;/&quot;</span>, <span style="color: #0000ff;">&quot;Canada&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</div>
<div class="mention-block">
<strong>Last Thoughts:</strong><br />
Though the endpoint path is ugly (because it consists of extensions, selectors, and etc&#8230;), it is recomended to register servlet by resource types rather than by path (<a href="https://sourcedcode.com/registering-slingservletpaths-component-property-type">click here to learn why @SlingServletPaths, register by path, are not recommended</a>).</p>
<p>If there is a requirement to mask or to sugarcoat the ugly URI paths, a common strategy used is the Apache webserver. <a href="/sugar-coating-servlet-scripts-and-paths">Click here to learn to sugar coat registered AEM servlet scripts and paths endpoint</a>.
</div>
<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Horizontal Preview Ad #2 --><br />
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-9053803095882933"
     data-ad-slot="2263738126"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins><br />
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/aem-sling-servlet-osgi-r7-by-resource-type-unit-test-junit-4-with-examples">JUnit 4: AEM Sling Servlet Unit Test Example Using wcm.io AEM Mocks, Servlet by Resource Type</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/aem-sling-servlet-osgi-r7-by-resource-type-unit-test-junit-4-with-examples/feed</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>JUnit 4: AEM WCMUsePojo Unit Test Example (Passing Paramaters)</title>
		<link>https://sourcedcode.com/blog/aem/junit-4-aem-wcmusepojo-unit-test-example-passing-paramaters</link>
					<comments>https://sourcedcode.com/blog/aem/junit-4-aem-wcmusepojo-unit-test-example-passing-paramaters#respond</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Wed, 25 Sep 2019 20:35:18 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[JUNIT]]></category>
		<category><![CDATA[JUNIT4]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=528</guid>

					<description><![CDATA[<p>This article will demonstrate how to write AEM Unit tests for building WCMUsePojo Java classes using the Junit4 testing framework. With developers being more visual, the source code is posted below. In this example, I am building a Sightly utils class that helps me append .html to page path that are configured by the author. [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/junit-4-aem-wcmusepojo-unit-test-example-passing-paramaters">JUnit 4: AEM WCMUsePojo Unit Test Example (Passing Paramaters)</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>This article will demonstrate how to write AEM Unit tests for building WCMUsePojo Java classes using the Junit4 testing framework. With developers being more visual, the source code is posted below.</p>
<p>In this example, I am building a Sightly utils class that helps me append .html to page path that are configured by the author. The utils class can be used by any AEM components during development. A value is passed through a parameter in this demo.</p>
<div class="mention-block">
Technologies here used are:</p>
<ul>
<li>AEM project archetype 19 (<a href="https://github.com/adobe/aem-project-archetype/tree/aem-project-archetype-19" target="_BLANK" rel="noopener noreferrer">link</a>)</li>
</ul>
</div>
<p>This example uses the AEM project archetype 19 to generate a new AEM project, Junit 4 will be used as the testing framework.</p>
<h3 id="sightly-example">Sightly Example Use:</h3>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&lt;!-- somecomponent --&gt;<br />
&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;somecomponent&quot;</span>&gt;<br />
&nbsp; &nbsp; &lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${lnk.link}&quot;</span> <span style="color: #000066;">target</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;_blank&quot;</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;noopener noreferrer&quot;</span>&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; Click Here<br />
&nbsp; &nbsp; &lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;<br />
&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</div></td></tr></tbody></table></div>
<h3 id="wcmusepojo-example">WCMUsePojo Example Code:</h3>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourcedcode.core.wcm.utils</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.adobe.cq.sightly.WCMUsePojo</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.lang3.StringUtils</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> LinkChecker <span style="color: #000000; font-weight: bold;">extends</span> WCMUsePojo <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> LINK <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;link&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> link<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> activate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; resolveLink<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Resolves the path of a JCR page path with a .html extension<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> resolveLink<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; link <span style="color: #339933;">=</span> get<span style="color: #009900;">&#40;</span>LINK, <span style="color: #003399;">String</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>StringUtils.<span style="color: #006633;">isNotEmpty</span><span style="color: #009900;">&#40;</span>link<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>link.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;.html&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; link <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot;.html&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getLink<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> link<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<h3 id="wcmusepojo-unit-test-example">WCMUsePojo Unit Test Example Code:</h3>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourcedcode.core.wcm.utils</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.Test</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.script.Bindings</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> junitx.<span style="color: #006633;">framework</span>.<span style="color: #000000; font-weight: bold;">Assert</span>.<span style="color: #006633;">assertEquals</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">mockito</span>.<span style="color: #006633;">Mockito</span>.<span style="color: #006633;">mock</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">mockito</span>.<span style="color: #006633;">Mockito</span>.<span style="color: #006633;">when</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> LinkCheckerTest <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testLinkValueWithNoHTML<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Bindings bindings <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>Bindings.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>bindings.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;link&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/content/sourcedcode/home&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; LinkChecker use <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LinkChecker<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; use.<span style="color: #006633;">init</span><span style="color: #009900;">&#40;</span>bindings<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/content/sourcedcode/home.html&quot;</span>, use.<span style="color: #006633;">getLink</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testLinkValueWithHTML<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Bindings bindings <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>Bindings.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>bindings.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;link&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/content/sourcedcode/home.html&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; LinkChecker use <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LinkChecker<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; use.<span style="color: #006633;">init</span><span style="color: #009900;">&#40;</span>bindings<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/content/sourcedcode/home.html&quot;</span>, use.<span style="color: #006633;">getLink</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<div class="mention-block">
<strong>Tip:</strong><br />
I hope the example above helps you out with your development of Java Classes that extend the WCMUsePojo Class. Instead of writing a new WCMUsePojo Class, you can utilise magic that sightly 1.4 offers.</p>
<p>Sightly 1.4 offers a quicker way for you to add extensions to a given url, as examples below:</p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">${'sourcedcode/home' @ extension='html'}<br />
<span style="color: #808080; font-style: italic;">&lt;!-- outputs: sourcedcode/home.html --&gt;</span><br />
<br />
${'sourcedcode/home.json' @ extension='html'}<br />
<span style="color: #808080; font-style: italic;">&lt;!-- outputs: sourcedcode/home.html --&gt;</span><br />
<br />
${'sourcedcode/home.selector.json' @ extension='html'}<br />
<span style="color: #808080; font-style: italic;">&lt;!-- outputs: sourcedcode/home.selector.html --&gt;</span><br />
<br />
${'sourcedcode/home.json/suffix' @ extension='html'}<br />
<span style="color: #808080; font-style: italic;">&lt;!-- outputs: sourcedcode/home.html/suffix --&gt;</span><br />
<br />
${'sourcedcode/home.json?key=value' @ extension='html'}<br />
<span style="color: #808080; font-style: italic;">&lt;!-- outputs: sourcedcode/home.html?key=value --&gt;</span><br />
<br />
${'sourcedcode/home.json#fragment' @ extension='html'}<br />
<span style="color: #808080; font-style: italic;">&lt;!-- outputs: sourcedcode/home.html#fragment --&gt;</span><br />
<br />
${'sourcedcode/home.json' @ extension}<br />
<span style="color: #808080; font-style: italic;">&lt;!-- outputs: sourcedcode/home --&gt;</span><br />
<br />
// documentation<br />
https://github.com/adobe/htl-spec/blob/master/SPECIFICATION.md</div></td></tr></tbody></table></div>
</div>
<div class="mention-block">
<strong>Common Questions for Writing Test Code for WCMUsePojo Classes</strong></p>
<ul>
<li>How do I pass a parameter to the WCMUsePojo class in AEM? Call the fully qualified java class name in sightly, and use the @ symbol to pass the parameter. Take a look a the example above <a href="#wcmusepojo-example">example</a></li>
</ul>
</div>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/junit-4-aem-wcmusepojo-unit-test-example-passing-paramaters">JUnit 4: AEM WCMUsePojo Unit Test Example (Passing Paramaters)</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/junit-4-aem-wcmusepojo-unit-test-example-passing-paramaters/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>JUnit 4: AEM Sling Models Unit Test Example Using wcm.io AEM Mocks</title>
		<link>https://sourcedcode.com/blog/aem/aem-sling-models-unit-test-junit-4-with-examples</link>
					<comments>https://sourcedcode.com/blog/aem/aem-sling-models-unit-test-junit-4-with-examples#comments</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Mon, 27 May 2019 12:31:40 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[JUNIT]]></category>
		<category><![CDATA[JUNIT4]]></category>
		<category><![CDATA[Sling Models]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=346</guid>

					<description><![CDATA[<p>Creating new AEM components, we sometimes need backend logic to compute user requests with business logic. There are multiple ways of doing so, like using the Java-Use API or Javascript-Use API, but the most popular and best practice of writing business logic for an AEM component will be using Sling Models. This article will demonstrate [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/aem-sling-models-unit-test-junit-4-with-examples">JUnit 4: AEM Sling Models Unit Test Example Using wcm.io AEM Mocks</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Creating new AEM components, we sometimes need backend logic to compute user requests with business logic. There are multiple ways of doing so, like using the Java-Use API or Javascript-Use API, but the <strong>most popular and best practice of writing business logic for an AEM component will be using Sling Models</strong>.  </p>
<p>This article will demonstrate how to write AEM Unit tests for sling models using the Junit4 testing framework. With developers being more visual, the source code is posted below.</p>
<div class="mention-block">
Technologies here used are:</p>
<ul>
<li>AEM project archetype 19 (<a href="https://github.com/adobe/aem-project-archetype/tree/aem-project-archetype-19" target="_BLANK" rel="noopener noreferrer">link</a>)</li>
<li>Mockito 2.27.0 (<a href="https://mvnrepository.com/artifact/org.mockito/mockito-core/2.27.0" target="_BLANK" rel="noopener noreferrer">link</a>)</li>
<li>AEM Mocks JUnit 4 2.7.2 (<a href="https://mvnrepository.com/artifact/io.wcm/io.wcm.testing.aem-mock.junit4/2.7.2" target="_BLANK" rel="noopener noreferrer">link</a>)</li>
</ul>
</div>
<p>This example uses the AEM project archetype 19 to generate a new AEM project, Junit 4 will be used as the testing framework, Mockito 2.27.0 will be used as the mocking framework, and AEM Mocks will be used to mock AEM objects and AEM API&#8217;s.</p>
<p>What&#8217;s really great about the latest versions of AEM mocks, is that the setup is very minimal. After spinning up a new AEM project from the AEM project archetype 19, you simply need to include the AEM Mocks dependency, and you are ready to go!</p>
<h3>Dependencies</h3>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #666666; font-style: italic;">// pom.xml</span><br />
<span style="color: #339933;">&lt;!--</span> Maven Surefire Plugin <span style="color: #339933;">--&gt;</span><br />
<span style="color: #339933;">&lt;</span>plugin<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>groupId<span style="color: #339933;">&gt;</span>org.<span style="color: #006633;">apache</span>.<span style="color: #006633;">maven</span>.<span style="color: #006633;">plugins</span><span style="color: #339933;">&lt;/</span>groupId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>artifactId<span style="color: #339933;">&gt;</span>maven<span style="color: #339933;">-</span>surefire<span style="color: #339933;">-</span>plugin<span style="color: #339933;">&lt;/</span>artifactId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>version<span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">2.20</span><span style="color: #339933;">&lt;/</span>version<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>configuration<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>junitArtifactName<span style="color: #339933;">&gt;</span>junit<span style="color: #339933;">:</span>junit<span style="color: #339933;">:</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">&lt;/</span>junitArtifactName<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>configuration<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;/</span>plugin<span style="color: #339933;">&gt;</span><br />
...<br />
<span style="color: #339933;">&lt;</span>dependencies<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; ...<br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>dependency<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>groupId<span style="color: #339933;">&gt;</span>io.<span style="color: #006633;">wcm</span><span style="color: #339933;">&lt;/</span>groupId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>artifactId<span style="color: #339933;">&gt;</span>io.<span style="color: #006633;">wcm</span>.<span style="color: #006633;">testing</span>.<span style="color: #006633;">aem</span><span style="color: #339933;">-</span>mock<span style="color: #339933;">&lt;/</span>artifactId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>version<span style="color: #339933;">&gt;</span>2.7.2<span style="color: #339933;">&lt;/</span>version<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>scope<span style="color: #339933;">&gt;</span>test<span style="color: #339933;">&lt;/</span>scope<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>dependency<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; ...<br />
<span style="color: #339933;">&lt;/</span>dependencies<span style="color: #339933;">&gt;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// core/pom.xml</span><br />
<span style="color: #339933;">&lt;</span>dependencies<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; ...<br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>dependency<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>groupId<span style="color: #339933;">&gt;</span>io.<span style="color: #006633;">wcm</span><span style="color: #339933;">&lt;/</span>groupId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>artifactId<span style="color: #339933;">&gt;</span>io.<span style="color: #006633;">wcm</span>.<span style="color: #006633;">testing</span>.<span style="color: #006633;">aem</span><span style="color: #339933;">-</span>mock<span style="color: #339933;">&lt;/</span>artifactId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>dependency<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; ...<br />
<span style="color: #339933;">&lt;/</span>dependencies<span style="color: #339933;">&gt;</span></div></td></tr></tbody></table></div>
<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Horizontal Preview Ad --><br />
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-9053803095882933"
     data-ad-slot="8658919803"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins><br />
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<h3>Sling Model Class : Header.class</h3>
<div class="code-large">
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourcedcode.core.models</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.day.cq.wcm.api.Page</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.resource.Resource</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.resource.ResourceResolver</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.models.annotations.DefaultInjectionStrategy</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.models.annotations.Model</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.models.annotations.injectorspecific.ChildResource</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.models.annotations.injectorspecific.ScriptVariable</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.models.annotations.injectorspecific.ValueMapValue</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.annotation.PostConstruct</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.inject.Inject</span><span style="color: #339933;">;</span><br />
<br />
@Model<span style="color: #009900;">&#40;</span>adaptables <span style="color: #339933;">=</span> Resource.<span style="color: #000000; font-weight: bold;">class</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; resourceType <span style="color: #339933;">=</span> Header.<span style="color: #006633;">RESOURCE_TYPE</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; defaultInjectionStrategy <span style="color: #339933;">=</span> DefaultInjectionStrategy.<span style="color: #006633;">OPTIONAL</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Header <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> RESOURCE_TYPE <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;sourcedcode/components/structure/header&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @ValueMapValue<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> contactUsPath<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @ScriptVariable<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;currentPage&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> Page currentPage<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @ChildResource<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;link&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; Resource childResource<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @SlingObject<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> ResourceResolver resolver<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> contactUsPageSecretChar<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @PostConstruct<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; setContactUsPageSecretChar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> setContactUsPageSecretChar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Resource resource <span style="color: #339933;">=</span> resolver.<span style="color: #006633;">getResource</span><span style="color: #009900;">&#40;</span>contactUsPath<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>resource <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Page contactUsPage <span style="color: #339933;">=</span> resource.<span style="color: #006633;">adaptTo</span><span style="color: #009900;">&#40;</span>Page.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contactUsPageSecretChar <span style="color: #339933;">=</span> contactUsPage.<span style="color: #006633;">getTitle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getContactUsPageSecretChar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> contactUsPageSecretChar.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span>contactUsPageSecretChar.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// demo of testing the @ScriptVariable(&quot;currentPage&quot;) annotation</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getPageTitle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> currentPage.<span style="color: #006633;">getPageTitle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// demo of testing the @ChildResource annotation</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getChildLinkPropFlag<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> childResource.<span style="color: #006633;">getValueMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;flag&quot;</span>, <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</div>
<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Horizontal Preview Ad #2 --><br />
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-9053803095882933"
     data-ad-slot="2263738126"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins><br />
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<h3>Sling Model Test Class : HeaderTest.class</h3>
<div class="code-large">
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourcedcode.core.models</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.adobe.cq.commerce.common.ValueMapDecorator</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.day.cq.wcm.api.Page</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.common.collect.ImmutableMap</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">io.wcm.testing.mock.aem.junit.AemContext</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.resource.Resource</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.testing.mock.sling.ResourceResolverType</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.Rule</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.Test</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.runner.RunWith</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.mockito.InjectMocks</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.mockito.Mock</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.mockito.junit.MockitoJUnitRunner</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> junitx.<span style="color: #006633;">framework</span>.<span style="color: #000000; font-weight: bold;">Assert</span>.<span style="color: #006633;">assertEquals</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">mockito</span>.<span style="color: #006633;">Mockito</span>.<span style="color: #006633;">when</span><span style="color: #339933;">;</span><br />
<br />
@RunWith<span style="color: #009900;">&#40;</span>MockitoJUnitRunner.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HeaderTest <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; @Rule<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> AemContext context <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AemContext<span style="color: #009900;">&#40;</span>ResourceResolverType.<span style="color: #006633;">JCR_MOCK</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// the context.resourceResolver() is auto injected by the AemContext, cannot be mocked.</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// ResourceResolver resolver;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// mocking the global AEM object &quot;currentPage&quot;.</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// variable does not need to match the variables in the underTest.class</span><br />
&nbsp; &nbsp; @Mock<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> Page currentPage<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// injects all the mocks into the tested object.</span><br />
&nbsp; &nbsp; @InjectMocks<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> Header underTest<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> itShouldReturnTheCorrectSecretCharWhenResourceExist<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// using the AEM context to create an AEM resource in the context, to set properties for the resource.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// the resource path can be anything made up.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Resource headerResourceContext <span style="color: #339933;">=</span> context.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">resource</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/content/sourcedcode/home/jcr:content/header&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> ValueMapDecorator<span style="color: #009900;">&#40;</span>ImmutableMap.<span style="color: #339933;">&lt;</span><span style="color: #003399;">String</span>, Object<span style="color: #339933;">&gt;</span> of<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;contactUsPath&quot;</span>, <span style="color: #0000ff;">&quot;/content/sourcedcode/contact&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;anotherProperty&quot;</span>, <span style="color: #0000ff;">&quot;example&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// create mock page, resolved by the resolver.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">page</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/content/sourcedcode/contact&quot;</span>, <span style="color: #0000ff;">&quot;&quot;</span>, ImmutableMap.<span style="color: #339933;">&lt;</span><span style="color: #003399;">String</span>, Object<span style="color: #339933;">&gt;</span>builder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;jcr:title&quot;</span>, <span style="color: #0000ff;">&quot;Contact Us Page&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; underTest <span style="color: #339933;">=</span> headerResourceContext.<span style="color: #006633;">adaptTo</span><span style="color: #009900;">&#40;</span>Header.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;e&quot;</span>, underTest.<span style="color: #006633;">getContactUsPageSecretChar</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> itShouldReturnTheCorrectCurrentPageTitle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; when<span style="color: #009900;">&#40;</span>currentPage.<span style="color: #006633;">getPageTitle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Home Page&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Home Page&quot;</span>, underTest.<span style="color: #006633;">getPageTitle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> itShouldReturnTheCorrectChildLinkProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">resource</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/content/sourcedcode/home/jcr:content/header&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006633;">siblingsMode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006633;">resource</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;link&quot;</span>, <span style="color: #0000ff;">&quot;flag&quot;</span>, <span style="color: #0000ff;">&quot;newPage&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; underTest <span style="color: #339933;">=</span> context.<span style="color: #006633;">resourceResolver</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getResource</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/content/sourcedcode/home/jcr:content/header&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">adaptTo</span><span style="color: #009900;">&#40;</span>Header.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;newPage&quot;</span>, underTest.<span style="color: #006633;">getChildLinkPropFlag</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</div>
<div class="mention-block">
<strong>Common Questions for Writing Test Code for Sling Models</strong></p>
<ul>
<li>How do I initial properties in my sling model object? First, ensure that your sling model allows a resource.class to be adaptable, then in your sling model test class, create a mockResource object, setup up the mockResource object, and adapt to the sling model class that you are trying to test.</a></li>
<li>Which Context should I be used when testing for sling models? You should use the JCR_MOCK context.</li>
</ul>
<p><strong>For AEM Sling Servlet by Resource Type, Unit Test Example Using wcm.io AEM Mocks, <a href="/aem-sling-servlet-osgi-r7-by-resource-type-unit-test-junit-4-with-examples" target="_blank" rel="noopener noreferrer">click here</a>.</strong>
</div>
<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Horizontal Preview Ad #3 --><br />
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-9053803095882933"
     data-ad-slot="3385248102"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins><br />
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/aem-sling-models-unit-test-junit-4-with-examples">JUnit 4: AEM Sling Models Unit Test Example Using wcm.io AEM Mocks</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/aem-sling-models-unit-test-junit-4-with-examples/feed</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
			</item>
	</channel>
</rss>
