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
5
votes
2 answers

@Value properties are always null in JUnit test

I'm writing a few unit tests for a Spring application that can be run with two different configurations. The two different configurations are given by two application.properties file. I need to write tests twice for each class, since I need to…
5
votes
0 answers

Spring Boot test: @Scheduled beans continue to execute after dirty context

Context I'm working on a Spring Boot project that makes heavy use of a @Scheduled bean. We have written a very light event-sourcing-like framework that relies on the data in the database: every x time the database is checked for new records, which…
Stijn Hooft
  • 170
  • 1
  • 12
5
votes
3 answers

@MockBean is returning null object

I am trying to use @MockBean; java version 11, Spring Framework Version (5.3.8), Spring Boot Version(2.5.1) and Junit Jupiter (5.7.2) . @SpringBootTest public class PostEventHandlerTest { @MockBean private AttachmentService…
bhavya
  • 51
  • 1
  • 4
5
votes
3 answers

How to create reusable @MockBean definitions in @SpringBootTest?

I have a @SpringBootTest class that has a rather complex mock definition setup with mocked return values. Question: can I externalize @MockBean setups into an own class, so I could reuse the mock configuration in multiple class (sidenote: I'm not…
membersound
  • 81,582
  • 193
  • 585
  • 1,120
5
votes
0 answers

Mockito does not reset when using @Nested class in Spring boot tests

After reading the JUnit doc again, I found it is good to use a @Nested inner class to group tests and finally display them in a tree structure in the test reports. But when refactoring my PostController like this. @WebFluxTest( controllers =…
Hantsy
  • 8,006
  • 7
  • 64
  • 109
5
votes
2 answers

How to init a default trace context with span in controller when using SpringBootTest

I'm making a move from Spring Boot 1.5.21 to 2.2.5, and in the process also moving from spring-boot-cloud version Edgware.SR6 to Hoxton.SR3. This move forced me to ditch the sleuth's own implementation of tracer/span model and embrace the new model…
5
votes
1 answer

Spring Boot Test @ConfigurationProperties does not bind fields

Good evening everyone. I have a class that is designed for binding with properties. In the class labeled @Configuration, I make this bean object like: // Into TargetConfiguration…
Mister_Jesus
  • 194
  • 1
  • 12
5
votes
2 answers

Excluding Configuration class from SpringBootTest

I have a class to configure Kafka under src/main/java: @Configuration public class SenderConfig { @Value("${spring.kafka.producer.bootstrap-servers}") private String bootstrapServers; @SuppressWarnings({ "unchecked", "rawtypes" }) …
Aldo Inácio da Silva
  • 824
  • 2
  • 14
  • 38
5
votes
1 answer

Spring Boot test does not start context or load dependencies

A very beginner problem but one I can't get past. I have a basic Spring Boot app, and one Spring Data MongoDB repository that connects to the cloud atlas instance. The problem is that in my Spring Boot test, my repository is not autowired and the…
2dor
  • 851
  • 3
  • 15
  • 35
5
votes
2 answers

Spring Boot OpenFeign random port test

I have an OpenFeign client set up like this: @FeignClient(name = "myService", qualifier = "myServiceClient", url = "${myservice.url}") public interface MyServiceClient { ... } and a Spring Boot test set up like this: @SpringBootTest(webEnvironment…
Lukasz G.
  • 119
  • 1
  • 10
5
votes
1 answer

Migrating schemas in docker testcontainers environment using Flyway and Spring boot

I'm trying to set up testing environment with testcontainers and flyway in spring boot application. All this supposed to run via DinD scheme. Current test example as follows: import com.testapp.testapp.entity.TestEntity import…
im_infamous
  • 972
  • 1
  • 17
  • 29
5
votes
2 answers

@WebMvcTest, @DataJpaTest I wonder if it's "Unit Tests" or "Integration Tests."

I am not good at English. Please understand. I knew. The use of @SpringBootTest() Annotation to load the entire Bean was clearly thought of as an "Integration Test." However, the use of @SpringBootTest(classes=mybean.class) was thought to be "Unit…
sieunkim
  • 381
  • 1
  • 3
  • 6
5
votes
1 answer

SpringBootTest - how to assert if context loading fails

I wrote an ApplicationListener that should check if the environment is prepared during context initialization. I'm having trouble testing the scenario since I'm adding the listener manually both in my configure() and main()…
5
votes
2 answers

How to use @TestConfiguration for avoiding src/test/java scanning?

According to 25.3.3. Excluding Test Configuration this capability exists: When placed on a top-level class, @TestConfiguration indicates that classes in src/test/java should not be picked up by scanning I created both @Configuration…
Adrian
  • 3,321
  • 2
  • 29
  • 46
5
votes
2 answers

SpringBootTest - how to assert context not loads and change properties at test level?

I rely on @SpringBootTest heavily when testing application configuration. App properties can be complex, having default values and non-trivial validations. For example: prop: ports: 1205,2303,4039 fqdn:…
Adam Hughes
  • 14,601
  • 12
  • 83
  • 122