Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose mock method api #89

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,13 @@ public class StubGenerator {
new HashMap<ClassAndMethod, StubMethod>();
static {
// Anchor.getElement must return an AnchorElement rather than a plain Element
STUB_METHODS.put(
new ClassAndMethod(Anchor.class, "getAnchorElement"),
new ReturnMockStubMethod(AnchorElement.class));
replaceMethodWithMock(Anchor.class, "getAnchorElement", AnchorElement.class);
// ListBox.getSelectElement must return a SelectElement rather than a plain Element
STUB_METHODS.put(
new ClassAndMethod(ListBox.class, "getSelectElement"),
new ReturnMockStubMethod(SelectElement.class));
replaceMethodWithMock(ListBox.class, "getSelectElement", SelectElement.class);
// TextBox.getInputElement must return an InputElement rather than a plain Element
STUB_METHODS.put(
new ClassAndMethod(TextBox.class, "getInputElement"),
new ReturnMockStubMethod(InputElement.class));
replaceMethodWithMock(TextBox.class, "getInputElement", InputElement.class);
// InputElement needs to be able to convert generic elements into input elements
STUB_METHODS.put(
new ClassAndMethod(InputElement.class, "as"),
new ReturnMockStubMethod(InputElement.class));
replaceMethodWithMock(InputElement.class, "as", InputElement.class);
// URL.encodeQueryStringImpl
STUB_METHODS.put(
new ClassAndMethod(URL.class, "encodeQueryStringImpl"),
Expand All @@ -72,6 +64,18 @@ public class StubGenerator {
new ReturnStringStubMethod("encodePathSegmentImpl"));
}

/**
* Replaces implementation of any method (including static ones) with given name
* by a method that returns mocks of a target class.
* @param source class whose method should be replaced
* @param methodName method name
* @param target class of mocks to be returned
*/
public static void replaceMethodWithMock(Class<?> source, String methodName, Class<?> target) {
STUB_METHODS.put(new ClassAndMethod(source, methodName),
new ReturnMockStubMethod(target));
}

/** Returns whether the behavior of the given method should be replaced. */
public static boolean shouldStub(CtMethod method, Collection<Class<?>> classesToStub) {
// Stub any methods for which we have given explicit implementations
Expand Down Expand Up @@ -101,7 +105,6 @@ public static Object invoke(Class<?> returnType, String className, String method
if (STUB_METHODS.containsKey(new ClassAndMethod(className, methodName))) {
return STUB_METHODS.get(new ClassAndMethod(className, methodName)).invoke();
}

// Otherwise return an appropriate basic type
if (returnType == String.class) {
return "";
Expand Down