Refactor factorial, add unit tests (#4266)

This commit is contained in:
Albina Gimaletdinova
2023-07-28 22:02:04 +03:00
committed by GitHub
parent cc9afea036
commit f83008d80a
2 changed files with 15 additions and 13 deletions

View File

@ -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));
}
}