Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

002 - feat: unit tests #1

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
39 changes: 39 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# This workflow will build a Java project with Maven
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven

name: 'CI'

# Controls when the action will run.
on:
workflow_call:
workflow_dispatch:

# Triggers the workflow on push or pull request events but only for the master branch
push:
branches:
- main
pull_request:

jobs:
continuous-testing:
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v4
# Runs a single command using the runners shell
- name: Set up JDK 1.17
uses: actions/setup-java@v4
with:
distribution: "liberica"
java-version: "17"
cache: "maven"
- name: Compile
run: mvn compile
- name: Test
run: mvn verify
- uses: dorny/test-reporter@v1
with:
name: Test Results
path: "target/**/TEST*.xml"
reporter: java-junit
18 changes: 18 additions & 0 deletions .github/workflows/test-report.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: 'Test Report'
on:
workflow_call:
workflow_dispatch:
workflow_run:
workflows: ['CI'] # runs after CI workflow
types:
- completed
jobs:
report:
runs-on: ubuntu-latest
steps:
- uses: dorny/test-reporter@v1
with:
artifact: test-results # artifact name
name: Test Results # Name of the check run which will be created
path: '*.xml' # Path to test results (inside artifact .zip)
reporter: java-junit # Format of test results
7 changes: 7 additions & 0 deletions .gitpod.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM gitpod/workspace-full

USER gitpod

RUN bash -c ". /home/gitpod/.sdkman/bin/sdkman-init.sh && \
sdk install java 17.0.9-tem && \
sdk default java 17.0.9-tem"
24 changes: 24 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# This configuration file was automatically generated by Gitpod.
# Please adjust to your needs (see https://www.gitpod.io/docs/config-gitpod-file)
# and commit this file to your remote git repository to share the goodness with others.

image:
file: .gitpod.Dockerfile

tasks:
- init: mvn package -DskipTests=false

vscode:
extensions:
- redhat.java
- vscjava.vscode-java-debug
- vscjava.vscode-maven

# Ports to expose on workspace startup
ports:
- port: 8080
onOpen: open-preview
name: Calculator API
description: Calculator API init
visibility: private
protocol: http
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Create calculator App
* Substract: substract two numbers and return result
* Multiply: multiply two numbers and return result
* Divide: divide two numbers and return result
* divide by zero should return error message

## System requirements

Expand Down Expand Up @@ -35,7 +36,23 @@ $ ./mvnw clean test

## 2. Automatically Build and Test

**TODO**
[Github action ci](.github/workflows/ci.yml) step definition:
```yaml
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
# Runs a single command using the runners shell
- name: Set up JDK 1.11
uses: actions/[email protected]
with:
java-version: '11'
distribution: 'zulu'
cache: 'maven'
- name: Compile
run: mvn compile
- name: Test
run: mvn verify
```

## 3. Containerize Your Web App

Expand Down
22 changes: 1 addition & 21 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.geekshubs.javawebapp</groupId>
<artifactId>java-maven-calculator-web-app</artifactId>
<packaging>war</packaging>
<packaging>jar</packaging>
<version>1.1-SNAPSHOT</version>
<name>Calculator Web</name>
<description>A Java Maven Calculator Web Application</description>
Expand All @@ -17,7 +17,6 @@
<maven-compiler-plugin.version>3.9.0</maven-compiler-plugin.version>
<maven.resources.plugin.version>3.2.0</maven.resources.plugin.version>
<maven.surefire.plugin.version>2.22.2</maven.surefire.plugin.version>
<jacoco.maven.plugin.version>0.8.7</jacoco.maven.plugin.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -71,25 +70,6 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.maven.plugin.version}</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
21 changes: 21 additions & 0 deletions src/main/java/com/geekshubs/calculator/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,25 @@

public class Calculator {

int _x;
int _y;
int _result;

public Calculator(int x, int y, int result) {
_x = x;
_y = y;
_result = result;
}

public int getX() {
return _x;
}

public int getY() {
return _y;
}

public int getResult() {
return _result;
}
}
29 changes: 29 additions & 0 deletions src/test/java/com/geekshubs/calculator/CalculatorTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
package com.geekshubs.calculator;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class CalculatorTest {

@Test
public void testSum() {
assertEquals(34, new Calculator(8, 26, 8 + 26).getResult());
}

@Test
public void testSub() {
assertEquals(4, new Calculator(12, 8, 12 - 8).getResult());
}

@Test
public void testMul() {
assertEquals(88, new Calculator(11, 8, 11 * 8).getResult());
}

@Test
public void testDiv() {
assertEquals(1, new Calculator(12, 12, 12 / 12).getResult());
}

@Test
public void testDivByZero() {
assertThrows(ArithmeticException.class, () -> new Calculator(12, 0, 12 / 0).getResult());
}
}