Last active
October 25, 2017 12:03
-
-
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.
This file contains 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 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