Return to site

🍃🎓 SPRING CERTIFICATION QUESTION: Which testing web environment modes make the embedded server gets started?

· vcp,java

Which testing web environment modes make the embedded server gets started?

* DEFINED_PORT

* MOCK

* NONE

* RANDOM_PORT

#spring #certificationquestion #vcp

The embedded server gets started by the testing framework when RANDOM_PORT, DEFINED_PORT are used.

When working with a web application in Spring Boot, it's important to note that it typically comes with an embedded servlet container, which is Tomcat by default.

However, when it comes to your testing environment, you have some choices to make regarding Tomcat's behavior.

This is where the concept of the 'web environment' comes into play.

You can specify whether you want a web environment, and if so, whether you want to 'mock' it.

When you choose to mock the web environment, Tomcat won't actually start, but you'll still have the ability to access web components for testing purposes.

Alternatively, you can decide not to use a web environment at all.

If you do want Tomcat to start, you can further specify whether it should start on a randomly assigned port that won't conflict with any other ports in use on your local machine, or on a specific port that you define.

Java Doc:

DEFINED_PORT

Creates a (reactive) web application context without defining any server.port=0 Environment property.

MOCK

Creates a WebApplicationContext with a mock servlet environment if servlet APIs are on the classpath, a ReactiveWebApplicationContext if Spring WebFlux is on the classpath or a regular ApplicationContext otherwise.

NONE

Creates an ApplicationContext and sets SpringApplication.setWebApplicationType(WebApplicationType) to WebApplicationType.NONE.

RANDOM_PORT

Creates a web application context (reactive or servlet based) and sets a server.port=0 Environment property (which usually triggers listening on a random port).