Skip to content

Instantly share code, notes, and snippets.

@dherges
Created September 25, 2016 11:57
Show Gist options
  • Select an option

  • Save dherges/cd7ed2179f17b4e8c334ce3b55ce8ba8 to your computer and use it in GitHub Desktop.

Select an option

Save dherges/cd7ed2179f17b4e8c334ce3b55ce8ba8 to your computer and use it in GitHub Desktop.
ok-testing-reloaded-medium
import static spark.Spark.get;
public class TwitterApp implements SparkApplication {
@Override
public void init() {
get("/", (req, res) -> "Hello again!");
}
}
import static org.assertj.core.api.Assertions.assertThat;
import static spark.Spark.awaitInitialization;
import static spark.Spark.port;
import static spark.Spark.stop;
public class TwitterAppTest {
/** bootstraps the spark application by starting an embedded web server */
@Before
public void before() {
port(4444);
new TwitterApp().init();
awaitInitialization();
}
/** after test run, shuts down the web server */
@After
public void after() {
stop();
}
@Test
public void test() throws IOException {
final OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
final Request request = new Request.Builder()
.get()
.url("http://localhost:4444")
.build();
final Response response = okHttpClient.newCall(request).execute();
assertThat(response.body().string()).isEqualTo("Hello again!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment