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

create a little MockGitHub class for tests mocking out the github REST API #382

Closed
jstrachan opened this issue Sep 26, 2017 · 5 comments
Closed

Comments

@jstrachan
Copy link

there's a few projects I'd really like a simple mock implementation of GitHub for easier testing.

I'm fine with it being pretty basic (and defaulting to just not finding stuff ;) but if it was able to find users/orgs/repos/PRs and whatnot from in memory objects it'd be super handy.

I started a little spike using a real JAXRS server side and tried reusing the DTOs from this excellent library; but the way the DTOs work they are kinda hard wired into the Requestor; so figure it was maybe simplest to just add a little MockGitHub derived class. By barely changing the code I was able to get something basic going without breaking the GitHub API or implementation.

I can submit a little PR to show you the kinda thing am thinking. Its kinda an extension of the offline mode but just with a custom API that lets you populate some resources etc

@jstrachan
Copy link
Author

@kohsuke what do you think of the general idea? I've got a little spike going but thought I'd wait for your thoughts before i go further. Can query/add users/orgs so far - gotta figure out how to support creating repos etc...

@jgangemi
Copy link
Contributor

couldn't you do this w/ wiremock and return canned json responses?

either way, i support this idea!

@ometa
Copy link

ometa commented Oct 27, 2017

This would be an excellent addition.

@jstrachan
Copy link
Author

jstrachan commented Nov 14, 2017

Here's my first spike https://github.com/jstrachan/github-api/tree/mock-api

e.g. https://github.com/jstrachan/github-api/blob/mock-api/src/test/java/org/kohsuke/github/mock/MockGitHubTest.java#L45

so we can create a mock user + list the repos for the user & create a new repository. There's an issue printing out the repository though (ownerName issues) due to:

estUser(org.kohsuke.github.mock.MockGitHubTest)  Time elapsed: 0.303 sec  <<< ERROR!
java.lang.NullPointerException
	at org.kohsuke.github.GHRepository.getOwnerName(GHRepository.java:355)
	at org.kohsuke.github.GHRepository.hashCode(GHRepository.java:1571)
	at java.util.WeakHashMap.hash(WeakHashMap.java:298)
	at java.util.WeakHashMap.put(WeakHashMap.java:449)
	at org.apache.commons.lang.builder.ToStringStyle.register(ToStringStyle.java:181)
	at org.apache.commons.lang.builder.ToStringStyle.appendClassName(ToStringStyle.java:1443)
	at org.apache.commons.lang.builder.ToStringStyle.appendStart(ToStringStyle.java:368)
	at org.apache.commons.lang.builder.ToStringBuilder.<init>(ToStringBuilder.java:266)
	at org.apache.commons.lang.builder.ReflectionToStringBuilder.<init>(ReflectionToStringBuilder.java:539)
	at org.kohsuke.github.GHObject$1.<init>(GHObject.java:108)
	at org.kohsuke.github.GHObject.toString(GHObject.java:108)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at org.kohsuke.github.mock.MockGitHubTest.testUser(MockGitHubTest.java:58)

@kohsuke
Copy link
Collaborator

kohsuke commented Nov 15, 2017

@jstrachan , my apologies for the delay. Yes, I think better testability would be great, and ability to mock things would be wonderful. I think @stephenc worked in some Jenkins plugins to extensively mock GH*** objects from literal JSON files. How can I help make this happen?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants