Skip to content

Commit 9de19b6

Browse files
committed
Add finally clause test + refactoring
1 parent b4389ef commit 9de19b6

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

src/test/java/pl/mperor/interview/tasks/QuizQuestionsTest.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ public void compareStrings() {
9191
public void implementComparable() {
9292
//When implementing Comparable interface, a.compareTo(b) should return? Select one:
9393
//a. Any positive number when a < b, exactly 0 when a = b, any negative number when a ≥ b
94-
//b. Exactly: -1 when a < b, 0 when a = b, 1 when a>b*
95-
//c. Exactly: 1 when a < b, 0 when a = b, -1 when a =b
94+
//b. Exactly: -1 when a < b, 0 when a = b, 1 when a>b
95+
//c. Exactly: 1 when a < b, 0 when a = b, -1 when a>b
9696
//d. Any negative number when a < b, exactly 0 when a = b, any positive number when a ≥ b
9797

9898
Comparator<Integer> comparator = QuizQuestionsTest::compare;
@@ -270,7 +270,32 @@ public void testDoubleValuePrecision() {
270270

271271
// BigDecimal offers more precision with control over rounding, but double is faster in terms of performance.
272272
assertEquals(new BigDecimal("0.01"), new BigDecimal("0.03").subtract(new BigDecimal("0.02")));
273-
assertNotEquals(0.01, 0.3, 0.2);
273+
assertNotEquals(0.01, 0.03 - 0.02);
274274
}
275275

276+
@Test
277+
public void testFinallyClauseHandling() {
278+
// Which block will return the value: try, catch, or finally? And what is the result?"
279+
280+
assertFalse(finallyVsTry());
281+
assertFalse(finallyVsTryCatch());
282+
}
283+
284+
public boolean finallyVsTryCatch() {
285+
try {
286+
throw new IllegalStateException();
287+
} catch (Exception exception) {
288+
return true;
289+
} finally {
290+
return false;
291+
}
292+
}
293+
294+
public boolean finallyVsTry() {
295+
try {
296+
return true;
297+
} finally {
298+
return false;
299+
}
300+
}
276301
}

0 commit comments

Comments
 (0)