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

003 - feat: Add integration components tests #2

Open
wants to merge 5 commits into
base: feat-unit-tests
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test-report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on:
workflow_call:
workflow_dispatch:
workflow_run:
workflows: ['CI'] # runs after CI workflow
workflows: [ 'CI' ] # runs after CI workflow
types:
- completed
jobs:
Expand Down
22 changes: 18 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,42 +1,56 @@
# A Java Maven Calculator Web App

A Java calculator web app, build by Maven, CI/CD

# Requirements

Create calculator App

* Sum: sum two numbers and return result
* Subtract: subtract 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

Create a service to wrap those methods

* Sum: sum two numbers and return result
* Substract: substract two numbers and return result
* Subtract: subtract 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

JDK-11
Maven 3.8.4
* JDK-11
* Maven 3.8.4

## 1. Manualy Build, Test, and Deploy By Maven
## 1. Manually Build, Test, and Deploy By Maven

### 1.2 Run JUnit Test

Maven execution:

```console
$ mvn clean test
```

Maven wrapper:

```console
$ mvn wrapper:wrapper -Dmaven=3.8.4
```

Execute (linux/macos mvnw or windows mvnw.cmd):

```console
$ ./mvnw clean test
```

## 2. Automatically Build and Test

[Github action ci](.github/workflows/ci.yml) step definition:

```yaml
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
Expand Down
5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.geekshubs.javawebapp</groupId>
<artifactId>java-maven-calculator-web-app</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.geekshubs.calculator.service;

import com.geekshubs.calculator.Calculator;

public class CalculatorService {

public Calculator Add(int x, int y) {
return new Calculator(x, y, x + y);
}

public Calculator Sub(int x, int y) {
return new Calculator(x, y, x - y);
}

public Calculator Mul(int x, int y) {
return new Calculator(x, y, x * y);
}

public Calculator Div(int x, int y) {
return new Calculator(x, y, x / y);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.geekshubs.calculator.service;

import org.junit.jupiter.api.Test;

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

public class CalculatorServiceTest {

CalculatorService calculatorService = new CalculatorService();

@Test
public void testAdd() {
assertEquals(34, calculatorService.Add(8, 26).getResult());
}

@Test
public void testSub() {
assertEquals(4, calculatorService.Sub(12, 8).getResult());
}

@Test
public void testMul() {
assertEquals(88, calculatorService.Mul(11, 8).getResult());
}

@Test
public void testDiv() {
assertEquals(1, calculatorService.Div(12, 12).getResult());
}

@Test
public void testDivByZero() {
assertThrows(ArithmeticException.class, () -> calculatorService.Div(10, 0).getResult());
}
}