This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Representation of a Tweet object transferred from/to Twitter REST API in JSON format. | |
*/ | |
public class Tweet { | |
public long id; | |
public String text; | |
public User user; | |
@Json(name="created_at") public Date createdAt; | |
public boolean favorited; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class MyAssertions { | |
public static RecordedRequestAssert assertThat(RecordedRequest recordedRequest) { | |
return new RecordedRequestAssert(recordedRequest); | |
} | |
public static ResponseAssert assertThat(Response<?> response) { | |
return new ResponseAssert(response); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TwitterApiTest { | |
@Rule | |
public MockWebServerPlus server = new MockWebServerPlus(); | |
@Test | |
public void tweet_success() throws IOException { | |
server.enqueue("twitter/statuses/update_ok"); | |
final Response<Tweet> response = twitterApi() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static spark.Spark.get; | |
public class TwitterApp implements SparkApplication { | |
@Override | |
public void init() { | |
get("/", (req, res) -> "Hello again!"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Retention(RetentionPolicy.RUNTIME) | |
@Target(ElementType.TYPE) | |
public @interface SparkApplicationTest { | |
/** SparkApplication that will be tested */ | |
Class<? extends SparkApplication> value(); | |
/** Port number for the embedded web server */ | |
int port() default spark.Service.SPARK_DEFAULT_PORT; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RetweetsIdRoute implements Route { | |
@Override | |
public Object handle(Request request, Response response) throws Exception { | |
final String idParam = request.params(":id"); | |
final String count = request.queryParams("count"); | |
final List<Tweet> tweetList = fetchTweets( | |
Long.valueOf(idParam), | |
Integer.valueOf(count != null && count.length() > 0 ? count : "100") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ContentTypeRoute implements Route { | |
private final String contentType; | |
private final Route delegate; | |
private final Optional<ResponseTransformer> responseTransformer; | |
private ContentTypeRoute(String contentType, Route delegate, | |
ResponseTransformer responseTransformer) { | |
this.contentType = contentType; | |
this.delegate = delegate; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RetweetsIdRoute implements Route { | |
private List<Tweet> fetchTweets(long id, int count) { | |
return IntStream.range(0, 99) | |
.mapToObj((i) -> { | |
final Tweet tweet = new Tweet(); | |
tweet.id = id + i; | |
tweet.text = String.format("Retweet #%s of %s for %s", i, count, id); | |
tweet.favorited = i % 2 == 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.assertj.core.api.Assertions.assertThat; | |
import static ext.assertj.MyAssertions.assertThat; | |
@RunWith(SparkRunner.class) | |
@SparkApplicationTest(value = TwitterApp.class, port = 4444) | |
public class TwitterAppTest { | |
private final OkHttpClient okHttpClient = new OkHttpClient.Builder().build(); | |
@Test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.assertj.core.api.Assertions.fail; | |
public class OkHttpResponseAssert | |
extends AbstractAssert<OkHttpResponseAssert, Response> | |
implements Assert<OkHttpResponseAssert, Response> { | |
/* ... */ | |
public <T> OkHttpResponseAssert jsonPath(String jsonPath, Class<T> type, T expected) { | |
T object = JsonPath.parse(consumeResponseBody()).read(jsonPath, type); |