Class StaticMethodChecker
- java.lang.Object
-
- de.hsh.graja.modules.junit.api.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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
StaticMethodChecker.Func3<T,U,V,R>
-
Constructor Summary
Constructors Constructor Description StaticMethodChecker()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description StaticMethodChecker
allowingDoubleReturnTolerance(double tolerance)
StaticMethodChecker
allowingMaxRuntimeMillis(long maxRuntimeMillis)
Allow a maximum runtime per method call.StaticMethodChecker
callStaticMethod(String methodName)
<T,U,V,R>
StaticMethodCheckercompareWithSample(StaticMethodChecker.Func3<T,U,V,R> func)
<T,U,R>
StaticMethodCheckercompareWithSample(BiFunction<T,U,R> func)
<T,R>
StaticMethodCheckercompareWithSample(Function<T,R> func)
<R> StaticMethodChecker
compareWithSample(Supplier<R> func)
StaticMethodChecker
onSubmission(Class<?> submissionClass)
void
with(String message, Object... args)
StaticMethodChecker
withReturnType(Class<?> returnType)
-
-
-
Method Detail
-
callStaticMethod
public StaticMethodChecker callStaticMethod(String methodName)
-
withReturnType
public StaticMethodChecker withReturnType(Class<?> returnType)
-
onSubmission
public StaticMethodChecker onSubmission(Class<?> submissionClass)
-
compareWithSample
public <R> StaticMethodChecker compareWithSample(Supplier<R> func)
-
compareWithSample
public <T,R> StaticMethodChecker compareWithSample(Function<T,R> func)
-
compareWithSample
public <T,U,R> StaticMethodChecker compareWithSample(BiFunction<T,U,R> func)
-
compareWithSample
public <T,U,V,R> StaticMethodChecker compareWithSample(StaticMethodChecker.Func3<T,U,V,R> func)
-
allowingDoubleReturnTolerance
public StaticMethodChecker allowingDoubleReturnTolerance(double tolerance)
-
allowingMaxRuntimeMillis
public StaticMethodChecker allowingMaxRuntimeMillis(long maxRuntimeMillis)
Allow a maximum runtime per method call.- Parameters:
maxRuntimeMillis
- milliseconds. Default: unbounded runtime.
-
-