@Before public void setUp() { Executor executor = Executors.newSingleThreadExecutor(); instance = new Reddit(executor); Http mockHttp = mock(Http.class); instance.http = mockHttp; }
@Test public void testGetPosts() throws Exception { ListenableFuture futu; futu = mock(ListenableFuture.class); // mock the get method to return our special future when(instance.http.get(any(String.class))).thenReturn(futu); // the special future's addListener method // needs to be hacked functional when(futu.addListener(any(Runnable.class), any(Executor.class))) .thenAnswer( new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { Object[] args = invocation.getArguments(); Runnable run = (Runnable) args[0]; run.run(); return null; } }); // finally as the listener runs we feed it our special JSON when(futu.get()) .thenReturn( new JSONObject() { { put( "data", new JSONObject() { { put( "children", new JSONArray() { { add( new JSONObject() { { put( "data", new JSONObject() { { put("author", "hylje"); put("url", "http://www.google.com/"); put("id", "abcde"); put("title", "Yet another fake Reddit post"); } }); } }); add( new JSONObject() { { put( "data", new JSONObject() { { put("author", "hylje"); put("url", "http://www.bash.org/"); put("id", "abcdf"); put("title", "You gotta be kidding me"); } }); } }); } }); } }); } }); ListenableFuture<List<Post>> post_futu = instance.get_posts("foo"); List<Post> posts = post_futu.get(); assertEquals(posts.get(0).author, "hylje"); assertEquals(posts.get(0).id, "abcde"); assertEquals(posts.get(1).title, "You gotta be kidding me"); }
public MircString reddit(String subreddit) { MircString mircString = new MircString(); String redditOutput = Reddit.redditTopLink(subreddit); mircString = MircString.of(redditOutput); return mircString; }
/** * Gets the comments for this Post. * * @param post The Post you want Comments from. * @param sort The order in which you want the results to be sorted. * @param callback A function that will process the results or errors. * @throws RedditException */ public void getComments(final SortMode sort, final CommentListener callback) { Reddit.getInstance().getComments(this.subreddit, this.id, sort, callback); }