diff --git a/proJect/pom.xml b/proJect/pom.xml index 436410e..dadefc0 100644 --- a/proJect/pom.xml +++ b/proJect/pom.xml @@ -12,5 +12,26 @@ 11 - + + + org.testng + testng + 7.8.0 + test + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.1.2 + + + src/test/resources/testng.xml + + + + + \ No newline at end of file diff --git a/proJect/src/main/java/anlov/java/Factorial.java b/proJect/src/main/java/anlov/java/Factorial.java new file mode 100644 index 0000000..5e0b32f --- /dev/null +++ b/proJect/src/main/java/anlov/java/Factorial.java @@ -0,0 +1,15 @@ +package anlov.java; + +public class Factorial { + + public static long getFactorial(int num) { + long factorial = 1; + if (num < 0) { + throw new IllegalArgumentException("Число должно быть положительным"); + } + for (int i = 1; i <= num; i++) { + factorial *= i; + } + return factorial; + } +} diff --git a/proJect/src/test/java/FactorialTests.java b/proJect/src/test/java/FactorialTests.java new file mode 100644 index 0000000..c1cd12e --- /dev/null +++ b/proJect/src/test/java/FactorialTests.java @@ -0,0 +1,36 @@ +import anlov.java.Factorial; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import static org.testng.Assert.assertEquals; + +public class FactorialTests { + + @Test + void factorialOfZeroShouldBeOne() { + assertEquals(Factorial.getFactorial(0), 1, "Факториал нуля равен 1"); + } + + @Test(expectedExceptions = IllegalArgumentException.class) + public void negativeInputShouldThrowException() { + Factorial.getFactorial(-1); + } + + @DataProvider(name = "factorialData") + public Object[][] provideFactorialData() { + return new Object[][]{ + {0, 1}, + {1, 1}, + {2, 2}, + {3, 6}, + {5, 120}, + {7, 5040} + }; + } + + @Test(dataProvider = "factorialData") + public void testFactorial(int input, int expected) { + assertEquals(Factorial.getFactorial(input), expected); + } + +} \ No newline at end of file