diff --git a/pom.xml b/pom.xml index becd538..021e0fc 100644 --- a/pom.xml +++ b/pom.xml @@ -21,7 +21,6 @@ org.junit.jupiter junit-jupiter-engine 5.8.2 - test @@ -29,7 +28,6 @@ org.junit.platform junit-platform-runner 1.8.2 - test diff --git a/src/main/java/com/thebackendpro/junit5/exceptionstesting/ArithmeticOperators.java b/src/main/java/com/thebackendpro/junit5/exceptionstesting/ArithmeticOperators.java new file mode 100644 index 0000000..1fceedf --- /dev/null +++ b/src/main/java/com/thebackendpro/junit5/exceptionstesting/ArithmeticOperators.java @@ -0,0 +1,11 @@ +package com.thebackendpro.junit5.exceptionstesting; + +public class ArithmeticOperators { + + public int division(int a, int b) { + if(b == 0) { + throw new ArithmeticException("Cannot divide a number by 0"); + } + return a/b; + } +} diff --git a/src/main/java/com/thebackendpro/junit5/exceptionstesting/ArithmeticOperatorsTest.java b/src/main/java/com/thebackendpro/junit5/exceptionstesting/ArithmeticOperatorsTest.java new file mode 100644 index 0000000..dc6837c --- /dev/null +++ b/src/main/java/com/thebackendpro/junit5/exceptionstesting/ArithmeticOperatorsTest.java @@ -0,0 +1,23 @@ +package com.thebackendpro.junit5.exceptionstesting; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class ArithmeticOperatorsTest { + + @Test + public void divisionTestWhenExceptionIsThrown() { + ArithmeticOperators arithmeticOperators = new ArithmeticOperators(); + ArithmeticException arithmeticException = Assertions.assertThrows(ArithmeticException.class, + () -> arithmeticOperators.division(5, 0), "Failed due to no excpeption is thrown"); + Assertions.assertEquals("Cannot divide a number by 0", arithmeticException.getMessage()); + } + + @Test + public void divisionTestWhenNoExceptionIsThrown() { + ArithmeticOperators arithmeticOperators = new ArithmeticOperators(); + ArithmeticException arithmeticException = Assertions.assertThrows(ArithmeticException.class, + () -> arithmeticOperators.division(5, 1), "Failed due to no excpeption is thrown"); + Assertions.assertEquals("Cannot divide a number by 0", arithmeticException.getMessage()); + } +}