Class StaticMethodChecker
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
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionallowingDoubleReturnTolerance
(double tolerance) allowingMaxRuntimeMillis
(long maxRuntimeMillis) Allow a maximum runtime per method call.callStaticMethod
(String methodName) <T,
U, V, R> StaticMethodChecker compareWithSample
(StaticMethodChecker.Func3<T, U, V, R> func) <T,
U, R> StaticMethodChecker compareWithSample
(BiFunction<T, U, R> func) <T,
R> StaticMethodChecker compareWithSample
(Function<T, R> func) compareWithSample
(Supplier<R> func) onSubmission
(Class<?> submissionClass) void
withReturnType
(Class<?> returnType)
-
Constructor Details
-
StaticMethodChecker
public StaticMethodChecker()
-
-
Method Details
-
callStaticMethod
-
withReturnType
-
onSubmission
-
compareWithSample
-
compareWithSample
-
compareWithSample
-
compareWithSample
-
allowingDoubleReturnTolerance
-
allowingMaxRuntimeMillis
Allow a maximum runtime per method call.- Parameters:
maxRuntimeMillis
- milliseconds. Default: unbounded runtime.
-
with
-