* added example code for BAEL-3082

* added another test case, refactored method names

* moved to basics module
This commit is contained in:
Paul Latzelsperger 2019-07-29 22:38:42 +02:00 committed by maibin
parent ab9856b31c
commit 8f5bb3c6a9
2 changed files with 54 additions and 0 deletions

View File

@ -0,0 +1,15 @@
package com.baeldung.failure_vs_error;
/**
* @author paullatzelsperger
* @since 2019-07-17
*/
public class SimpleCalculator {
public static double divideNumbers(double dividend, double divisor) {
if (divisor == 0) {
throw new ArithmeticException("Division by zero!");
}
return dividend / divisor;
}
}

View File

@ -0,0 +1,39 @@
package com.baeldung.failure_vs_error;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* @author paullatzelsperger
* @since 2019-07-17
*/
class SimpleCalculatorUnitTest {
@Test
void whenDivideByValidNumber_thenAssertCorrectResult() {
double result = SimpleCalculator.divideNumbers(6, 3);
assertEquals(2, result);
}
@Test
@Disabled("test is expected to fail, disabled so that CI build still goes through")
void whenDivideNumbers_thenExpectWrongResult() {
double result = SimpleCalculator.divideNumbers(6, 3);
assertEquals(15, result);
}
@Test
@Disabled("test is expected to raise an error, disabled so that CI build still goes through")
void whenDivideByZero_thenThrowsException() {
SimpleCalculator.divideNumbers(10, 0);
}
@Test
void whenDivideByZero_thenAssertException(){
assertThrows(ArithmeticException.class, () -> SimpleCalculator.divideNumbers(10, 0));
}
}