How to Configure GitHub to Run Unit Tests Automatically
Published: 2024-07-06 05:43:59

# 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](https://github.com/olsido/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](https://github.com/olsido/github-workflow-demo) GitHub repository.

Now we will "put some meat" into the project. Let's add a controller:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qs4h1eim29no7ey34das.png)

...and a unit test:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q0xbbr74o4flvpimfmk2.png)

We will launch the application and see how it works. Right-click `GithubWorkflowDemoApplication` class and choose "Run 'GithubWorkflow....main()':

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/en6z3o4q5i3x1cjulcf6.png)

The application will start very quickly:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c04be8aiblie27xipon3.png)

In a browser, navigate to http://localhost:8080/, and you will see the message "Hello, World!" as we programmed in our application:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f09j53n293j3a55dfh1l.png)

Now let's launch the tests. Right-click `GithubWorkflowDemoApplicationTests` and choose "Run GithubWorkflowDemoApplicationTests:"

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x3xba3f6b3kiul9jsmwu.png)

The tests are successful:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tcjazbof6fsp003g6h49.png)

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:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i1v8s4s46y93hzjqs7gc.png)

...and then click on the "New workflow" button:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/plyghqscavgzw58ljxpk.png)

Search for "test":

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/17sh467l4k98i46gb4t1.png)

The very first option is what we are looking for - we have a Maven project. Click "Configure":

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2q9gaxlksk94juxdjbnr.png)

GitHub will generate a sample workflow for you:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1gn4slzxspnivikqm4z0.png)

After you check it in, it will run it. However, unfortunately, the default workflow fails:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hx8wkuq5ydthq7id623q.png)

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:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5lsursotzdcn7b925u3a.png)

I also added the surefire Maven plugin to `pom.xml`:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hkoo1cblqwo58kj1nk88.png)

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:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v6xoazj8tgw8uipzboi2.png)

GitHub template has these lines at the end, that are indicated as "optional", and they are giving errors:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ks5ylqg9zy95mbteciy.png)

To fix the dependency graph permissions, let's create a personal access token with all the needed permissions. First, go to your profile > Settings:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iyqwythstx4kydnk6akz.png)

Then navigate to "Developer Settings" at the bottom:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/awtbna6p6lrmq4eq5co7.png)

Create a personal access token. Make sure you check all the needed permissions:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ie0ev4p6lth1gzdar4c.png)

Copy your token:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lmynmctd95mfquukdzd8.png)

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:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d855x0r34yab0766aapt.png)

Add your token to the workflow configuration:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/htrk56bgflacatxbezp3.png)

Now the workflow succeeds!

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bp7h0lm2lnjx3tfmhcb0.png)

# 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:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y7y3bmkpss2io0jhxelo.png)

Now when I launch them from the IDE, one of them fails:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a2ilcrktfvl842yihvbq.png)

After committing the changes, I found that the workflow also failed:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/21p56ne5t8tq2oer9ysx.png)

You can see the details, e.g., which tests failed and why, in the log:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p3kgxjc4zf1tdbwpux5i.png)

I also see the failure in my email that is associated with my GitHub account:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qy17pw5u1zfwpqxnd6rk.png)

Once I fixed the tests, the workflow was successful again:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hvg0y1fbrn63wnm9ypf4.png)

# 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.