<?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>JUNIT5 Archives - Sourced Code</title>
	<atom:link href="https://sourcedcode.com/blog/category/aem/junit/junit5/feed" rel="self" type="application/rss+xml" />
	<link>https://sourcedcode.com</link>
	<description>AEM Blog Made Just for You.</description>
	<lastBuildDate>Tue, 13 Aug 2024 04:26:53 +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>JUNIT5 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>
	</channel>
</rss>
