Unit tests code for a Java class that that checks whether an array is sorted I'm trying to write unit tests code for a java class. The class tests an array for sorted (either way) or not.

Here is the Unit Tests code:

public class SortedOrNotTest
{

   @Test
   public void test()
   {
      int[] A = { 32, 32, 32, 32, 32, 32, 32, 32, 34 };
      assertEquals(true, SortedOrNot.isSorted(A));

      int[] B = { 32, 32, 34, 33 };
      assertEquals(false, SortedOrNot.isSorted(B));

      int[] C = { 32 };
      assertEquals(true, SortedOrNot.isSorted(C));

      int[] D = {};
      assertEquals(true, SortedOrNot.isSorted(D));

      int[] E = { 32, 32, 31, 30, 32 };
      assertEquals(false, SortedOrNot.isSorted(E));
   }
}


Here is the Class, it checks an array is sorted or not in either way:

public class SortedOrNot
{

   public static boolean isSorted(int[] arr)
   {
      int n = arr.length;

      if(n&lt;2) return true;

      int i = 0;
      while (i &lt; n - 1 &amp;&amp; arr[i] == arr[i + 1]) // same elements at the beginning
      {
         i++;
      }

      if (i == n - 1)
         return true;   // all same elements considered as sorted.

      if (arr[i] &lt; arr[i + 1]) // candidate for ascending, non-decreasing
      {
         i++;

         for (; i &lt; n - 1; i++)
         {
            if (arr[i] &gt; arr[i + 1])
               return false;
         }

         return true;
      }

      else // candidate for descending, non-increasing
      {
         i++;

         for (; i &lt; n - 1; i++)
         {
            if (arr[i] &lt; arr[i + 1])
               return false;
         }
         return true;
      }
   }
}


Tests works, need to make sure that I'm doing in correct way.
I don't see anything wrong with the way you are testing.

You may want to include negative numbers in your test cases. 

I prefer to have test cases broken up. It's easier to track down failing tests:

@Test
public void testIsSortedSingleNode()
{
    int[] C = { 32 };
    assertEquals(true, SortedOrNot.isSorted(C));
}


Even in tests you should follow naming standards. Names such as A, B C are not very descriptive. Also they should start with a lower-case letter. result would be a better name. You use the same variable throughout.

Use assertTrue and assertFalse instead of assertEquals(true/false, x).

IMO arrayLength would be a better name then n. Or you could use arr.length.