Bael 3082 (#7446)
* added example code for BAEL-3082 * added another test case, refactored method names * moved to basics module
This commit is contained in:
parent
ab9856b31c
commit
8f5bb3c6a9
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue