mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-28 06:55:02 +08:00
Refactor factorial, add unit tests (#4266)
This commit is contained in:

committed by
GitHub

parent
cc9afea036
commit
f83008d80a
@ -5,9 +5,19 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class FactorialTest {
|
||||
private static final String EXCEPTION_MESSAGE = "Input number cannot be negative";
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
public void testWhenInvalidInoutProvidedShouldThrowException() {
|
||||
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> Factorial.factorial(-1));
|
||||
assertEquals(exception.getMessage(), EXCEPTION_MESSAGE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCorrectFactorialCalculation() {
|
||||
assertEquals(1, Factorial.factorial(0));
|
||||
assertEquals(1, Factorial.factorial(1));
|
||||
assertEquals(120, Factorial.factorial(5));
|
||||
assertEquals(3628800, Factorial.factorial(10));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user