<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mohd Kaif on Medium]]></title>
        <description><![CDATA[Stories by Mohd Kaif on Medium]]></description>
        <link>https://medium.com/@2024sl93018?source=rss-94f398e206ff------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*XQqsWFQcdbxWKbEW</url>
            <title>Stories by Mohd Kaif on Medium</title>
            <link>https://medium.com/@2024sl93018?source=rss-94f398e206ff------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 18:52:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@2024sl93018/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Writing and Running Unit Tests Using JUnit and TestNG]]></title>
            <link>https://medium.com/@2024sl93018/writing-and-running-unit-tests-using-junit-and-testng-a41bc7ae891c?source=rss-94f398e206ff------2</link>
            <guid isPermaLink="false">https://medium.com/p/a41bc7ae891c</guid>
            <category><![CDATA[junit]]></category>
            <category><![CDATA[sofwaretesting]]></category>
            <category><![CDATA[testng]]></category>
            <dc:creator><![CDATA[Mohd Kaif]]></dc:creator>
            <pubDate>Sat, 10 May 2025 06:59:33 GMT</pubDate>
            <atom:updated>2025-05-10T06:59:33.203Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZL73v6aavuR_fj6haTFkEA.png" /></figure><blockquote>“Code without tests is broken by design.” — Jacob Kaplan-Moss</blockquote><p>In modern software development, unit testing is not optional — it’s essential. Whether you’re fixing bugs, refactoring, or adding new features, having a solid test suite helps you catch regressions early, increase code quality, and ship with confidence.</p><p>In this post, we’ll explore how to write and run unit tests in Java using two of the most popular testing frameworks: <strong>JUnit</strong> and <strong>TestNG</strong>. You’ll learn the basics of both, when to use which, and how to integrate them into your workflow.</p><h3>What Is Unit Testing?</h3><p>Unit testing involves testing individual units of code (usually methods or classes) in isolation from the rest of the application. A good unit test is:</p><ul><li><strong>Fast</strong></li><li><strong>Independent</strong></li><li><strong>Repeatable</strong></li><li><strong>Deterministic</strong></li></ul><p>The goal is to verify that a specific piece of functionality behaves as expected.</p><h3><strong>JUnit: The De Facto Standard</strong></h3><h4>Setting Up JUnit</h4><p>For JUnit 5 (the latest), include the following Maven dependency:</p><pre>&lt;dependency&gt;<br>    &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;<br>    &lt;artifactId&gt;junit-jupiter&lt;/artifactId&gt;<br>    &lt;version&gt;5.10.0&lt;/version&gt;<br>    &lt;scope&gt;test&lt;/scope&gt;<br>&lt;/dependency&gt;</pre><p><strong>Writing a Test</strong></p><p>Let’s say you have a simple Calculator class:</p><pre>public class Calculator {<br>    public int add(int a, int b) {<br>    return a + b;<br>    }<br>    public int divide(int a, int b) {<br>        return a / b;<br>    }<br>}</pre><p>Create a test class:</p><pre>import org.junit.jupiter.api.Test;<br>import static org.junit.jupiter.api.Assertions.*;<br><br>class CalculatorTest {<br><br>    Calculator calc = new Calculator();<br><br>    @Test<br>    void testAddition() {<br>        assertEquals(5, calc.add(2, 3), &quot;2 + 3 should equal 5&quot;);<br>    }<br><br>    @Test<br>    void testDivisionByZero() {<br>        assertThrows(ArithmeticException.class, () -&gt; calc.divide(10, 0));<br>    }<br>}</pre><h4>Running Tests</h4><p>Most IDEs (like IntelliJ or Eclipse) have built-in support for JUnit.</p><p>In IntelliJ/Eclipse: Right-click the test class → Run</p><p>You can also run tests via Maven:</p><pre>mvn test</pre><h3>TestNG: A Powerful Alternative</h3><p>TestNG (Test Next Generation) is another powerful testing framework, offering more advanced features like test configuration, parallel execution, and data-driven testing.</p><h4>Setup</h4><p>Add this to your Maven pom.xml:</p><pre>&lt;dependency&gt;<br>    &lt;groupId&gt;org.testng&lt;/groupId&gt;<br>    &lt;artifactId&gt;testng&lt;/artifactId&gt;<br>    &lt;version&gt;7.9.0&lt;/version&gt;<br>    &lt;scope&gt;test&lt;/scope&gt;<br>&lt;/dependency&gt;</pre><h4>Writing a Test</h4><p>Here’s the same example in TestNG:</p><pre>import org.testng.annotations.Test;<br>import static org.testng.Assert.*;<br><br>public class CalculatorTest {<br><br>    Calculator calc = new Calculator();<br><br>    @Test<br>    public void testSubtraction() {<br>        assertEquals(calc.subtract(5, 3), 2);<br>    }<br><br>    @Test(expectedExceptions = ArithmeticException.class)<br>    public void testDivideByZero() {<br>        calc.divide(5, 0);<br>    }<br>}</pre><h3>Running Tests</h3><p>If using Maven:</p><pre>mvn test</pre><p>Or use testng.xml for suite-level configuration and control.</p><p><strong>JUnit vs TestNG: Which One to Use?</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-Zb4zZwBr_wVoZrJlApJrw.png" /></figure><p><strong>Recommendation:</strong></p><ul><li>Use <strong>JUnit</strong> if you’re working on a standard Spring Boot or Java project — it’s the default.</li><li>Use <strong>TestNG</strong> for more advanced test scenarios like functional or integration testing, especially where parallelism or configuration is key.</li></ul><h3>Best Practices for Unit Testing</h3><ul><li>Test one thing at a time.</li><li>Use meaningful method names like shouldReturnCorrectSum_whenAddingTwoNumbers().</li><li>Avoid external dependencies like databases or APIs in unit tests.</li><li>Aim for high <strong>branch coverage</strong> but don’t obsess over 100% — focus on <strong>meaningful coverage</strong>.</li><li>Use @BeforeEach, @AfterEach, and @BeforeAll wisely to avoid code duplication.</li></ul><h3>Conclusion</h3><p>Unit testing in Java is made seamless with tools like <strong>JUnit</strong> and <strong>TestNG</strong>. Whether you’re writing quick sanity checks or building an enterprise-level test suite, both frameworks offer rich features and integrations.</p><p>Invest in your tests as much as you invest in your code — they’ll save you hours of debugging and rework down the line.</p><p>Want to go deeper? Add tools like Mockito, JaCoCo (code coverage), and integrate your tests with GitHub Actions or Jenkins for full CI/CD magic.</p><p>👏 If you found this helpful, give it a clap (or 50) and share it with your fellow devs.</p><p>🧪 Happy Testing!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a41bc7ae891c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>