wiki:comTestEn
Last modified 7 years ago Last modified on 2011-04-10 20:06:53

Make testing easy: ComTest

Projet/Properties/Java Build Path/Add Library/JUnit/JUnit 4 Finish 

How to test a simple function

  1. Create a new package 'examples' and go there
  2. Write following program:
      public class LowestDivider {
      //#STATICIMPORT
          
          /**
           * A function to find the lowest divider of a number.
           * @param n number to be examined
           * @return the lowest divider.  1 if prime number.
           * @example
           * <pre name="test">
           *   lowestDivider(1) === 1;
           *   lowestDivider(2) === 1;
           *   lowestDivider(3) === 1;
           *   lowestDivider(4) === 2;
           *   lowestDivider(5) === 1;
           *   lowestDivider(6) === 2;
           * </pre>
           */
          public static int lowestDivider(int n) {
              for (int i=2; i<=n/2; i++)
                  if ( n % i == 0 ) return i;
              return 1;
          }
          
          /**
           * @param args not in use
           */
          public static void main(String[] args) {
              int n = 25;
              int divider = lowestDivider(n);
              System.out.println(divider);
          }

      }

  1. Run ComTest: Right mouse click and ComTest/Generate and run
  2. Add more tests to the example. Try a wrong value too.
  3. Alter the subprogram so that it wont divide with even numbers for nothing.
  4. Test if it still works
  5. Change the upper limit of the test as the square root of the number (the divison wont be equal with higher than that).
  6. NOTE: Any problems, refer, https://trac.cc.jyu.fi/projects/ohj2/wiki/ohj2ComTest (only in Finnish)