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
20
votes
3 answers

How to use these @DataMongoTest and @SpringBootTest together in integration test

I am trying to write integration test case for one of my rest application which uses mongodb internally to persist the data @DataMongoTest @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) public class MainControllerTest…
scoder
  • 2,451
  • 4
  • 30
  • 70
20
votes
1 answer

Spring boot test: context loaded for every test?

In my project we have a super class for all our tests. This is the signature of that class @RunWith(SpringRunner.class) @SpringBootTest(value = {"management.port=0"}, classes = Application.class, webEnvironment =…
Perimosh
  • 2,304
  • 3
  • 20
  • 38
19
votes
4 answers

Is there any special configuration to use SpringRunner with junit5?

My micro-service project based on spring-boot framework and all my unit test running with spring runner. @RunWith(SpringRunner.class) adding this annotations, imports the following library: import…
Ilan Miller
  • 323
  • 1
  • 3
  • 11
18
votes
1 answer

@SpringBootTest Vs @WebMvcTest & @DataJpaTest &service unit tests, what is the best?

I have a SpringBoot MVC application, and I want to cover it with tests. I have the controller, service and repository layers. What is the best practice to cover the application by tests? Why do people use @SpringBootTest while it seems it could be…
J.J. Beam
  • 2,612
  • 2
  • 26
  • 55
17
votes
3 answers

Difference between webEnvironment = RANDOM_PORT and webEnvironment = MOCK

I wrote spring boot integration test and it is working. Here is the test config: @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = RANDOM_PORT) @AutoConfigureMockMvc @Transactional public class SomeTest { @Autowired private MockMvc…
Spasoje Petronijević
  • 1,476
  • 3
  • 13
  • 27
17
votes
2 answers

Unit Test or Integration Test in Spring Boot

I looked various tutorial online related to testing in Spring Boot and got confused by the way the tests were referred. Some articles refer to controller tests that use @WebMvcTest annotation as Unit Test whereas some refer it as Integration Test.…
Nital
  • 5,784
  • 26
  • 103
  • 195
17
votes
1 answer

Junit5 with spring-boot 1.5

I have a spring-boot application which uses spring-boot version 1.5.9.RELEASE. To test this application I want to use junit-jupiter version 5.0.2. For simple service tests it works without any problems. But when it comes to testing rest endpoints, I…
tgr
  • 3,557
  • 4
  • 33
  • 63
16
votes
1 answer

Spring-boot application-test.properties

I am trying to unit test the spring-boot application using junit. I have placed the application-test.properties under src/test/resources. I have a ApplicationConfiguration Class which reads the application.properties. My test class looks like…
15
votes
5 answers

Mock Projection Result Spring Data JPA

I am using spring data jpa in my spring boot project. I am firing an JPQL query and using an projection to store the result of query. My Projection : public interface VeryBasicProjection { String getTitle(); String getUrl(); } My service…
Ankit Bansal
  • 2,162
  • 8
  • 42
  • 79
14
votes
1 answer

JsonPathResultMatchers cannot be applied to ResultMatcher

I'm trying to make a simple test using spring boot. mockMvc.perform(post("/user") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(userJohn))) .andExpect(jsonPath("$[0].username",…
user2295277
  • 209
  • 1
  • 4
  • 12
14
votes
2 answers

Spring Boot WebFlux test not finding MockMvc

Problem I'm trying to run a simple spring boot test and I'm getting errors that suggest it can't MockMvc at runtime. Documentation suggests I'm using the correct annotations and I created my pom.xml using start.spring.io. Not sure why its having…
13
votes
2 answers

How to fix error from embedded kafka that cannot find meta.properties

I am trying to do integration test for app that using kafka, kafka-streams and cassandra. But when I am trying to setUp test class, i've got 2 errors: ERROR [main] BrokerMetadataCheckpoint: Failed to read meta.properties file under dir ERROR [main]…
faceoff
  • 151
  • 3
  • 12
13
votes
5 answers

How to prevent logging noise during startup in @SpringBootTest?

@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = "logging.level.root=OFF") public class MyTest { @Test public void test() {} } As a result of…
membersound
  • 81,582
  • 193
  • 585
  • 1,120
13
votes
2 answers

How to test a Spring Boot handler Interceptor

we are trying to do an intergration test our interceptors in our spring boot application using spring boot version 1.4.0, but not sure how; here is our application setting @Configuration @EnableAutoConfiguration() @ComponentScan public class…
sthbuilder
  • 561
  • 1
  • 7
  • 22
12
votes
2 answers

How to disable logging (except errors) in spring junit tests?

I have a /src/test/resources/application.properties with a simple spring-boot project: spring.main.banner-mode=off logging.level.root=ERROR logging.level.org.springframework.*=ERROR Problem: during test runs, I still see the following output in…
membersound
  • 81,582
  • 193
  • 585
  • 1,120
1
2
3
93 94