Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a85c120
commit 6960013
Showing
3 changed files
with
34 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/com/thebackendpro/junit5/exceptionstesting/ArithmeticOperators.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/thebackendpro/junit5/exceptionstesting/ArithmeticOperatorsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |