Skip to content

Commit 91482d8

Browse files
committed
test: fix swallowed exceptions
1 parent 715f897 commit 91482d8

1 file changed

Lines changed: 9 additions & 27 deletions

File tree

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package com.cbfacademy.operators;
22

3-
import org.junit.jupiter.api.DisplayName;
4-
import org.junit.jupiter.params.ParameterizedTest;
5-
import org.junit.jupiter.params.provider.CsvSource;
6-
73
import static org.hamcrest.CoreMatchers.is;
84
import static org.hamcrest.MatcherAssert.assertThat;
95
import static org.hamcrest.Matchers.closeTo;
106

11-
import java.util.concurrent.Callable;
7+
import org.junit.jupiter.api.DisplayName;
8+
import org.junit.jupiter.params.ParameterizedTest;
9+
import org.junit.jupiter.params.provider.CsvSource;
1210

1311
public class AppTest {
1412

@@ -18,57 +16,41 @@ public class AppTest {
1816
@ParameterizedTest(name = "{0} + {1} should be {2}")
1917
@CsvSource({ "20.3, 3.0, 23.3", "0, 0, 0", "-10.0, -5.0, -15.0", "10.5, 5.2, 15.7", "-5.0, 2.0, -3.0", "-26.5, 26.5, 0", "0.357, 0, 0.357" })
2018
public void add_ReturnsCorrectResult(double operand1, double operand2, double expected) {
21-
assertDoubleResult(() -> App.add(operand1, operand2), expected);
19+
assertThat(App.add(operand1, operand2), is(closeTo(expected, delta)));
2220
}
2321

2422
@DisplayName("subtract() method should return correct value: ")
2523
@ParameterizedTest(name = "{0} - {1} should be {2}")
2624
@CsvSource({ "20.3, 3.0, 17.3", "0, 0, 0", "-10.0, -5.0, -5.0", "10.5, 5.2, 5.3", "-5.0, 2.0, -7.0", "-5.5, -3.1, -2.4", "-3.1, -5.5, 2.4" })
2725
public void subtract_ReturnsCorrectResultWithDoubleInputs(double operand1, double operand2, double expected) {
28-
assertDoubleResult(() -> App.subtract(operand1, operand2), expected);
26+
assertThat(App.subtract(operand1, operand2), is(closeTo(expected, delta)));
2927
}
3028

3129
@DisplayName("multiply() method should return correct value: ")
3230
@ParameterizedTest(name = "{0} * {1} should be {2}")
3331
@CsvSource({ "20.3, 3.0, 60.9", "0, 0, 0", "-10.0, -5.0, 50.0", "10.5, 5.2, 54.6", "-5.0, 2.0, -10.0" })
3432
public void multiply_ReturnsCorrectResult(double operand1, double operand2, double expected) {
35-
assertDoubleResult(() -> App.multiply(operand1, operand2), expected);
33+
assertThat(App.multiply(operand1, operand2), is(closeTo(expected, delta)));
3634
}
3735

3836
@DisplayName("areEqual() method should return correct value: ")
3937
@ParameterizedTest(name = "{0} == {1} should be {2}")
4038
@CsvSource({ "20.3, 20.3, true", "0, 0, true", "-10.0, -5.0, false", "10.5, 10.4, false", "0, 0.01, false" })
4139
public void areEqual_ReturnsCorrectResult(double operand1, double operand2, Boolean expected) {
42-
assertBooleanResult(() -> App.areEqual(operand1, operand2), expected);
40+
assertThat(App.areEqual(operand1, operand2), is(expected));
4341
}
4442

4543
@DisplayName("isLessThan() method should return correct value: ")
4644
@ParameterizedTest(name = "{0} < {1} should be {2}")
4745
@CsvSource({ "20.3, 20.3, false", "0, 0, false", "-10.0, -5.0, true", "10.5, 10.4, false", "0, 0.01, true", "-1, 1, true" })
4846
public void isLessThan_ReturnsCorrectResult(double operand1, double operand2, Boolean expected) {
49-
assertBooleanResult(() -> App.isLessThan(operand1, operand2), expected);
47+
assertThat(App.isLessThan(operand1, operand2), is(expected));
5048
}
5149

5250
@DisplayName("isMoreThan() method should return correct value: ")
5351
@ParameterizedTest(name = "{0} > {1} should be {2}")
5452
@CsvSource({ "20.3, 20.3, false", "0, 0, false", "-10.0, -5.0, false", "10.5, 10.4, true", "0, 0.01, false", "1, -1, true" })
5553
public void isMoreThan_ReturnsCorrectResult(double operand1, double operand2, Boolean expected) {
56-
assertBooleanResult(() -> App.isMoreThan(operand1, operand2), expected);
57-
}
58-
59-
private void assertBooleanResult(Callable<Boolean> fn, Boolean expected) {
60-
try {
61-
assertThat(fn.call(), is(expected));
62-
} catch (Exception e) {
63-
e.printStackTrace();
64-
}
65-
}
66-
67-
private void assertDoubleResult(Callable<Double> fn, double expected) {
68-
try {
69-
assertThat(fn.call(), is(closeTo(expected, delta)));
70-
} catch (Exception e) {
71-
e.printStackTrace();
72-
}
54+
assertThat(App.isMoreThan(operand1, operand2), is(expected));
7355
}
7456
}

0 commit comments

Comments
 (0)