Repository: TheAlgorithms/Java
Pull request #4227 (2023-07-01 15:25:21)

Added unit tests for PalindromeNumber class
Changes:
- added unit tests
- minor refactoring

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.



Patch:
diff --git a/DIRECTORY.md b/DIRECTORY.md
index 782bb74a..926c4bd9 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -655,6 +655,7 @@
             * [MedianTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/MedianTest.java)
             * [MobiusFunctionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/MobiusFunctionTest.java)
             * [NthUglyNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/NthUglyNumberTest.java)
+            * [PalindromeNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/PalindromeNumberTest.java)
             * [PascalTriangleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/PascalTriangleTest.java)
             * [PerfectCubeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/PerfectCubeTest.java)
             * [PerfectNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/PerfectNumberTest.java)
diff --git a/src/main/java/com/thealgorithms/maths/PalindromeNumber.java b/src/main/java/com/thealgorithms/maths/PalindromeNumber.java
index 56c34d3d..5dad99ef 100644
--- a/src/main/java/com/thealgorithms/maths/PalindromeNumber.java
+++ b/src/main/java/com/thealgorithms/maths/PalindromeNumber.java
@@ -1,13 +1,6 @@
 package com.thealgorithms.maths;
 
 public class PalindromeNumber {
-
-    public static void main(String[] args) {
-        assert isPalindrome(12321);
-        assert !isPalindrome(1234);
-        assert isPalindrome(1);
-    }
-
     /**
      * Check if {@code n} is palindrome number or not
      *
@@ -17,7 +10,7 @@ public class PalindromeNumber {
      */
     public static boolean isPalindrome(int number) {
         if (number < 0) {
-            throw new IllegalArgumentException(number + "");
+            throw new IllegalArgumentException("Input parameter must not be negative!");
         }
         int numberCopy = number;
         int reverseNumber = 0;


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/PalindromeNumberTest.java b/src/test/java/com/thealgorithms/maths/PalindromeNumberTest.java
new file mode 100644
index 00000000..a70100c0
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/PalindromeNumberTest.java
@@ -0,0 +1,30 @@
+package com.thealgorithms.maths;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/**
+ * @author Albina Gimaletdinova on 01/07/2023
+ */
+public class PalindromeNumberTest {
+    @Test
+    public void testNumbersArePalindromes() {
+        Assertions.assertTrue(PalindromeNumber.isPalindrome(0));
+        Assertions.assertTrue(PalindromeNumber.isPalindrome(1));
+        Assertions.assertTrue(PalindromeNumber.isPalindrome(2332));
+        Assertions.assertTrue(PalindromeNumber.isPalindrome(12321));
+    }
+
+    @Test
+    public void testNumbersAreNotPalindromes() {
+        Assertions.assertFalse(PalindromeNumber.isPalindrome(12));
+        Assertions.assertFalse(PalindromeNumber.isPalindrome(990));
+        Assertions.assertFalse(PalindromeNumber.isPalindrome(1234));
+    }
+
+    @Test
+    public void testIfNegativeInputThenExceptionExpected() {
+        IllegalArgumentException exception = Assertions.assertThrows(IllegalArgumentException.class, () -> PalindromeNumber.isPalindrome(-1));
+        Assertions.assertEquals(exception.getMessage(), "Input parameter must not be negative!");
+    }
+}