diff --git a/core-java/src/main/java/com/baeldung/stackoverflowerror/RecursionWithCorrectTerminationCondition.java b/core-java/src/main/java/com/baeldung/stackoverflowerror/RecursionWithCorrectTerminationCondition.java index 0c0c392532..16d771f389 100644 --- a/core-java/src/main/java/com/baeldung/stackoverflowerror/RecursionWithCorrectTerminationCondition.java +++ b/core-java/src/main/java/com/baeldung/stackoverflowerror/RecursionWithCorrectTerminationCondition.java @@ -1,7 +1,7 @@ package com.baeldung.stackoverflowerror; public class RecursionWithCorrectTerminationCondition { - public static int calculateFactorial(final int number) { + public int calculateFactorial(final int number) { return number <= 1 ? 1 : number * calculateFactorial(number - 1); } } diff --git a/core-java/src/test/java/com/baeldung/stackoverflowerror/InfiniteRecursionWithTerminationConditionManualTest.java b/core-java/src/test/java/com/baeldung/stackoverflowerror/InfiniteRecursionWithTerminationConditionManualTest.java index 25ef504206..996d0e9017 100644 --- a/core-java/src/test/java/com/baeldung/stackoverflowerror/InfiniteRecursionWithTerminationConditionManualTest.java +++ b/core-java/src/test/java/com/baeldung/stackoverflowerror/InfiniteRecursionWithTerminationConditionManualTest.java @@ -6,7 +6,7 @@ import org.junit.Test; public class InfiniteRecursionWithTerminationConditionManualTest { @Test - public void givenPositiveIntNoOne_whenCalcFact_thenThrowsException() { + public void givenPositiveIntNoOne_whenCalcFact_thenCorrectlyCalc() { int numToCalcFactorial = 1; InfiniteRecursionWithTerminationCondition irtc = new InfiniteRecursionWithTerminationCondition(); @@ -14,7 +14,7 @@ public class InfiniteRecursionWithTerminationConditionManualTest { } @Test - public void givenPositiveIntGtOne_whenCalcFact_thenThrowsException() { + public void givenPositiveIntGtOne_whenCalcFact_thenCorrectlyCalc() { int numToCalcFactorial = 5; InfiniteRecursionWithTerminationCondition irtc = new InfiniteRecursionWithTerminationCondition(); diff --git a/core-java/src/test/java/com/baeldung/stackoverflowerror/RecursionWithCorrectTerminationConditionManualTest.java b/core-java/src/test/java/com/baeldung/stackoverflowerror/RecursionWithCorrectTerminationConditionManualTest.java new file mode 100644 index 0000000000..a4269aef56 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/stackoverflowerror/RecursionWithCorrectTerminationConditionManualTest.java @@ -0,0 +1,15 @@ +package com.baeldung.stackoverflowerror; + +import static junit.framework.TestCase.assertEquals; + +import org.junit.Test; + +public class RecursionWithCorrectTerminationConditionManualTest { + @Test + public void givenNegativeInt_whenCalcFact_thenCorrectlyCalc() { + int numToCalcFactorial = -1; + RecursionWithCorrectTerminationCondition rctc = new RecursionWithCorrectTerminationCondition(); + + assertEquals(1, rctc.calculateFactorial(numToCalcFactorial)); + } +}