Class Mock<T>
java.lang.Object
io.github.valentinherrmann.levenshtein.Mock<T>
- Type Parameters:
T- the type of the class being mocked
An object of a student class whose methods the test can replace, and which records every call to its methods.
Created with
ClassWrapper.mock(); used to keep tests independent: a mistake in one student method then
only fails the tests of that method.
Mock<?> guitarist = guitaristWrapper.mock()
.override(guitaristWrapper.calculateLoudness, args -> (int) args[0] * 7);
Object drummer = drummerWrapper.newObjWithoutConstructor();
drummerWrapper.guitarist.setValue(drummer, guitarist.obj());
assertThat(drummerWrapper.calculateLoudness.invokeOnSpecificObject(drummer, 5, 3)).isEqualTo(38);
guitarist.assertCalled(guitaristWrapper.calculateLoudness, 1);
- The object is an instance of a generated subclass. No constructor of the student class runs, so all
attributes keep their default values (
0,false,null); set them withAttributeWrapper.setValue(Object, Object)if needed. - An overridden method runs the test's function. Every other method runs the student's code (supervised by
Ares as usual), or returns its default value after
othersReturnDefaults(). - Methods are matched by the method the wrapper actually found, so a misspelt but tolerated name is
still overridden. Calls from the student's own code (
this.getCount()) are intercepted, too. - Every call of a method that can be overridden is counted, and the arguments of the first
1000<T> calls per method are recorded (arrays as copies, other objects as references),
see
callCount(MethodWrapper),calls(MethodWrapper)andassertCalled(MethodWrapper, int). - Not possible:
finalclasses and methods, records, enums, sealed classes,staticandprivatemethods, constructors, and attributes read directly by student code.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intCalls per method whose arguments are kept; further calls are only counted. -
Method Summary
Modifier and TypeMethodDescriptionvoidassertCalled(MethodWrapper<?, ?> method, int times) Fails unless the method was called exactlytimestimes onobj().voidassertCalled(MethodWrapper<?, ?> method, int times, Supplier<String> context) LikeassertCalled(MethodWrapper, int), but the text ofcontextis put in front of the failure message, e.g. which call of the method under test should have called this method.voidassertNotCalled(MethodWrapper<?, ?> method) Fails if the method was called onobj().longcallCount(MethodWrapper<?, ?> method) How often a method was called onobj()(or on a clone of it).calls(MethodWrapper<?, ?> method) The recorded calls of a method onobj(), in call order: the first 1000<T> calls, seecallCount(MethodWrapper)for the number of all calls.obj()The object of the mock, created on the first call (without running a constructor) and the same afterwards.Makes this a full mock: methods without override return their default value (0,false,null) instead of running the student's code.override(MethodWrapper<?, ?> method, Function<Object[], Object> function) Replaces a method: every call of it runsfunctioninstead of the student's code.voidForgets all recorded calls, e.g. after preparing the object.returning(MethodWrapper<?, ?> method, Object value) Replaces a method by one that always returnsvalue.
-
Field Details
-
MAX_RECORDED_CALLS
public static final int MAX_RECORDED_CALLSCalls per method whose arguments are kept; further calls are only counted.- See Also:
-
-
Method Details
-
override
Replaces a method: every call of it runsfunctioninstead of the student's code. The function gets the arguments converted to the parameter types the wrapper expects (e.g. anintasInteger, also if the student declaredlong). Its result is converted to the student's return type (numeric types are converted, e.g. anIntegerfor adoublemethod) and ignored forvoidmethods. If the function throws (or returnsnullfor a primitive return type), the call fails like a call that throws. The function runs as test code: Ares does not restrict it, but it is called from student code, so keep it to plain computations.A method that is missing fails with the usual message. A method that cannot be overridden (
final,static,private, or one of another class) fails with a readable message.- Parameters:
method- a method of the mocked class or of one of its superclasses or interfacesfunction- computes the return value from the arguments- Returns:
- this mock
-
returning
Replaces a method by one that always returnsvalue.- Parameters:
method- a method of the mocked class or of one of its superclasses or interfacesvalue- the value to return (converted like the result ofoverride(MethodWrapper, Function))- Returns:
- this mock
-
othersReturnDefaults
Makes this a full mock: methods without override return their default value (0,false,null) instead of running the student's code. Their calls are still recorded.equals,hashCodeandtoStringwithout override behave like those ofObject(identity), so the object still works in lists, sets and messages.- Returns:
- this mock
-
obj
The object of the mock, created on the first call (without running a constructor) and the same afterwards. Overrides added later apply to it as well.- Returns:
- the object, an instance of a generated subclass of the mocked class
-
callCount
How often a method was called onobj()(or on a clone of it).- Parameters:
method- a method of the mocked class or of one of its superclasses or interfaces- Returns:
- the number of calls
-
calls
The recorded calls of a method onobj(), in call order: the first 1000<T> calls, seecallCount(MethodWrapper)for the number of all calls. Arrays were copied when the call was recorded; other objects are the ones passed.- Parameters:
method- a method of the mocked class or of one of its superclasses or interfaces- Returns:
- the arguments of each call, converted to the parameter types the wrapper expects where they fit
(a value that does not fit, e.g. 2.5 for a student's
floatwhereintis expected, is returned as passed)
-
assertCalled
Fails unless the method was called exactlytimestimes onobj(). The message shows the arguments of the recorded calls.- Parameters:
method- a method of the mocked class or of one of its superclasses or interfacestimes- the expected number of calls
-
assertCalled
LikeassertCalled(MethodWrapper, int), but the text ofcontextis put in front of the failure message, e.g. which call of the method under test should have called this method.- Parameters:
method- a method of the mocked class or of one of its superclasses or interfacestimes- the expected number of callscontext- supplies the text shown in front of the failure message (may benull)
-
assertNotCalled
Fails if the method was called onobj().- Parameters:
method- a method of the mocked class or of one of its superclasses or interfaces
-
resetCalls
public void resetCalls()Forgets all recorded calls, e.g. after preparing the object.
-