From d7ce2a3ea234de3a040e762083db88aa3610f320 Mon Sep 17 00:00:00 2001 From: Kamal Wickramanayake Date: Sat, 27 Jun 2026 17:10:54 +0530 Subject: Added two JUnit sample apps --- junit/02-maths/.gitignore | 2 + junit/02-maths/.mvn/jvm.config | 0 junit/02-maths/.mvn/maven.config | 0 junit/02-maths/pom.xml | 90 ++++++++++++++++++++++ .../src/main/java/com/example/maths/Maths.java | 16 ++++ .../src/test/java/com/example/maths/MathsTest.java | 32 ++++++++ 6 files changed, 140 insertions(+) create mode 100644 junit/02-maths/.gitignore create mode 100644 junit/02-maths/.mvn/jvm.config create mode 100644 junit/02-maths/.mvn/maven.config create mode 100644 junit/02-maths/pom.xml create mode 100644 junit/02-maths/src/main/java/com/example/maths/Maths.java create mode 100644 junit/02-maths/src/test/java/com/example/maths/MathsTest.java (limited to 'junit/02-maths') diff --git a/junit/02-maths/.gitignore b/junit/02-maths/.gitignore new file mode 100644 index 0000000..4e3ee4c --- /dev/null +++ b/junit/02-maths/.gitignore @@ -0,0 +1,2 @@ +.vscode +target diff --git a/junit/02-maths/.mvn/jvm.config b/junit/02-maths/.mvn/jvm.config new file mode 100644 index 0000000..e69de29 diff --git a/junit/02-maths/.mvn/maven.config b/junit/02-maths/.mvn/maven.config new file mode 100644 index 0000000..e69de29 diff --git a/junit/02-maths/pom.xml b/junit/02-maths/pom.xml new file mode 100644 index 0000000..bba20e1 --- /dev/null +++ b/junit/02-maths/pom.xml @@ -0,0 +1,90 @@ + + + 4.0.0 + + com.example.maths + demo + 1.0-SNAPSHOT + + demo + + http://www.example.com + + + UTF-8 + 25 + + + + + + org.junit + junit-bom + 5.11.0 + pom + import + + + + + + + org.junit.jupiter + junit-jupiter-api + test + + + + org.junit.jupiter + junit-jupiter-params + test + + + + + + + + + maven-clean-plugin + 3.4.0 + + + + maven-resources-plugin + 3.3.1 + + + maven-compiler-plugin + 3.13.0 + + + maven-surefire-plugin + 3.3.0 + + + maven-jar-plugin + 3.4.2 + + + maven-install-plugin + 3.1.2 + + + maven-deploy-plugin + 3.1.2 + + + + maven-site-plugin + 3.12.1 + + + maven-project-info-reports-plugin + 3.6.1 + + + + + diff --git a/junit/02-maths/src/main/java/com/example/maths/Maths.java b/junit/02-maths/src/main/java/com/example/maths/Maths.java new file mode 100644 index 0000000..aac3ac0 --- /dev/null +++ b/junit/02-maths/src/main/java/com/example/maths/Maths.java @@ -0,0 +1,16 @@ +package com.example.maths; + +public class Maths { + + public int factorial(int number) { + if (number == 0) { + return 1; + } + + return number * factorial(number - 1); + } + + public int divide(int a, int b) { + return a / b; + } +} diff --git a/junit/02-maths/src/test/java/com/example/maths/MathsTest.java b/junit/02-maths/src/test/java/com/example/maths/MathsTest.java new file mode 100644 index 0000000..4396901 --- /dev/null +++ b/junit/02-maths/src/test/java/com/example/maths/MathsTest.java @@ -0,0 +1,32 @@ +package com.example.maths; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class MathsTest { + private Maths maths; + + @BeforeEach + void setUp() { + maths = new Maths(); + } + + @Test + void testFactorial() { + assertEquals(1, maths.factorial(0), "Factorial of 0"); + assertEquals(1, maths.factorial(1), "Factorial of 1"); + assertEquals(120, maths.factorial(5), "Factorial of 5"); + } + + @Test + void testDivideByZeroThrowsException() { + ArithmeticException exception = assertThrows(ArithmeticException.class, () -> { + maths.divide(10, 0); + }); + + assertEquals("/ by zero", exception.getMessage()); + } +} \ No newline at end of file -- cgit v1.2.3