Yomal
Yomal

Reputation: 361

Data JPA Test with reactive spring

I want to test the repository layer and I'm using spring webflux. My test class is as follows

@RunWith(SpringRunner.class)
@DataJpaTest
public class DataTester {
    @Autowired
    private MyRepository repository;

    @Test
    .....

}

Even though this would work in spring-mvc when using spring-weblux I get the following error.

Failed to load ApplicationContext
java.lang.IllegalStateException: Failed to load ApplicationContext
...

Caused by: org.springframework.context.ApplicationContextException: Unable to start ReactiveWebApplicationContext due to missing ReactiveWebServerFactory bean.

How to resolve this? If I am to start the whole application context with @SpringBootApplication it works. Any other options without using that?

Upvotes: 1

Views: 1775

Answers (2)

Yomal
Yomal

Reputation: 361

The reason for this was that in the application.properties the application type was set as reactive.

spring.main.web-application-type=reactive

This tries to auto configure a web server in this case a reactive web server. As @DataJpaTest does not provide a bean for that, this fails. This can be fixed in either two ways.

One is by Adding an application.properties file in the resources directory of the test package and setting the value as,sprig.main-web-application-type=none solves this issue.

Or we can simple pass a property value to the annotation as follows. @DataJpaTest(properties = "spring.main.web-application-type=none")

Upvotes: 6

Goro
Goro

Reputation: 546

If you are using Spring Boot 2+ then only @DataJpaTest is enough on test class. So your test class should be

@DataJpaTest
public class DataTester {
    @Autowired
    private MyRepository repository;

    @Test
    .....

}

Upvotes: 0

Related Questions