{"id":1658,"date":"2021-06-05T11:31:24","date_gmt":"2021-06-05T11:31:24","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=1658"},"modified":"2021-06-07T06:03:30","modified_gmt":"2021-06-07T06:03:30","slug":"junit5-learning-how-to-write-the-code-the-right-way","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2021\/06\/05\/junit5-learning-how-to-write-the-code-the-right-way\/","title":{"rendered":"JUNIT5: Learning how to write the code the right way!"},"content":{"rendered":"<p>JUnit was developed by <strong>Kent Beck<\/strong> and <strong>Erich Gamma<\/strong>. Its first version was released in <strong>1997<\/strong>.<\/p>\n\n\n<figure class=\"wp-block-pullquote has-background has-pale-cyan-blue-background-color is-style-solid-color\"><blockquote class=\"has-text-color has-black-color\"><p><strong>JUnit 5 = <em>JUnit Platform<\/em> + <em>JUnit Jupiter<\/em> + <em>JUnit Vintage<\/em><\/strong><\/p><cite>https:\/\/junit.org\/junit5\/docs\/current\/user-guide\/<\/cite><\/blockquote><\/figure>\n\n\n\n<p>It is powerful testing framework, with many modules. These modules are part of three sub-projects<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Junit Platform<\/strong>: Serves as a foundation for <a href=\"https:\/\/junit.org\/junit5\/docs\/current\/user-guide\/#launcher-api\">launching testing frameworks<\/a> on the JVM.&nbsp;<\/li><li><strong>Junit Jupiter<\/strong>: It is the combination of the new <a href=\"https:\/\/junit.org\/junit5\/docs\/current\/user-guide\/#writing-tests\">programming model<\/a> and <a href=\"https:\/\/junit.org\/junit5\/docs\/current\/user-guide\/#extensions\">extension model<\/a> for writing tests and extensions in JUnit 5.<\/li><li><strong>Junit Vintage<\/strong>: Provides a <code>TestEngine<\/code> for running JUnit 3 and JUnit 4 based tests on the platform.<\/li><\/ul>\n\n\n\n<p>Let&#8217;s start a basic project structure<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mvn archetype:generate -DgroupId=samarthya.me -DartifactId=learnunit -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=true<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"940\" height=\"1024\" src=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/06\/Terminal-940x1024.png\" alt=\"\" class=\"wp-image-1661\" srcset=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/06\/Terminal-940x1024.png 940w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/06\/Terminal-275x300.png 275w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/06\/Terminal-768x836.png 768w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/06\/Terminal.png 1000w\" sizes=\"(max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-pullquote has-background is-style-solid-color\" style=\"background-color:#ff6900\"><blockquote class=\"has-text-color has-white-color\"><p>In previous <code>JUnit<\/code> versions, there was <strong>limited\/no<\/strong> support to allow parameters in test constructors or methods. One of the major changes in <code>JUnit 5<\/code> Jupiter was that both test constructors and test methods are now allowed to have parameters.<\/p><\/blockquote><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">POM: JUNIT4 Support<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;project xmlns=\"http:\/\/maven.apache.org\/POM\/4.0.0\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n  xsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/maven-v4_0_0.xsd\"&gt;\n  &lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\n  &lt;groupId&gt;samarthya.me&lt;\/groupId&gt;\n  &lt;artifactId&gt;learnunit&lt;\/artifactId&gt;\n  &lt;packaging&gt;jar&lt;\/packaging&gt;\n  &lt;version&gt;v1.0&lt;\/version&gt;\n  &lt;name&gt;learnunit&lt;\/name&gt;\n  &lt;url&gt;http:\/\/maven.apache.org&lt;\/url&gt;\n  &lt;dependencies&gt;\n    &lt;dependency&gt;\n      &lt;groupId&gt;junit&lt;\/groupId&gt;\n      &lt;artifactId&gt;junit&lt;\/artifactId&gt;\n      &lt;version&gt;3.8.1&lt;\/version&gt;\n      &lt;scope&gt;test&lt;\/scope&gt;\n    &lt;\/dependency&gt;\n  &lt;\/dependencies&gt;\n&lt;\/project&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Adding Libraries : JUNIT5<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;project xmlns=\"http:\/\/maven.apache.org\/POM\/4.0.0\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/maven-v4_0_0.xsd\"&gt;\n  &lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\n  &lt;groupId&gt;samarthya.me&lt;\/groupId&gt;\n  &lt;artifactId&gt;learnunit&lt;\/artifactId&gt;\n  &lt;packaging&gt;jar&lt;\/packaging&gt;\n  &lt;version&gt;v1.0&lt;\/version&gt;\n  &lt;name&gt;learnunit&lt;\/name&gt;\n  &lt;url&gt;http:\/\/maven.apache.org&lt;\/url&gt;\n  &lt;dependencies&gt;\n    &lt;dependency&gt;\n      &lt;groupId&gt;org.junit.jupiter&lt;\/groupId&gt;\n      &lt;artifactId&gt;junit-jupiter-api&lt;\/artifactId&gt;\n      &lt;version&gt;5.7.2&lt;\/version&gt;\n      &lt;scope&gt;test&lt;\/scope&gt;\n    &lt;\/dependency&gt;\n    &lt;dependency&gt;\n      &lt;groupId&gt;org.junit.jupiter&lt;\/groupId&gt;\n      &lt;artifactId&gt;junit-jupiter-engine&lt;\/artifactId&gt;\n      &lt;version&gt;5.7.2&lt;\/version&gt;\n      &lt;scope&gt;test&lt;\/scope&gt;\n    &lt;\/dependency&gt;\n    &lt;!-- https:\/\/mvnrepository.com\/artifact\/org.junit.vintage\/junit-vintage-engine --&gt;\n    &lt;dependency&gt;\n      &lt;groupId&gt;org.junit.vintage&lt;\/groupId&gt;\n      &lt;artifactId&gt;junit-vintage-engine&lt;\/artifactId&gt;\n      &lt;version&gt;5.7.2&lt;\/version&gt;\n      &lt;scope&gt;test&lt;\/scope&gt;\n    &lt;\/dependency&gt;\n  &lt;\/dependencies&gt;\n&lt;\/project&gt;\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">JUNIT<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">JUNIT4: Test<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package samarthya.me;\n\nimport junit.framework.Test;\nimport junit.framework.TestCase;\nimport junit.framework.TestSuite;\n\n\/**\n * Unit test for simple App.\n *\/\npublic class AppTest \n    extends TestCase\n{\n    \/**\n     * Create the test case\n     *\n     * @param testName name of the test case\n     *\/\n    public AppTest( String testName )\n    {\n        super( testName );\n    }\n\n    \/**\n     * @return the suite of tests being tested\n     *\/\n    public static Test suite()\n    {\n        return new TestSuite( AppTest.class );\n    }\n\n    \/**\n     * Rigourous Test :-)\n     *\/\n    public void testApp()\n    {\n        System.out.println(\"JUNIT4: Test\");\n        assertTrue( true );\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">JUNIT5: Test case<\/h3>\n\n\n\n<p>I will be using the <code>@Test<\/code> annotation that is from the package <code>org.junit.jupiter.api<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package samarthya.me;\n\nimport static org.junit.Assert.assertTrue;\n\nimport org.junit.jupiter.api.Test;\n\nclass AppTest5 {\n\n    @Test\n    void test() {\n        System.out.println(\"JUNIT5: Test\");\n        assertTrue(true);\n    }    \n}<\/code><\/pre>\n\n\n\n<p>Not much is there, but you can execute the suite and see the output. I am using Visual Studio Code and launching test gives the result as below.<\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"253\" height=\"149\" src=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/06\/Launch-tests.png\" alt=\"\" class=\"wp-image-1669\"\/><\/figure><\/div>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"166\" src=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/06\/Result-terminal-1024x166.png\" alt=\"\" class=\"wp-image-1666\" srcset=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/06\/Result-terminal-1024x166.png 1024w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/06\/Result-terminal-300x49.png 300w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/06\/Result-terminal-768x125.png 768w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/06\/Result-terminal-1536x250.png 1536w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/06\/Result-terminal.png 1612w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>You can try some failures as below too..<\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"237\" src=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/06\/Failure-test-1024x237.png\" alt=\"\" class=\"wp-image-1671\" srcset=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/06\/Failure-test-1024x237.png 1024w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/06\/Failure-test-300x70.png 300w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/06\/Failure-test-768x178.png 768w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/06\/Failure-test-1536x356.png 1536w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/06\/Failure-test.png 1591w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>Test Failure<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">SampleFunction<\/h2>\n\n\n\n<p>A trivial class with one sample function <code>willReturnOne()<\/code> that return one.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package samarthya.me;\n\npublic class SampleFunctions {\n    public int willReturnOne() {\n        return 1;\n    }\n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"240\" src=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/06\/samplefunction-test-1024x240.png\" alt=\"\" class=\"wp-image-1674\" srcset=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/06\/samplefunction-test-1024x240.png 1024w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/06\/samplefunction-test-300x70.png 300w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/06\/samplefunction-test-768x180.png 768w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/06\/samplefunction-test-1536x359.png 1536w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/06\/samplefunction-test.png 1607w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Help<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>TestingEngine: https:\/\/junit.org\/junit5\/docs\/current\/api\/org.junit.platform.engine\/org\/junit\/platform\/engine\/TestEngine.html<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>JUnit was developed by Kent Beck and Erich Gamma. Its first version was released in 1997. JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage https:\/\/junit.org\/junit5\/docs\/current\/user-guide\/ It is powerful testing framework, with many modules. These modules are part of three sub-projects Junit Platform: Serves as a foundation for launching testing frameworks on the [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1666,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"image","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[120,34],"tags":[188],"class_list":["post-1658","post","type-post","status-publish","format-image","has-post-thumbnail","hentry","category-java","category-technical","tag-junit","post_format-post-format-image"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/1658","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=1658"}],"version-history":[{"count":0,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/1658\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/1666"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=1658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=1658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=1658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}