Skip to content

Commit

Permalink
JUnit Assertions.assertThrows()
Browse files Browse the repository at this point in the history
  • Loading branch information
ulasalasreenath-sap committed Sep 4, 2023
1 parent a85c120 commit 6960013
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
2 changes: 0 additions & 2 deletions pom.xml
Expand Up @@ -21,15 +21,13 @@
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-runner -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>1.8.2</version>
<scope>test</scope>
</dependency>

<dependency>
Expand Down
@@ -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;
}
}
@@ -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());
}
}

0 comments on commit 6960013

Please sign in to comment.