Class StaticMethodChecker


  • public class StaticMethodChecker
    extends Object

    The StaticMethodChecker invokes a static method of a student submitted class and compares the return value with the return value of a sample implementation of that method. Currently the static method may have zero to three arguments and a return value. The type of arguments and return value can be any of the following types: int, double, int[], double[], int[][] or double[][] or the respective wrapper classes. If the return values differ, a CommentAssertionError will be thrown that contains a detailed description of the causing method call.

    Example:

     @RunWith(GrajaRunner.class)
     public class Grader  {
        @InjectContext public static GraderContext ctx;
         private static StaticMethodChecker check;
         
        @BeforeClass public static void setUpOnce() {
             check= new StaticMethodChecker()
                     .callStaticMethod("create")
                     .withReturnType(double[].class)
                     .onSubmission(ctx.getPublicClassForName("ArrayFactory"))
                     .compareWithSample(Grader::sampleImpl)
                     .allowingMaxRuntimeMillis(10000)
                     .allowingDoubleReturnTolerance(0.0);
         }
        @Test public void testValid() {
             check.with("Valid parameters", 4, 0.6);
         }
        @Test public void testInvalid() {
             check.with("Invalid parameters", -1, 0.6);
         }
         private static double[] sampleImpl(int n, double v) {
             if (n < 0) throw new IllegalArgumentException();
             double[] a= new double[n];
             Arrays.fill(a, v);
             return a;
         }
     }
     

    When applied to the following poor student submission, the test leads to the following student feedback:

    Poor submission:

     public class ArrayFactory {
         public static double[] create(int n, double v) {
             return new double[n];
         }
     }
     

    Feedback of testValid method

    When trying to call

    ArrayFactory.create(4, 0.6)

    the following error was detected:

    Expected result: [0.6, 0.6, 0.6, 0.6]

    Observed result: [0.0, 0.0, 0.0, 0.0]

    Feedback of testInvalid method:

    When trying to call

    ArrayFactory.create(-1, 0.6)

    the following error was detected:

    Expected result: java.lang.IllegalArgumentException

    Observed result: java.lang.NegativeArraySizeException