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 to execute these tests efficiently.
I – Running Tests Using our IDE
Modern Integrated Development Environments (IDEs) like IntelliJ IDEA, Eclipse, or Visual Studio Code make it easy to execute specific tests.
In IntelliJ IDEA
- Open the
GetAllowedBdoControllerTest.java
file. - Look for the green play button (?) next to the class name or individual test methods.
- Click the play button:
- Next to the class name: Runs all tests in the file.
- Next to a specific method: Runs only that test method.
- The test results will appear in the Run window.
In Eclipse
- Right-click on the
GetAllowedBdoControllerTest.java
file in the Project Explorer or open it in the editor. - Select Run As > JUnit Test.
- This will execute all tests within the file.
In Visual Studio Code
- Ensure the Java Test Runner plugin is installed.
- Open the file and look for the “Run Test” or “Debug Test” links above the class or test methods.
- Click “Run Test” to execute the tests.
II – Running Tests with Maven
If we are using Maven as our build tool, we can run a specific test class with the following command:
mvn test -Dtest=GetAllowedBdoControllerTest
This command will execute only the tests defined within the GetAllowedBdoControllerTest
class.
III – Running Tests with Gradle
For Gradle-based projects, we can execute a specific test class using the following command:
./gradlew test --tests "ch.sbb.itop.orchestration.bdo.controller.GetAllowedBdoControllerTest"
IV – Executing Tests Directly with JUnit
If we are running tests directly via the JUnit platform, we can use the following command:
java -jar junit-platform-console-standalone.jar \
--class-path target/test-classes \
--select-class ch.sbb.itop.orchestration.bdo.controller.GetAllowedBdoControllerTest
Note: Replace target/test-classes
with the correct path to your compiled test classes.
V – Filtering Tests in CI/CD Pipelines
In Continuous Integration/Continuous Deployment (CI/CD) scenarios, we can configure our pipeline to run only the specific test file:
- Maven: Update the
pom.xml
or pipeline configuration to include the-Dtest
parameter. - Gradle: Use the above Gradle command in your pipeline script.
Conclusion
By using these methods, we can efficiently run tests for the GetAllowedBdoControllerTest.java
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.