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

Integration tests on springboot with constructor injection pattern

I am trying to use Constructor Injection dependency pattern. I wonder what is the correct approach to inject JPA Repositories on Integration test classes: I have my source code: RepoClass @Repository public interface MyClassRepo extends…
Mayday
  • 4,680
  • 5
  • 24
  • 58
7
votes
2 answers

Kotlin - How to manage @BeforeClass static method in springBootTest

I want to have a @BeforeClass method in my springBootTest, which should be static and declared in "companion object". @RunWith(SpringRunner::class) @SpringBootTest @ActiveProfiles("test") open class MyTest { companion object { @Autowired …
Mhy
  • 187
  • 3
  • 10
7
votes
1 answer

SpringBootTest exclude packages from being scanned

I want to exclude some packages from being scanned when using spring @SpringBootTest in the same way it is done with @ComponentScan. Is there something like @SpringBootTest(excludeFilters =@ComponentScan.Filter( type = FilterType.REGEX, …
gkatzioura
  • 2,655
  • 2
  • 26
  • 39
7
votes
1 answer

No further requests expected while MockRestServiceServer was set to ExpectedCount.manyTimes()

I have following test class for my spring-integration application that passes sucessfully being launched alone @SpringBootTest(classes = {BackupTestDefinition.class}) @ActiveProfiles({"test", "dev"}) @RunWith(SpringRunner.class) public class…
Oleg K.
  • 137
  • 1
  • 1
  • 6
7
votes
1 answer

Avoiding Kafka Streams to start in tests using Spring Boot 1.5

I am facing a very annoying problem, during test process in my Spring Boot application. I have an application that uses Kafka Streams and declares them in a dedicated configuration file. @EnableKafka @EnableKafkaStreams @Configuration public class…
7
votes
2 answers

Spring boot and camel testing with @SpringBootTest

I have spring boot app, with spring boot of version 1.5.8 and there camel 2.20.1 Simple route : @Component public class MyRoute extends RouteBuilder { public static final String IN = "file://in"; public static final String OUT =…
bijesanu
  • 225
  • 1
  • 2
  • 12
7
votes
1 answer

Spring Boot integration tests failing when run after one another

I have a Spring Boot integration test and a Spring Boot web integration test. Both tests pass when run individually. However, when run as part of a suite, the test that is executed second always fails. Each test starts up (and tears down) my…
Blair Nangle
  • 1,221
  • 12
  • 18
7
votes
4 answers

How to set active profile programmatically for any environment?

I want to set active profile host dependent for any envrionment and cannot find an environment independent hook. Following factory will set the active profile before application context will…
7
votes
3 answers

@DataMongoTest is creating a null MongoTemplate

I have been trying to follow the instructions in the link below to try to add an automatically configured Embedded MongoDB Instance for Cucumber Integration testing. This is currently not working, since the I keep getting a null MongoTemplate. I…
user6201420
6
votes
0 answers

How to make Spring Boot Test under JUnit 5 resolve indented/hierarchical properties externalized on a YAML file

How to make Spring Boot Test under JUnit 5 resolve indented/hierarchical properties externalized on a YAML file? I would like to write tests to validate some logic that depends on Environment.getProperty(String…
6
votes
1 answer

MockBean and MyBatis Mapper not working together (as they did before Spring Boot 2.2.7)

I am using MyBatis Spring Boot Starter version 2.1.3. Ever since moving to Spring Boot 2.2.7 (I've also tried 2.2.8 and 2.3.1 with the same results), I've had a problem using the MockBean annotation to mock a MyBatis interface (e.g. DAO). Let's…
David Diehl
  • 225
  • 1
  • 3
  • 11
6
votes
2 answers

Spring boot with WebFlux always throw 403 status in tests

Great thx for viewing my question) I have some strange subject: my spring boot tests don't work. They start successfully but always throwing 403 HTTP status when making the requests to any controller I have some project with next…
6
votes
1 answer

Spring weirdly depends on class location

I have a SpringBootTest test that should rely on a separate class to setup an embedded Postgres and datasource. So the Repository configuration looks like this: package com.stream.repository.configuration @Configuration @ComponentScan(basePackages =…
munHunger
  • 2,572
  • 5
  • 34
  • 63
6
votes
1 answer

Disable database connections with @SpringBootTest

I have a SpringBoot application with hibernate. In my tests I would like to disable any sort of db connection and configuration (tests don't have access to db). How should I do it ? My test class is annotated with @SpringBootTest and has @Autowired…
HIT_girl
  • 785
  • 7
  • 23
6
votes
3 answers

How to disable `@EnableKafka` in Spring Boot tests?

I would like to run my integration tests but I don't know how to disable @EnableKafka. My app looks like that: @SpringBootApplication @EnableKafka public class MyApplication {
pixel
  • 24,905
  • 36
  • 149
  • 251