<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ewelina Fiedorowicz</title>
    <description>The latest articles on DEV Community by Ewelina Fiedorowicz (@ewefie).</description>
    <link>https://dev.to/ewefie</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F362430%2F7054516a-5807-433c-8a3e-d6815d67ea7b.jpeg</url>
      <title>DEV Community: Ewelina Fiedorowicz</title>
      <link>https://dev.to/ewefie</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ewefie"/>
    <language>en</language>
    <item>
      <title>Getting started with Github Actions: Run JUnit 5 tests in a Java project with Maven</title>
      <dc:creator>Ewelina Fiedorowicz</dc:creator>
      <pubDate>Wed, 17 Jun 2020 15:15:00 +0000</pubDate>
      <link>https://dev.to/ewefie/getting-started-with-github-actions-run-junit-5-tests-in-a-java-project-with-maven-20g4</link>
      <guid>https://dev.to/ewefie/getting-started-with-github-actions-run-junit-5-tests-in-a-java-project-with-maven-20g4</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;This blog post was originally published at &lt;a href="https://blog.codeleak.pl/2020/06/gh-actions-maven-junit5.html"&gt;blog.codeleak.pl&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Github Actions is a CI/CD service provided by Github and it is free for public repositories. For private repositories, each GitHub account receives a certain amount of free minutes and storage, depending on the product used with the account. &lt;/p&gt;

&lt;p&gt;In this blog post, you will learn how to create a simple workflow for running JUnit 5 tests in a Maven based Java project and how to add a build status badge to a README.md file.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Table of Contents&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Problem&lt;/li&gt;
&lt;li&gt;Solution&lt;/li&gt;
&lt;li&gt;
Introducing Workflow Syntax

&lt;ul&gt;
&lt;li&gt;Step 1: Checkout the repository&lt;/li&gt;
&lt;li&gt;Step 2: Set up JDK 14&lt;/li&gt;
&lt;li&gt;Step 3: Cache Maven packages&lt;/li&gt;
&lt;li&gt;Step 4: Run tests with Maven&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Badges&lt;/li&gt;
&lt;li&gt;Summary&lt;/li&gt;
&lt;li&gt;Source code&lt;/li&gt;
&lt;li&gt;References&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Problem
&lt;/h1&gt;

&lt;p&gt;On every &lt;code&gt;push&lt;/code&gt; event, run JUnit 5 tests in a Java project with Maven and cache downloaded dependencies to speed up subsequent runs.&lt;/p&gt;

&lt;h1&gt;
  
  
  Solution
&lt;/h1&gt;

&lt;p&gt;TL;DR&lt;/p&gt;

&lt;p&gt;The complete workflow, consiting of a single job with four steps, for automating the process of running JUnit tests in a Java project with Maven.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;tests&lt;/span&gt;
&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;push&lt;/span&gt;
&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;run_tests&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Checkout the repository&lt;/span&gt;
        &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v2&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Set up JDK &lt;/span&gt;&lt;span class="m"&gt;14&lt;/span&gt;
        &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/setup-java@v1&lt;/span&gt;
        &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;java-version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;14&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Cache Maven packages&lt;/span&gt;
        &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/cache@v2&lt;/span&gt;
        &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;~/.m2&lt;/span&gt;
          &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}&lt;/span&gt;
          &lt;span class="na"&gt;restore-keys&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ runner.os }}-m2&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Run tests with Maven&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;mvn -B test --file pom.xml&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  Introducing Workflow Syntax
&lt;/h1&gt;

&lt;p&gt;Workflow files use &lt;em&gt;YAML&lt;/em&gt; syntax and should have either &lt;code&gt;.yml&lt;/code&gt; or &lt;code&gt;.yaml&lt;/code&gt; extension. All workflow files have to be placed in &lt;code&gt;.github/workflows&lt;/code&gt; directory in the root of your project.&lt;/p&gt;

