Close

Spring Boot - Unit Testing Web Application With Embedded Server

[Last Updated: Aug 11, 2020]

We can use an embedded web server to test our web application. We can achieve that by using
@SpringBootTest(webEnvironment = RANDOM_PORT)
and injecting WebTestClient.

WebTestClient is similar to WebClient but after exchange() call further chined methods are used to verify responses.

Example

pom.xml

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
</dependency>

Example web application

package com.logicbig.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MyMvcController {
  @Autowired
  private HelloService helloService;

  @GetMapping(value = "/")
  @ResponseBody
  public String sayHi(String name) {
      return helloService.getMessage(name);
  }
}
package com.logicbig.example;

import org.springframework.stereotype.Service;

@Service
public class HelloService {
  public String getMessage(String name) {
      return String.format("Hi there, %s.", name);
  }
}
package com.logicbig.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class WebAppMain {
  public static void main(String[] args) {
      SpringApplication.run(WebAppMain.class, args);
  }
}

JUnit test with embedded server

package com.logicbig.example;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class MyWebAppTest {
  @Autowired
  private WebTestClient webTestClient;

  @Test
  public void testSayHi() {
      webTestClient.get()
                   .uri(uriBuilder -> uriBuilder.path("/")
                                                .queryParam("name", "Joe")
                                                .build())
                   .exchange()
                   .expectStatus().isOk()
                   .expectBody(String.class).isEqualTo("Hi there, Joe.");
  }
}
D:\boot-unit-testing-with-embedded-server>mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ----< com.logicbig.example:boot-unit-testing-with-embedded-server >-----
[INFO] Building boot-unit-testing-with-embedded-server 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ boot-unit-testing-with-embedded-server ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\example-projects\spring-boot\unit-testing\boot-unit-testing-with-embedded-server\src\main\resources
[INFO] skip non existing resourceDirectory D:\example-projects\spring-boot\unit-testing\boot-unit-testing-with-embedded-server\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ boot-unit-testing-with-embedded-server ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to D:\example-projects\spring-boot\unit-testing\boot-unit-testing-with-embedded-server\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ boot-unit-testing-with-embedded-server ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\example-projects\spring-boot\unit-testing\boot-unit-testing-with-embedded-server\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ boot-unit-testing-with-embedded-server ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\example-projects\spring-boot\unit-testing\boot-unit-testing-with-embedded-server\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ boot-unit-testing-with-embedded-server ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
00:26:32.642 [main] DEBUG org.springframework.test.context.junit4.SpringJUnit4ClassRunner - SpringJUnit4ClassRunner constructor called with [class com.logicbig.example.MyWebAppTest]
00:26:32.648 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
00:26:32.669 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)]
00:26:32.722 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [com.logicbig.example.MyWebAppTest] from class [org.springframework.boot.test.context.SpringBootTestContextBootstrapper]
00:26:32.751 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.logicbig.example.MyWebAppTest], using SpringBootContextLoader
00:26:32.757 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.logicbig.example.MyWebAppTest]: class path resource [com/logicbig/example/MyWebAppTest-context.xml] does not exist
00:26:32.759 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.logicbig.example.MyWebAppTest]: class path resource [com/logicbig/example/MyWebAppTestContext.groovy] does not exist
00:26:32.760 [main] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.logicbig.example.MyWebAppTest]: no resource found for suffixes {-context.xml, Context.groovy}.
00:26:32.763 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.logicbig.example.MyWebAppTest]: MyWebAppTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
00:26:32.823 [main] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [com.logicbig.example.MyWebAppTest]
00:26:32.921 [main] DEBUG org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider - Identified candidate component class: file [D:\example-projects\spring-boot\unit-testing\boot-unit-testing-with-embedded-server\target\classes\com\logicbig\example\WebAppMain.class]
00:26:32.924 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Found @SpringBootConfiguration com.logicbig.example.WebAppMain for test class com.logicbig.example.MyWebAppTest
00:26:33.086 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - @TestExecutionListeners is not present for class [com.logicbig.example.MyWebAppTest]: using defaults.
00:26:33.087 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
00:26:33.158 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Skipping candidate TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener] due to a missing dependency. Specify custom listener classes or make the default listener classes and their required dependencies available. Offending class: [org/springframework/transaction/interceptor/TransactionAttributeSource]
00:26:33.162 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Skipping candidate TestExecutionListener [org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener] due to a missing dependency. Specify custom listener classes or make the default listener classes and their required dependencies available. Offending class: [org/springframework/transaction/interceptor/TransactionAttribute]
00:26:33.164 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@5f20155b, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@72ade7e3, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@239105a8, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@3fce8fd9, org.springframework.test.context.support.DirtiesContextTestExecutionListener@609bcfb6, org.springframework.test.context.event.EventPublishingTestExecutionListener@7d94beb9, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@560348e6, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@1df8b5b8, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@23202fce, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@7b993c65, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@37911f88]
00:26:33.172 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.logicbig.example.MyWebAppTest]
00:26:33.173 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.logicbig.example.MyWebAppTest]
00:26:33.218 [main] DEBUG org.springframework.test.context.junit4.SpringJUnit4ClassRunner - SpringJUnit4ClassRunner constructor called with [class com.logicbig.example.MyWebAppTest]
00:26:33.218 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
00:26:33.218 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)]
00:26:33.220 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [com.logicbig.example.MyWebAppTest] from class [org.springframework.boot.test.context.SpringBootTestContextBootstrapper]
00:26:33.222 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.logicbig.example.MyWebAppTest], using SpringBootContextLoader
00:26:33.224 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.logicbig.example.MyWebAppTest]: class path resource [com/logicbig/example/MyWebAppTest-context.xml] does not exist
00:26:33.225 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.logicbig.example.MyWebAppTest]: class path resource [com/logicbig/example/MyWebAppTestContext.groovy] does not exist
00:26:33.226 [main] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.logicbig.example.MyWebAppTest]: no resource found for suffixes {-context.xml, Context.groovy}.
00:26:33.226 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.logicbig.example.MyWebAppTest]: MyWebAppTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
00:26:33.231 [main] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [com.logicbig.example.MyWebAppTest]
00:26:33.232 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Found @SpringBootConfiguration com.logicbig.example.WebAppMain for test class com.logicbig.example.MyWebAppTest
00:26:33.235 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - @TestExecutionListeners is not present for class [com.logicbig.example.MyWebAppTest]: using defaults.
00:26:33.235 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
00:26:33.237 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Skipping candidate TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener] due to a missing dependency. Specify custom listener classes or make the default listener classes and their required dependencies available. Offending class: [org/springframework/transaction/interceptor/TransactionAttributeSource]
00:26:33.238 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Skipping candidate TestExecutionListener [org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener] due to a missing dependency. Specify custom listener classes or make the default listener classes and their required dependencies available. Offending class: [org/springframework/transaction/interceptor/TransactionAttribute]
00:26:33.240 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@275bf9b3, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@1b8a29df, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@4fbe37eb, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@12a94400, org.springframework.test.context.support.DirtiesContextTestExecutionListener@6a47b187, org.springframework.test.context.event.EventPublishingTestExecutionListener@2049a9c1, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@1ef6d34c, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@46271dd6, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@11bb571c, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@7c51f34b, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@5495333e]
00:26:33.241 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.logicbig.example.MyWebAppTest]
00:26:33.241 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.logicbig.example.MyWebAppTest]
00:26:33.339 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.logicbig.example.MyWebAppTest]
00:26:33.339 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.logicbig.example.MyWebAppTest]
[INFO] Running com.logicbig.example.MyWebAppTest
00:26:33.345 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.logicbig.example.MyWebAppTest]
00:26:33.346 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.logicbig.example.MyWebAppTest]
00:26:33.347 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.logicbig.example.MyWebAppTest]
00:26:33.347 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.logicbig.example.MyWebAppTest]
00:26:33.360 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - Before test class: context [DefaultTestContext@3b96c42e testClass = MyWebAppTest, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@5d066c7d testClass = MyWebAppTest, locations = '{}', classes = '{class com.logicbig.example.WebAppMain}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true, server.port=0}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@279fedbd, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@2925bf5b, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@2002fc1d, org.springframework.boot.test.web.reactive.server.WebTestClientContextCustomizer@4988d8b8, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@561b6512], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> false]], class annotated with @DirtiesContext [false] with mode [null].
00:26:33.386 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.logicbig.example.MyWebAppTest]
00:26:33.386 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.logicbig.example.MyWebAppTest]
00:26:33.394 [main] DEBUG org.springframework.test.context.support.DependencyInjectionTestExecutionListener - Performing dependency injection for test context [[DefaultTestContext@3b96c42e testClass = MyWebAppTest, testInstance = com.logicbig.example.MyWebAppTest@34c01041, testMethod = [null], testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@5d066c7d testClass = MyWebAppTest, locations = '{}', classes = '{class com.logicbig.example.WebAppMain}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true, server.port=0}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@279fedbd, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@2925bf5b, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@2002fc1d, org.springframework.boot.test.web.reactive.server.WebTestClientContextCustomizer@4988d8b8, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@561b6512], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> false]]].
00:26:33.427 [main] DEBUG org.springframework.test.context.support.TestPropertySourceUtils - Adding inlined properties to environment: {spring.jmx.enabled=false, org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true, server.port=0}

. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.5.RELEASE)

2020-04-04 00:26:33.865 INFO 16912 --- [ main] com.logicbig.example.MyWebAppTest : Starting MyWebAppTest on DESKTOP-VCSEKL5 with PID 16912 (started by Joe in D:\example-projects\spring-boot\unit-testing\boot-unit-testing-with-embedded-server)
2020-04-04 00:26:33.869 INFO 16912 --- [ main] com.logicbig.example.MyWebAppTest : No active profile set, falling back to default profiles: default
2020-04-04 00:26:36.095 INFO 16912 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 0 (http)
2020-04-04 00:26:36.111 INFO 16912 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-04-04 00:26:36.112 INFO 16912 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.31]
2020-04-04 00:26:36.291 INFO 16912 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-04-04 00:26:36.292 INFO 16912 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2360 ms
2020-04-04 00:26:36.882 INFO 16912 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-04-04 00:26:38.100 INFO 16912 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 53480 (http) with context path ''
2020-04-04 00:26:38.103 INFO 16912 --- [ main] com.logicbig.example.MyWebAppTest : Started MyWebAppTest in 4.673 seconds (JVM running for 6.398)
2020-04-04 00:26:39.527 INFO 16912 --- [o-auto-1-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-04-04 00:26:39.528 INFO 16912 --- [o-auto-1-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-04-04 00:26:39.554 INFO 16912 --- [o-auto-1-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 25 ms
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 6.328 s - in com.logicbig.example.MyWebAppTest
2020-04-04 00:26:41.777 INFO 16912 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 16.164 s
[INFO] Finished at: 2020-04-04T00:26:42-05:00
[INFO] ------------------------------------------------------------------------

Example Project

Dependencies and Technologies Used:

  • Spring Boot 2.2.5.RELEASE
  • spring-boot-starter-web : Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container.
  • spring-boot-starter-webflux : Starter for building WebFlux applications using Spring Framework's Reactive Web support.
  • spring-boot-starter-test : Starter for testing Spring Boot applications with libraries including JUnit, Hamcrest and Mockito.
  • JDK 1.8
  • Maven 3.5.4

Boot - Embedded server with WebTestClient Example Select All Download
  • boot-unit-testing-with-embedded-server
    • src
      • main
        • java
          • com
            • logicbig
              • example
      • test
        • java
          • com
            • logicbig
              • example
                • MyWebAppTest.java

    See Also