{"id":154,"date":"2024-07-30T06:50:31","date_gmt":"2024-07-30T06:50:31","guid":{"rendered":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/chapter\/using-testing-frameworks\/"},"modified":"2026-03-16T14:30:20","modified_gmt":"2026-03-16T14:30:20","slug":"using-testing-frameworks","status":"publish","type":"chapter","link":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/chapter\/using-testing-frameworks\/","title":{"raw":"Using Testing Frameworks","rendered":"Using Testing Frameworks"},"content":{"raw":"<div class=\"using-testing-frameworks\">\n<p class=\"import-Normal\">While you could write your own testing classes, you\u2019re generally better off utilizing a testing framework. There are a number to choose from to test Java programs and classes. Some of the most <a class=\"rId139\" href=\"https:\/\/www.freecodecamp.org\/news\/these-are-the-top-testing-tools-libraries-and-frameworks-for-java-developers-8c0e3f9bc11d\/\">popular frameworks include JUnit, Selenium, and TestNG<\/a>.<\/p>\n<p class=\"import-Normal\">The major benefit of using a framework is that it already contains a set of methods and annotations that make writing tests easier. Instead of rewriting conditionals for passing or failing a test, implementing a counter for passed versus failed test cases, or setting up complicated control flow scenarios to test for exceptions, the testing framework already provides these things. You simply write your test methods, run the main method, and the test library takes care of executing your test methods, tallying the passes and failures, and handling exceptions. The driver program discovers and run the unit tests. This helps ensure that tests are run in a consistent manner with consistent output.<\/p>\n<p class=\"import-Normal\">For this course, we will use TestNG.<\/p>\n\n<h4>TestNG Basics<\/h4>\n<p class=\"import-Normal\">The following sections will introduce example TestNG tests, but here\u2019s the basic gist.\u00a0 You do not need a main method; you simply write the test methods.\u00a0 How to run the tests depends on your development environment.\u00a0 Most IDE\u2019s have a testing plug-in that allows you to run the tests using the GUI. The next section explains how to run the tests using Gradle and VSCode.<\/p>\n<p class=\"import-Normal\">Below is a very simple test file that demonstrates the basic use case.<\/p>\n<p class=\"import-Normal\"><\/p>\n\n<\/div>\n<code><\/code>\n<pre>\/*\n* Simple TestNG example tests\n*\/\n\/*Import the required annotations (like @Test) and assert methods\n*\/\nimport org.testng.annotations.*;\nimport static org.testng.Assert.*;\n\n\/\/ Declare a class as your test class\n\npublic class MainTest {\n    \/*\n     * Each test method must be annotated with @Test to be run as a test method.\n     * You can have other methods in your test file, but only those marked\n     * with @Test\n     * Will be run and tallied in the test results\n     *\/\n\n    \/*\n     * test methods are generally named starting with test and describe\n     * what is being tested\n     *\/\n    @Test\n    public void testGetAge() {\n        int expectedAge = 4;\n        ClassToBeTested testObject = new ClassToBeTested(expectedAge);\n        \/\/ Checks that the return value of getAge is equal\n        \/\/ to the value of expectedValue\n        \/\/ assertEquals ( actual, expected)\n        assertEquals(testObject.getAge(), expectedAge);\n        \/*\n         * \n         * There are a number of overloaded assertEquals methods. Check out the TestNG\n         * documentation linked at the end of this section for a complete list.\n         * \n         *\/\n    }\n\n    \/**\n     * Verifies that Java.String\u2019s length method works correctly\n     *\/\n    @Test\n    public void testStringLength() {\n        String testObj = \"how long would this be\";\n        int expectedLength = 22;\n        assertEquals(testObj.length(), expectedLength);\n    }\n}\n<\/pre>\n&nbsp;","rendered":"<div class=\"using-testing-frameworks\">\n<p class=\"import-Normal\">While you could write your own testing classes, you\u2019re generally better off utilizing a testing framework. There are a number to choose from to test Java programs and classes. Some of the most <a class=\"rId139\" href=\"https:\/\/www.freecodecamp.org\/news\/these-are-the-top-testing-tools-libraries-and-frameworks-for-java-developers-8c0e3f9bc11d\/\">popular frameworks include JUnit, Selenium, and TestNG<\/a>.<\/p>\n<p class=\"import-Normal\">The major benefit of using a framework is that it already contains a set of methods and annotations that make writing tests easier. Instead of rewriting conditionals for passing or failing a test, implementing a counter for passed versus failed test cases, or setting up complicated control flow scenarios to test for exceptions, the testing framework already provides these things. You simply write your test methods, run the main method, and the test library takes care of executing your test methods, tallying the passes and failures, and handling exceptions. The driver program discovers and run the unit tests. This helps ensure that tests are run in a consistent manner with consistent output.<\/p>\n<p class=\"import-Normal\">For this course, we will use TestNG.<\/p>\n<h4>TestNG Basics<\/h4>\n<p class=\"import-Normal\">The following sections will introduce example TestNG tests, but here\u2019s the basic gist.\u00a0 You do not need a main method; you simply write the test methods.\u00a0 How to run the tests depends on your development environment.\u00a0 Most IDE\u2019s have a testing plug-in that allows you to run the tests using the GUI. The next section explains how to run the tests using Gradle and VSCode.<\/p>\n<p class=\"import-Normal\">Below is a very simple test file that demonstrates the basic use case.<\/p>\n<p class=\"import-Normal\">\n<\/div>\n<p><code><\/code><\/p>\n<pre>\/*\n* Simple TestNG example tests\n*\/\n\/*Import the required annotations (like @Test) and assert methods\n*\/\nimport org.testng.annotations.*;\nimport static org.testng.Assert.*;\n\n\/\/ Declare a class as your test class\n\npublic class MainTest {\n    \/*\n     * Each test method must be annotated with @Test to be run as a test method.\n     * You can have other methods in your test file, but only those marked\n     * with @Test\n     * Will be run and tallied in the test results\n     *\/\n\n    \/*\n     * test methods are generally named starting with test and describe\n     * what is being tested\n     *\/\n    @Test\n    public void testGetAge() {\n        int expectedAge = 4;\n        ClassToBeTested testObject = new ClassToBeTested(expectedAge);\n        \/\/ Checks that the return value of getAge is equal\n        \/\/ to the value of expectedValue\n        \/\/ assertEquals ( actual, expected)\n        assertEquals(testObject.getAge(), expectedAge);\n        \/*\n         * \n         * There are a number of overloaded assertEquals methods. Check out the TestNG\n         * documentation linked at the end of this section for a complete list.\n         * \n         *\/\n    }\n\n    \/**\n     * Verifies that Java.String\u2019s length method works correctly\n     *\/\n    @Test\n    public void testStringLength() {\n        String testObj = \"how long would this be\";\n        int expectedLength = 22;\n        assertEquals(testObj.length(), expectedLength);\n    }\n}\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"author":1,"menu_order":5,"template":"","meta":{"pb_show_title":"","pb_short_title":"","pb_subtitle":"","pb_authors":[],"pb_section_license":""},"chapter-type":[49],"contributor":[],"license":[],"class_list":["post-154","chapter","type-chapter","status-publish","hentry","chapter-type-numberless"],"part":145,"_links":{"self":[{"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/pressbooks\/v2\/chapters\/154","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/wp\/v2\/users\/1"}],"version-history":[{"count":1,"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/pressbooks\/v2\/chapters\/154\/revisions"}],"predecessor-version":[{"id":155,"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/pressbooks\/v2\/chapters\/154\/revisions\/155"}],"part":[{"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/pressbooks\/v2\/parts\/145"}],"metadata":[{"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/pressbooks\/v2\/chapters\/154\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/wp\/v2\/media?parent=154"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/pressbooks\/v2\/chapter-type?post=154"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/wp\/v2\/contributor?post=154"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/wp\/v2\/license?post=154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}