Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Reactive Test case fails to start the Netty Server

I have spring test-case as shown below when I run it is not starting the Netty server and provides following exception.

Caused by: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:155)

Below is my test case:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringWebFluxDemoApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class CustomPriceControllerTest {

    @Autowired
    private ApplicationContext context;

    private WebTestClient testClient;

    @Before
    public void init() {
        testClient = WebTestClient.bindToApplicationContext(context).configureClient().baseUrl("http://localhost:8080").responseTimeout(Duration.ofSeconds(30)).build();
    }

    @Test
    public void broadcastVoltageConsumption() {

        this.testClient.get().uri("/api/getCustomPrice")
                .accept(MediaType.APPLICATION_STREAM_JSON)
                .exchange()
                .expectBodyList(Custom.class)
                .consumeWith((customResults) -> {
                    List<Custom> list = CustomResults.getResponseBody();
                    Assert.assertTrue(list != null && list.size() > 0);
                });
    }
}

My pom.xml has excluded the dependency for tomcat to enable Netty. My Spring boot class works perfectly fine. It boots Netty server.

Update - pom.xml

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
            <exclusions>
                <!-- Exclude the Tomcat dependency -->
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

Had to add javax.servlet-api because I was facing issues with javax.servlet-api missing.

Update - 2 Removing the javax.servlet dependency from pom.xml solves the issue.

While when I try to run my main application it starts the Netty server normally. What is missing in this configuration? Can anyone help on this?

like image 436
Bilbo Baggins Avatar asked May 25 '18 17:05

Bilbo Baggins


People also ask

Does spring boot support Netty?

If we're using WebFlux in a Spring Boot application, Spring Boot automatically configures Reactor Netty as the default server. In addition to that, we can explicitly add Reactor Netty to our project, and Spring Boot should again automatically configure it.

Does spring WebFlux use Netty?

For reactive stack applications, the spring-boot-starter-webflux includes Reactor Netty by including spring-boot-starter-reactor-netty , but you can use spring-boot-starter-tomcat , spring-boot-starter-jetty , or spring-boot-starter-undertow instead.

Is Netty reactive?

Say Hello to WebFlux on NettyIt supports reactive programming with its event-driven, asynchronous, and non-blocking approach to request handling. It also provides many functional APIs.

What is spring Netty?

Definition of Spring Boot Netty. It is an event-driven application framework that was used in-network, it will provide the HTTP, UDP, and non-blocking server and client. As per the name spring boot, netty is based on the netty framework, it is also known as the non-blocking input and output (NIO) framework.


1 Answers

You wanted the webserver to be powered by Netty, but the exception you are getting is Servlet Specific

Since Netty is not servlet based technology, it seems you that somewhere (gradle/maven/@Configuration) you are mixing them, so , just remove all references to Servlet dependencies and re try

like image 87
vamsi vegi Avatar answered Sep 26 '22 01:09

vamsi vegi