Skip to content

Instantly share code, notes, and snippets.

@highel
Last active October 25, 2017 12:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save highel/c6567938ba3e6e51a11712f7a614786e to your computer and use it in GitHub Desktop.
Save highel/c6567938ba3e6e51a11712f7a614786e to your computer and use it in GitHub Desktop.
Launch full fledged Spring Boot application in separate test. This is only for integration testing with HTTP protocol, normally you should use Spring Test MVC framework to test controllers and Spring less unit testing of separate classes.
public class SpringBootExternalTest {
static CompletableFuture<Throwable> initializationException;
@BeforeClass
public static void startSpringContext() {
initializationException = new CompletableFuture<>();
Thread appThread = new Thread(() -> {
SpringApplication springApplication = new SpringApplication(
SpringBootApplication.class);
springApplication.addListeners((ApplicationListener<ApplicationReadyEvent>) event -> initializationException.complete(null));
springApplication.addListeners((ApplicationListener<ApplicationFailedEvent>) event -> initializationException.complete(event.getException()));
springApplication.run();
});
appThread.start();
try {
Throwable exception = initializationException.get(2, TimeUnit.MINUTES);
if (exception != null) {
throw new IllegalStateException("Failed to start Spring Boot application", exception);
}
} catch (InterruptedException | ExecutionException e) {
throw new IllegalStateException(e);
} catch (TimeoutException e) {
throw new IllegalStateException("Timeout expired for Spring Boot application launch", e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment