This content originally appeared on DEV Community and was authored by Olga Strijewski
Introduction
Setting up automated unit tests in GitHub can streamline your development process, ensuring code quality and reliability. This guide demonstrates how to create a simple Java project with unit tests and configure GitHub Actions to run these tests automatically.
You can check out the GitHub repository used for this project here: github-workflow-demo.
Creating a Project with Unit Tests
We will now create a simple Java project with a unit test. This will serve as the foundation for setting up automated unit testing in GitHub.
Generate Code Using Spring Initializr:
- Open IntelliJ IDEA.
- Go to File > New > Project.
- Select Spring Boot and configure your project (Group, Artifact, etc.).
- Choose "Spring Web" as a dependency.
- Click Create, and IntelliJ IDEA will generate the project code for you.
This project can be found at github-workflow-demo GitHub repository.
Now we will "put some meat" into the project. Let's add a controller:
...and a unit test:
We will launch the application and see how it works. Right-click GithubWorkflowDemoApplication
class and choose "Run 'GithubWorkflow....main()':
The application will start very quickly:
In a browser, navigate to http://localhost:8080/, and you will see the message "Hello, World!" as we programmed in our application:
Now let's launch the tests. Right-click GithubWorkflowDemoApplicationTests
and choose "Run GithubWorkflowDemoApplicationTests:"
The tests are successful:
Great! You now have a simple application with a unit test. Don't forget to commit and push your new code to the repository.
Creating a Unit Test Workflow in GitHub
We will start with GitHub out-of-the-box template workflow for unit tests. Go to the "Actions" tab:
...and then click on the "New workflow" button:
Search for "test":
The very first option is what we are looking for - we have a Maven project. Click "Configure":
GitHub will generate a sample workflow for you:
After you check it in, it will run it. However, unfortunately, the default workflow fails:
You will need to add to it to make it succeed.
What's Wrong with the Out-of-the-Box Workflow?
Amazingly, the out-of-the-box workflow doesn't do any testing! I had to modify it as follows to make it run the tests:
I also added the surefire Maven plugin to pom.xml
:
Now it actually runs the tests, however we still have errors.
Dependency Graph Errors
The errors are security related, and are about the dependency graph:
GitHub template has these lines at the end, that are indicated as "optional", and they are giving errors:
To fix the dependency graph permissions, let's create a personal access token with all the needed permissions. First, go to your profile > Settings:
Then navigate to "Developer Settings" at the bottom:
Create a personal access token. Make sure you check all the needed permissions:
Copy your token:
Then navigate to your repository's Settings > Secrets and variables > Actions, and create a secret with the name GH_TOKEN and the value - the token that you just created:
Add your token to the workflow configuration:
Now the workflow succeeds!
Testing the Workflow
Our workflow seems to be working well. Let's see what happens if our tests fail.
I made the test fail as follows:
Now when I launch them from the IDE, one of them fails:
After committing the changes, I found that the workflow also failed:
You can see the details, e.g., which tests failed and why, in the log:
I also see the failure in my email that is associated with my GitHub account:
Once I fixed the tests, the workflow was successful again:
Conclusion
By following this guide, you have successfully set up a simple Java project with unit tests and configured GitHub Actions to automate the testing process. This ensures continuous integration and helps maintain code quality as your project evolves.
This content originally appeared on DEV Community and was authored by Olga Strijewski
Olga Strijewski | Sciencx (2024-07-06T05:43:59+00:00) How to Configure GitHub to Run Unit Tests Automatically. Retrieved from https://www.scien.cx/2024/07/06/how-to-configure-github-to-run-unit-tests-automatically/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.