{"id":4148,"date":"2021-02-10T04:48:45","date_gmt":"2021-02-10T04:48:45","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=4148"},"modified":"2021-02-11T05:38:17","modified_gmt":"2021-02-11T05:38:17","slug":"junit-5-annotations","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/junit-5-annotations\/","title":{"rendered":"Junit 5 Annotations"},"content":{"rendered":"<ol>\n<li><strong>@BeforeAll, @AfterAll<\/strong> &#8211; Runs Before and After Class and should be static<\/li>\n<li><strong>@DisplayName<\/strong> &#8211; Displays description about the Test Method<\/li>\n<li><strong>@Disabled<\/strong> &#8211; @Ignore in Junit 4. Disables the Test Method<\/li>\n<li><strong>@Nested<\/strong> &#8211; Helps in grouping of similar test methods together<\/li>\n<li><strong>@ParameterizedTest<\/strong> &#8211; Supplying more than one input for same method in row using array<\/li>\n<li><strong>@ValueSource<\/strong> &#8211; Provides multiple paramters to same test method for ParameterizedTest<\/li>\n<li><strong>@CsvSource<\/strong> &#8211; Provides multiple paramters to same test method for ParameterizedTest in Key Value Pair. Key is Input and Value is expected Output<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2021\/02\/Junit5Annotations.jpg\" alt=\"\" \/><\/p>\n<p><strong>AccountUtils.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.io.IOException;\r\n\r\npublic class AccountUtils {\r\n\r\n    public boolean validateAccountId(String acctNo) throws IOException {\r\n        if (getAccountIDLength(acctNo)) {\r\n            return true;\r\n        } else {\r\n            throw new IOException(&quot;Account ID is Invalid&quot;);\r\n        }\r\n    }\r\n\r\n    public boolean getAccountIDLength(String acctNo)\r\n    {\r\n        if(acctNo.length() &lt; 5) return false;\r\n        if(acctNo.length() &gt; 10) return false;\r\n        if(!acctNo.contains(&quot;-&quot;)) return false;\r\n\r\n        return true;\r\n    }\r\n\r\n    public  String getFormattedAccID(String accountNo){\r\n        return accountNo.toUpperCase();\r\n    }\r\n\r\n    public String&#x5B;] getBankDetails(String accountNo){\r\n        String&#x5B;] arrAccountDetails = accountNo.split(&quot;-&quot;);\r\n        return arrAccountDetails;\r\n    }\r\n}\r\n\r\n<\/pre>\n<p><strong>AccountUtilsTest.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport org.junit.jupiter.api.*;\r\nimport org.junit.jupiter.params.ParameterizedTest;\r\nimport org.junit.jupiter.params.provider.CsvSource;\r\nimport org.junit.jupiter.params.provider.ValueSource;\r\n\r\nimport java.io.IOException;\r\n\r\nimport static org.junit.jupiter.api.Assertions.fail;\r\n\r\npublic class AccountUtilsTest {\r\n\r\n    AccountUtils systemUnderTest = new AccountUtils();\r\n\r\n    @BeforeAll\r\n    static void init(){\r\n        System.out.println(&quot;Initilize connection to Database&quot;);\r\n    }\r\n\r\n    @AfterAll\r\n    static void destroy(){\r\n        System.out.println(&quot;Deallocate connection to Database&quot;);\r\n    }\r\n\r\n    @Test\r\n    @DisplayName(&quot;AccountID Length Validation Success&quot;)\r\n    void test_getAccountIDLength_Success(){\r\n        boolean expectedOutput = true;\r\n        boolean actualOutput = systemUnderTest.getAccountIDLength(&quot;SBI-104526&quot;);\r\n\r\n        Assertions.assertEquals(expectedOutput, actualOutput);\r\n        Assertions.assertTrue(actualOutput);\r\n    }\r\n\r\n    @Test\r\n    @Disabled\r\n    void test_validateAccountId_Success() throws IOException {\r\n        Assertions.assertTrue(systemUnderTest.validateAccountId(&quot;SBI-104526&quot;));\r\n    }\r\n\r\n    @Test\r\n    @DisplayName(&quot;AccountID is Invalid &quot;)\r\n    void test_validateAccountId_Exception(){\r\n        Assertions.assertThrows(IOException.class, ()-&gt;{\r\n            systemUnderTest.validateAccountId(&quot;&quot;);\r\n        });\r\n    }\r\n\r\n    @Test\r\n    @DisplayName(&quot;Account Bank Details Validation&quot;)\r\n    void test_AccountIDForBankName_Success(){\r\n        String&#x5B;] expectedOutput = new String&#x5B;]{&quot;SBI&quot;, &quot;104526&quot;};\r\n        String&#x5B;] actualOutput = systemUnderTest.getBankDetails(&quot;SBI-104526&quot;);\r\n\r\n        Assertions.assertArrayEquals(expectedOutput, actualOutput);\r\n    }\r\n\r\n    @Nested\r\n    class checkValidAccountId\r\n    {\r\n        @ParameterizedTest\r\n        @ValueSource(strings={&quot;SBI&quot;, &quot;SBI-123456789&quot;, &quot; &quot;, &quot;&quot;})\r\n        @DisplayName(&quot;Parameterized Test AccountID Length Validation Failiure&quot;)\r\n        void test_getAccountIDLength_Failiure(String accountId){\r\n            Assertions.assertFalse(systemUnderTest.getAccountIDLength(accountId));\r\n        }\r\n\r\n        @ParameterizedTest(name=&quot;AccountID {0} formatted to {1}&quot;)\r\n        @CsvSource(value={&quot;sbi-123456, SBI-123456&quot;, &quot;cbi-123456, CBI-123456&quot;})\r\n        @DisplayName(&quot;AccountID Format Validation&quot;)\r\n        void getFormattedAccID_Success(String inputString, String expectedOutput){\r\n            Assertions.assertEquals(expectedOutput, systemUnderTest.getFormattedAccID(inputString));\r\n        }\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>@BeforeAll, @AfterAll &#8211; Runs Before and After Class and should be static @DisplayName &#8211; Displays description about the Test Method @Disabled &#8211; @Ignore in Junit 4. Disables the Test Method @Nested &#8211; Helps in grouping of similar test methods together @ParameterizedTest &#8211; Supplying more than one input for same method in row using array @ValueSource&hellip; <a href=\"https:\/\/codethataint.com\/blog\/junit-5-annotations\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[331],"tags":[],"class_list":["post-4148","post","type-post","status-publish","format-standard","hentry","category-junit5"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=4148"}],"version-history":[{"count":4,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4148\/revisions"}],"predecessor-version":[{"id":4153,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4148\/revisions\/4153"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=4148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=4148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=4148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}