Questions tagged [spring-boot-test]

Spring Boot provides utilities and annotations to help when testing your application.

Spring Boot provides a @SpringBootTest annotation which can be used as an alternative to the standard spring-test @ContextConfiguration annotation when you need Spring Boot features. The annotation works by creating the ApplicationContext used in your tests via SpringApplication.

Utilities: ConfigFileApplicationContextInitializer,EnvironmentTestUtils,OutputCapture and TestRestTemplate

for example

EnvironmentTestUtils.addEnvironment(env, "org=Spring", "name=Boot");

Docs: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html

1399 questions
81
votes
3 answers

SpringRunner vs SpringBootTest

In unit test, what are the differences between @Runwith(SpringRunner.class) & @SpringBootTest? Can you explain to me the use cases of each one?
zouari
  • 967
  • 1
  • 6
  • 13
40
votes
6 answers

How to reuse Testcontainers between multiple SpringBootTests?

I'm using TestContainers with Spring Boot to run unit tests for repositories like this: @Testcontainers @ExtendWith(SpringExtension.class) @ActiveProfiles("itest") @SpringBootTest(classes =…
Martin Schröder
  • 4,176
  • 7
  • 47
  • 81
38
votes
3 answers

Difference between junit-vintage-engine and junit-jupiter-engine?

It's a two-fold question. What is the difference between junit-vintage-engine and junit-jupiter-engine? SpringBoot starter projects come with an exclusion for junit-vintage-engine. Is it to enforce the use of junit-jupiter-engine? Below is the…
Sandeep Kumar
  • 2,397
  • 5
  • 30
  • 37
38
votes
5 answers

SpringBootTest : No qualifying bean of type 'org.springframework.test.web.servlet.MockMvc' available:

Hey i have started learing spring-boot junit testing using spring boot Test framework at the time of creating the test case i am facing issues below . import static org.hamcrest.Matchers.containsString; import static…
Prabhat Yadav
  • 1,181
  • 6
  • 18
  • 29
38
votes
1 answer

What's the difference between @AutoConfigureWebMvc and @AutoConfigureMockMvc?

In which case should I use each one?
Ekaterina
  • 1,642
  • 3
  • 19
  • 36
33
votes
4 answers

/actuator/prometheus missing in @SpringbootTest

I'm using springbooot 2.4.0 and I added the following dependencies for enabling prometheus metrics: org.springframework.boot
31
votes
3 answers

SpringApplicationConfiguration not found: Erroneous spring-boot-starter-test content?

Getting a compilation error in Maven: [INFO] ------------------------------------------------------------- [ERROR] COMPILATION ERROR : [INFO] ------------------------------------------------------------- [ERROR]…
Simeon Leyzerzon
  • 18,658
  • 9
  • 54
  • 82
30
votes
9 answers

JUnit's @TestMethodOrder annotation not working

I'm having a problem with following integration test import org.junit.jupiter.api.Order; import org.junit.jupiter.api.TestMethodOrder; import…
toucheqt
  • 725
  • 2
  • 8
  • 15
28
votes
9 answers

Unable to start ReactiveWebApplicationContext due to missing ReactiveWebServerFactory bean

I have a new springboot application I am attempting to get started. The error I receive is org.springframework.context.ApplicationContextException: Unable to start reactive web server; nested exception is…
Daniel Lynch
  • 313
  • 1
  • 4
  • 8
27
votes
2 answers

Testing a @KafkaListener using Spring Embedded Kafka

I am trying to write a unit test for a Kafka listener that I am developing using Spring Boot 2.x. Being a unit test, I don't want to start up a full Kafka server an instance of Zookeeper. So, I decided to use Spring Embedded Kafka. The definition of…
24
votes
2 answers

What is the use of contextLoads method in Spring Boot Junit Testcases?

This method is empty in all my JUnit test cases. What is the use of this method? Sonarqube is complaining "Add a nested comment explaining why this method is empty, throw an UnsupportedOperationException or complete the implementation." I can…
Thiagarajan Ramanathan
  • 1,035
  • 5
  • 24
  • 32
23
votes
3 answers

Resolving port already in use in a Spring boot test DEFINED PORT

I have a spring boot application that fires up and executes a class that listens to Application Ready event to call an external service to fetch some data and then use that data to push some rules to the classpath for execution. For local testing we…
ahjashish
  • 573
  • 1
  • 3
  • 13
22
votes
3 answers

What's the difference between MockMvc, RestAssured, and TestRestTemplate?

For all I know, MockMvc is just testing the Controller, and mocking the Service layer. Whilst RestAssured and TestRestTemplate are testing the running instance of our API. Is that correct? And what's the difference between RestAssured and Spring…
Silly Sally
  • 1,027
  • 2
  • 10
  • 15
22
votes
3 answers

Spring Boot Unit Test a module not detecting an autowired component

we have split our Maven based Spring Boot project into two modules as…
Carmageddon
  • 2,627
  • 4
  • 36
  • 56
21
votes
9 answers

Set property with wiremock random port in spring boot test

I have a Spring Boot test that uses wiremock to mock an external service. In order to avoid conflicts with parallel builds I don't want to set a fixed port number for wiremock and would like to rely on its dynamic port configuration. The application…
1
2 3
93 94