Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save elvismetaphor/1bb6c8830bd43029232c77f7d6f86894 to your computer and use it in GitHub Desktop.
Save elvismetaphor/1bb6c8830bd43029232c77f7d6f86894 to your computer and use it in GitHub Desktop.
package com.example.retrofit;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import static junit.framework.TestCase.assertTrue;
// 一般來說,只要把名稱取為 ContributorAppTest 即可
public class ContributorAppIntegrationTest {
@Test
public void When_GetContributor_Should_GetMoreThan1() throws Exception {
// Arrange
ByteArrayOutputStream stream = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(stream);
System.setOut(printStream);
SimpleRefactorService.ContributorApp app =
new SimpleRefactorService.ContributorApp(
new SimpleRefactorService.ContributorManagerImpl(),
new SimpleRefactorService.ContributorPresenterImpl());
// Action
app.execute();
// Assert
assertTrue(stream.toString().split(System.lineSeparator()).length > 1);
}
@Test
public void When_GetContributor_Should_ContainJakeWharton() throws Exception {
// Arrange
ByteArrayOutputStream stream = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(stream);
System.setOut(printStream);
SimpleRefactorService.ContributorApp app =
new SimpleRefactorService.ContributorApp(
new SimpleRefactorService.ContributorManagerImpl(),
new SimpleRefactorService.ContributorPresenterImpl());
// Action
app.execute();
// Assert
assertTrue(stream.toString().contains("JakeWharton"));
}
}
package com.example.retrofit;
import org.junit.Test;
import java.io.IOException;
import static junit.framework.TestCase.assertEquals;
// 一般來說,只要把名稱取為 ContributorAppTest 即可
public class ContributorAppUnitTest {
@Test
public void When_Get2Contributors_Then_Present2Contributors() throws IOException{
// Arrange
MockContributorManager manager = new MockContributorManager();
MockContributorPresenter presenter = new MockContributorPresenter();
SimpleRefactorService.ContributorApp testee =
new SimpleRefactorService.ContributorApp(manager, presenter);
// Action
testee.execute();
// Assert
assertEquals(2, presenter.getPresentCount());
}
}
package com.example.retrofit;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class MockContributorManager implements SimpleRefactorService.ContributorManager {
@Override
public List<SimpleRefactorService.Contributor> getContributors() throws IOException {
List<SimpleRefactorService.Contributor> contributors = new ArrayList<>();
contributors.add(new SimpleRefactorService.Contributor("JakeWharton", 1000));
contributors.add(new SimpleRefactorService.Contributor("ElvisLin", 1000));
return contributors;
}
}
package com.example.retrofit;
import java.util.List;
public class MockContributorPresenter implements SimpleRefactorService.ContributorPresenter {
private int presentCount = 0;
@Override
public void presentContributors(List<SimpleRefactorService.Contributor> contributors) {
for (SimpleRefactorService.Contributor contributor : contributors) {
presentCount++;
}
}
public int getPresentCount() {
return presentCount;
}
}
/*
* Copyright (C) 2012 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.retrofit;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Path;
import java.io.IOException;
import java.util.List;
public final class SimpleRefactorService {
public static final String API_URL = "https://api.github.com";
public static class Contributor {
public final String login;
public final int contributions;
public Contributor(String login, int contributions) {
this.login = login;
this.contributions = contributions;
}
}
public interface GitHub {
@GET("/repos/{owner}/{repo}/contributors")
Call<List<Contributor>> contributors(
@Path("owner") String owner,
@Path("repo") String repo);
}
public interface ContributorManager {
List<Contributor> getContributors() throws IOException;
}
public static class ContributorManagerImpl implements ContributorManager {
public List<Contributor> getContributors() throws IOException {
// Create a very simple REST adapter which points the GitHub API.
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
// Create an instance of our GitHub API interface.
GitHub github = retrofit.create(GitHub.class);
// Create a call instance for looking up Retrofit contributors.
Call<List<Contributor>> call = github.contributors("square", "retrofit");
// Fetch and print a list of the contributors to the library.
List<Contributor> contributors = call.execute().body();
return contributors;
}
}
public interface ContributorPresenter {
void presentContributors(List<Contributor> contributors);
}
public static class ContributorPresenterImpl implements ContributorPresenter {
public void presentContributors(List<Contributor> contributors) {
for (Contributor contributor : contributors) {
System.out.println(contributor.login + " (" + contributor.contributions + ")");
}
}
}
public static class ContributorApp {
private ContributorManager manager;
private ContributorPresenter presenter;
public ContributorApp(ContributorManager manager, ContributorPresenter presenter) {
this.manager = manager;
this.presenter = presenter;
}
public void execute() throws IOException {
List<Contributor> contributors = manager.getContributors();
presenter.presentContributors(contributors);
}
}
public static void main(String... args) throws IOException {
ContributorApp app = new ContributorApp(new ContributorManagerImpl(), new ContributorPresenterImpl());
app.execute();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment