{"id":3714,"date":"2025-03-12T16:14:23","date_gmt":"2025-03-12T15:14:23","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=3714"},"modified":"2025-03-12T16:14:23","modified_gmt":"2025-03-12T15:14:23","slug":"efficiently-running-specific-java-test-classes","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2025\/03\/12\/efficiently-running-specific-java-test-classes\/","title":{"rendered":"Efficiently Running Specific Java Test Classes"},"content":{"rendered":"\n<p>Testing specific components in a project is often necessary during development to ensure targeted functionality and keep focus on the changes we just made previously. Let us assume, we need to run tests for the <code>GetAllowedBdoControllerTest.java<\/code> file for example , there are several methods depending on our tools and testing framework. Below, we explore how to execute these tests efficiently.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h4>I &#8211; <strong>Running Tests Using our IDE<\/strong><\/h4>\n\n\n\n<p>Modern Integrated Development Environments (IDEs) like IntelliJ IDEA, Eclipse, or Visual Studio Code make it easy to execute specific tests.<\/p>\n\n\n\n<h5><strong><span class=\"has-inline-color has-vivid-cyan-blue-color\">In IntelliJ IDEA<\/span><\/strong><\/h5>\n\n\n\n<ul><li>Open the&nbsp;<code>GetAllowedBdoControllerTest.java<\/code>&nbsp;file.<\/li><li>Look for the green play button (?) next to the class name or individual test methods.<\/li><li>Click the play button:<ul><li><strong>Next to the class name<\/strong>: Runs all tests in the file.<\/li><li><strong>Next to a specific method<\/strong>: Runs only that test method.<\/li><\/ul><\/li><li>The test results will appear in the&nbsp;<strong>Run window<\/strong>.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h5><strong><span class=\"has-inline-color has-vivid-cyan-blue-color\">In Eclipse<\/span><\/strong><\/h5>\n\n\n\n<ul><li>Right-click on the\u00a0<code>GetAllowedBdoControllerTest.java<\/code>\u00a0file in the\u00a0<strong>Project Explorer<\/strong>\u00a0or open it in the editor.<\/li><li>Select\u00a0<strong>Run As > JUnit Test<\/strong>.<\/li><li>This will execute all tests within the file.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h5><strong><span class=\"has-inline-color has-vivid-cyan-blue-color\">In Visual Studio Code<\/span><\/strong><\/h5>\n\n\n\n<ul><li>Ensure the&nbsp;<strong>Java Test Runner<\/strong>&nbsp;plugin is installed.<\/li><li>Open the file and look for the&nbsp;<strong>&#8220;Run Test&#8221;<\/strong>&nbsp;or&nbsp;<strong>&#8220;Debug Test&#8221;<\/strong>&nbsp;links above the class or test methods.<\/li><li>Click&nbsp;<strong>&#8220;Run Test&#8221;<\/strong>&nbsp;to execute the tests.<\/li><\/ul>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>II &#8211; <strong>Running Tests  with Maven<\/strong><\/h4>\n\n\n\n<p>If we are using Maven as our build tool, we can run a specific test class with the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mvn test -Dtest=GetAllowedBdoControllerTest\r<\/code><\/pre>\n\n\n\n<p>This command will execute only the tests defined within the <code>GetAllowedBdoControllerTest<\/code> class.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>III &#8211; <strong>Running Tests with Gradle<\/strong><\/h4>\n\n\n\n<p>For Gradle-based projects, we can execute a specific test class using the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/gradlew test --tests \"ch.sbb.itop.orchestration.bdo.controller.GetAllowedBdoControllerTest\"\r<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>IV &#8211; <strong>Executing Tests Directly with JUnit<\/strong><\/h4>\n\n\n\n<p>If we are running tests directly via the JUnit platform, we can use the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>java -jar junit-platform-console-standalone.jar \\\r\n    --class-path target\/test-classes \\\r\n    --select-class ch.sbb.itop.orchestration.bdo.controller.GetAllowedBdoControllerTest\r<\/code><\/pre>\n\n\n\n<p><strong>Note:<\/strong> Replace <code>target\/test-classes<\/code> with the correct path to your compiled test classes.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>V &#8211; <strong>Filtering Tests in CI\/CD Pipelines<\/strong><\/h4>\n\n\n\n<p>In Continuous Integration\/Continuous Deployment (CI\/CD) scenarios, we can configure our pipeline to run only the specific test file:<\/p>\n\n\n\n<ul><li><strong>Maven<\/strong>: Update the&nbsp;<code>pom.xml<\/code>&nbsp;or pipeline configuration to include the&nbsp;<code>-Dtest<\/code>&nbsp;parameter.<\/li><li><strong>Gradle<\/strong>: Use the above Gradle command in your pipeline script.<\/li><\/ul>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h5>Conclusion<\/h5>\n\n\n\n<p>By using these methods, we can efficiently run tests for the <code>GetAllowedBdoControllerTest.java<\/code> file without executing other tests in our project. This approach saves time and resources, especially in large codebases. Choose the method that best fits your workflow and streamline your development process.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Testing specific components in a project is often necessary during development to ensure targeted functionality and keep focus on the changes we just made previously. Let us assume, we need to run tests for the GetAllowedBdoControllerTest.java file for example , there are several methods depending on our tools and testing framework. Below, we explore how [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3716,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[554,868],"tags":[201,979,981,982,977,975,978,976,980],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3714"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=3714"}],"version-history":[{"count":1,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3714\/revisions"}],"predecessor-version":[{"id":3717,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3714\/revisions\/3717"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/3716"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=3714"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3714"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3714"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}