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
10
votes
1 answer

@SpringBootTest with @Sql: order of script execution and context initialization

I have integration tests that are executed on top on inmemory database. The signature of each test looks more or less like this: @RunWith(SpringRunner.class) @SpringBootTest @Sql("/clean-data-in-all-tables.sql") public class SomeTest { @Test …
Sasha Shpota
  • 9,436
  • 14
  • 75
  • 148
10
votes
4 answers

@Value("${local.server.port}") not working in Spring boot 1.5

I am upgrading Spring Boot from 1.3 to 1.5. For upgrading to 1.5 I have replaced @SpringApplicationConfiguration(classes = TestConfig.class) @WebIntegrationTest with @SpringBootTest(classes = TestConfig.class) Also, I am using …
Meena Chaudhary
  • 9,909
  • 16
  • 60
  • 94
10
votes
7 answers

@SpringBootTest does not autowire JavaMailSender and throws error

What am I doing wrong here? My understanding is Spring should autowire JavaMailSender the way its autowiring EventRepository. Any guidance? application.properties and application-test.properties mail.host='smtp.gmail.com'…
T D
  • 1,080
  • 2
  • 13
  • 20
9
votes
2 answers

R2DBC unable to create connection with h2 protocol

I'm trying to leverage H2 for testing and have the following configuration in src/test/resources/application.yaml: spring: r2dbc: url: r2dbc:h2:file://testdb I have an empty test annotated with @SpringBootTest but when I run it I get the…
Jit B
  • 1,206
  • 14
  • 26
9
votes
0 answers

How to enable JPA auditing with SpringBootTest?

I want to write integration tests for my RestAPI endpoints, and I'm struggling with @EnableJpaAuditing. I want some of my entities to be audited by Spring, so I've created the following configuration class: @Configuration @EnableJpaAuditing public…
9
votes
3 answers

@MockBean and @Autowired of the same service in one test class

Is it possible to somehow have in the same test class @MockBean and @Autowired of the same service? In other words, I would like to have @MockBean service only for one test, while for others tests of the same class I need it as @Autowired.
Mikhail Geyer
  • 881
  • 2
  • 9
  • 27
9
votes
0 answers

How to shutdown application context between tests

I have 3 groups of tests: unit, integration, acceptance. Latter two groups launches ApplicationContext: minimal for "integration" and full for "acceptance". Both application contexts register queue subscriber. Application contexts are deregistered…
9
votes
1 answer

Spring boot testing of a rest client using @RestClientTest

I am using spring boot 1.5.8 and want to test my client: @Component public class RestClientBean implements RestClient { private Map restTemplates = new HashMap<>(); @Autowired public RestClientBean(RestTemplateBuilder…
radio
  • 897
  • 2
  • 10
  • 25
9
votes
2 answers

why is spring boot's DataJpaTest scanning @Component

Confident this hasn't been asked but reading through the Spring docs and testing utilities I found this annotation and thought I'd start using it. Reading through the fine print I read: Regular @Component beans will not be loaded into the…
Michael Sampson
  • 369
  • 1
  • 6
  • 19
9
votes
1 answer

Unsatisfied dependency during test

I have a spring boot 2.0.0 M2 application who run well. I use autowired on constructor @RequestMapping(value = "/rest") @RestController public class AddressRestController extends BaseController{ private final AddressService AddressService; …
robert trudel
  • 5,283
  • 17
  • 72
  • 124
9
votes
1 answer

Context hierarchy in Spring Boot based tests

My Spring Boot application is started like this: new SpringApplicationBuilder() .sources(ParentCtxConfig.class) .child(ChildFirstCtxConfig.class) .sibling(ChildSecondCtxConfig.class) .run(args); Config classes are annotated with…
8
votes
0 answers

Spring boot context load test hangs

We have a Spring Boot app written in Kotlin. The problem is it hangs during Maven clean test while running context load test below. Java: 14.0.1 Kotlin: 1.4.20 Spring boot:2.3.6.RELEASE Spring…
Vahid
  • 1,625
  • 1
  • 18
  • 33
8
votes
2 answers

Could not find sun.misc.Unsafe when I tested model using mockito

I tried to make a mock tests for testing my entity, but I can't run it with java 13 or 11 only java 8. When I used java 8, everything works perfectly and when I change to java 11 or 13 it gives init error I find some posts on the StackOverflow but…
ziang qiu
  • 81
  • 1
  • 3
8
votes
2 answers

How to test @ConfigurationProperties with ApplicationContextRunner from spring-boot-test?

I need to test my autoconfiguration classes that make use of @ConfigurationProperties beans. I'm making use of ApplicationContextRunner as documented in…
Guillaume Berche
  • 3,049
  • 2
  • 17
  • 18
8
votes
1 answer

Unable to load application.properties file @DataJpaTest annotation

i am trying to load the application.properties in spring boot project for testing . i am using @DataJpaAnnotation along with the my custom application.properties file. Here is my sample configuration looks like…
Madhu
  • 552
  • 2
  • 10
  • 23