Last active
September 25, 2016 12:02
-
-
Save dherges/740569069457c37fc0087c4032581b69 to your computer and use it in GitHub Desktop.
ok-testing-reloaded-medium
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
| import static spark.Spark.awaitInitialization; | |
| import static spark.Spark.port; | |
| import static spark.Spark.stop; | |
| public class SparkRunner extends BlockJUnit4ClassRunner { | |
| private Class<? extends SparkApplication> sparkApplication; | |
| private int sparkPort; | |
| public SparkRunner(Class<?> testClass) throws InitializationError { | |
| super(testClass); | |
| } | |
| @Override | |
| public void run(RunNotifier notifier) { | |
| scanAnnotation(); | |
| try { | |
| sparkBootstrap(); | |
| } catch (Exception e) { | |
| notifier.fireTestFailure(new Failure(getDescription(), e)); | |
| } | |
| super.run(notifier); | |
| sparkShutdown(); | |
| } | |
| private void scanAnnotation() { | |
| SparkApplicationTest annotation = getTestClass() | |
| .getAnnotation(SparkApplicationTest.class); | |
| sparkApplication = annotation.value(); | |
| sparkPort = annotation.port(); | |
| } | |
| private void sparkBootstrap() throws IllegalAccessException, InstantiationException { | |
| port(sparkPort); | |
| sparkApplication.newInstance().init(); | |
| awaitInitialization(); | |
| } | |
| private void sparkShutdown() { | |
| stop(); | |
| } | |
| } |
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; | |
| @RunWith(SparkRunner.class) | |
| @SparkApplicationTest(value = TwitterApp.class, port = 4444) | |
| public class TwitterAppTest { | |
| @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