&lt;p&gt;You can create your fully customized workflow or you can use actions provided by the Github community. You can find them in &lt;a href="https://github.com/marketplace?type=actions"&gt;Github Marketplace&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Workflows consist of the following sections:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;name&lt;/code&gt; (optional): a workflow name that will be displayed on the actions page of the repository.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;on&lt;/code&gt; (required): an event that triggers the workflow (&lt;code&gt;push&lt;/code&gt;, &lt;code&gt;pull request&lt;/code&gt;, &lt;code&gt;page build&lt;/code&gt;, etc.). You can specify one or more events and also you can define branch(es) or tag(s) to run on. The workflow can be also scheduled using cron syntax.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;env&lt;/code&gt; (optional): environment variables. They can be set for the whole workflow, single job, or a step. It is recommended to use &lt;em&gt;Github Secrets&lt;/em&gt; to pass tokens or secrets to the workfow. Secrets are encrypted environment variables exposed only to selected actions.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Learn more about storing secrets in &lt;a href="https://help.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets"&gt;Github Actions Documentation&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;jobs&lt;/code&gt; (at least one is required): it is an actual list of tasks to be executed on a runner. Each job must have a unique identifier and may include several properties e.g.:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;runs-on&lt;/code&gt; (required): you can choose a type of machine to run the job on (Windows, Linux, or macOS) or your own runner.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;steps&lt;/code&gt;: a sequence of tasks. Every step can be either a simple shell command or an action consisting of various steps. An action can be defined in your own repository, any other public repository, or a Docker registry. Some actions require inputs that can be passed using &lt;code&gt;with&lt;/code&gt; keyword.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;The workflow syntax can be found in &lt;a href="https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions"&gt;Github Actions documentation&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Step 1: Checkout the repository
&lt;/h2&gt;

&lt;p&gt;The first step is to checkout the repository. In order to do so, use the &lt;code&gt;checkout&lt;/code&gt; action:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Checkhout the repository&lt;/span&gt;
  &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 2: Set up JDK 14
&lt;/h2&gt;

&lt;p&gt;To configure Java environment use &lt;code&gt;setup-java&lt;/code&gt; action. You can choose from various Java versions including major, minor and early access:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Set up JDK &lt;/span&gt;&lt;span class="m"&gt;14&lt;/span&gt;
  &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/setup-java@v1&lt;/span&gt;
  &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;java-version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;14&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3: Cache Maven packages
&lt;/h2&gt;

&lt;p&gt;To cache downloaded dependencies and build outputs use &lt;code&gt;cache&lt;/code&gt; action provided by the Github community. Path parameter defines the file path on the runner to cache or restore. Key parameter is used to search for a cache. If there is no match a new cache entry will be created and saved with the provided key in the path directory. Restore-keys parameter is optional and contains a list of alternative keys.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Cache Maven packages&lt;/span&gt;
  &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/cache@v2&lt;/span&gt;
  &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;~/.m2&lt;/span&gt;
    &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}&lt;/span&gt;
    &lt;span class="na"&gt;restore-keys&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ runner.os }}-m2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 4: Run tests with Maven
&lt;/h2&gt;

&lt;p&gt;Finally, to run tests use &lt;code&gt;mvn&lt;/code&gt; command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Test with Maven&lt;/span&gt;
  &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;mvn -B test --file pom.xml&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  Badges
&lt;/h1&gt;

&lt;p&gt;Adding a badge to the README.md file is a nice way to show to repository visitors that your project builds properly and all tests pass. &lt;/p&gt;

&lt;p&gt;To obtain a badge for a workflow simply replace placeholders in the following URL with relevant information. Note that  is a name defined in the first line of .yml file and it not necessarily has to be the file name:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://github.com/&amp;lt;OWNER&amp;gt;/&amp;lt;REPOSITORY&amp;gt;/workflows/&amp;lt;WORKFLOW_NAME&amp;gt;/badge.svg
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To add a badge to the project wrap the link to the badge in the following manner and paste it at the top of the README.md file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;![](https://github.com/kolorobot/spring-boot-junit5/workflows/tests/badge.svg)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The badge will appear after committing and pushing changes to the repository.&lt;/p&gt;

&lt;h1&gt;
  
  
  Summary
&lt;/h1&gt;

&lt;p&gt;After the workflow successfully triggers, you will see logs and results in the actions page in your Github repository. Github Actions uses the exit codes to define whether an action is passing or failing. Any non-zero exit code is considered as a failure.&lt;/p&gt;

&lt;h1&gt;
  
  
  Source code
&lt;/h1&gt;

&lt;p&gt;The source code for this article can be found on &lt;a href="https://github.com/kolorobot/spring-boot-junit5"&gt;Github&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  References
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://blog.codeleak.pl/2019/09/spring-boot-testing-with-junit-5.html"&gt;Spring Boot testing with JUnit 5&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://help.github.com/en/actions"&gt;Github Actions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://help.github.com/en/actions/configuring-and-managing-workflows/configuring-a-workflow#adding-a-workflow-status-badge-to-your-repository"&gt;Github Badges&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://help.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets"&gt;Github Secrets&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>java</category>
      <category>github</category>
      <category>testing</category>
    </item>
  </channel>
</rss>
