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

Cannot inject @Service in Unit Test in SpringBoot project

i have a @Service that I am trying to mock in an Unit Test but i get a null value so far. In the application class I specify what are the scanBasePackages. Do I have to do this in a different way? Thanks. This is my service class that implements an…
Gabi Radu
  • 1,107
  • 2
  • 16
  • 35
5
votes
1 answer

execute springboot tests without loading application context and without mocking on a real test environment -- Is is possible

I have a question. Requesting your help. We have a spring boot application and I have written integration tests without Mocks, instead with TestRestTemplate and @SpringbootTest. So on local machine, when i execute the tests, they execute fine as i…
5
votes
1 answer

@Autowired inside SpringRunner SpringBootTest Unit Test Cases

Our Spring Boot REST API currently has a pretty large unit test repository. The unit tests refactor common reusable test code out into TestUtil classes which are @Component annotated. It appears that the SpringRunner unit test cases can only find…
Bernie Lenz
  • 1,967
  • 23
  • 45
5
votes
3 answers

How to fix the null value response of RestTemplate.exchange in a Mockito Test?

My Service class is below, followed by its test - @Service public class MyServiceImpl implements MyService { @Autowired private RestTemplate restTemplate; @Override public StudentInfo getStudentInfo(String…
Righto
  • 855
  • 3
  • 11
  • 32
5
votes
1 answer

How to use user defined database proxy in @DataJpaTest

We need to track database metrics so we are using datasource-proxy to track this to integrate the same in spring boot project we have created custom datasource as below @Component @Slf4j @ConfigurationProperties(prefix = "spring.datasource") public…
rajadilipkolli
  • 3,475
  • 2
  • 26
  • 49
5
votes
3 answers

Spring boot @SpyBean causing test suite to error probably due to issue with context not being reset

I have an issue with a spring integration test. The behavior: When I run the test below in isolation, it is in success. However, when all tests are run, many of them including the one below are in error. When I ignore the test below and run all…
balteo
  • 23,602
  • 63
  • 219
  • 412
5
votes
3 answers

@JdbcTest detect class annotated @SpringBootApplication

I've tried the @JdbcTest on following project structure. . ├── pom.xml └── src ├── main │   ├── java │   │   └── com │   │   └── example │   │   ├── Application.java │   │   ├── repository │   │   …
Kazuki Shimizu
  • 399
  • 2
  • 7
5
votes
1 answer

Problems with Testing Spring Boot Application with Multi Maven Project Setup

I am currently running into some problems with spring boot and multi maven project structure. I am using Spring Boot 4.3.1. My project structure looks as follows: parent -- pom.xml -- application -- pom.xml -- src -- main --…
user3278695
  • 113
  • 3
  • 10
4
votes
0 answers

Run code before SpringBootTest loads context in spring-boot 3

Let's consider the following test: @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class ExampleTest { static { System.out.println("hello from static"); } @BeforeAll static void…
szakal
  • 127
  • 1
  • 1
  • 6
4
votes
2 answers

Cucumber + JUnit5: No tests found for given includes

I'm using Cucumber and JUnit5 to write tests for my projects. My project uses Spring Framework and Gradle as a build tool and I use IntelliJ Idea as an editor which has Cucumber and Gradle plugins. Here is my Cucumber's runner: package…
4
votes
0 answers

TestContainer can't start due to error: Timed out waiting for log output matching

I got "ContainerLaunchException: Timed out waiting for log output matching" when starting testcontainer for elasticserach. How should I fix this issue? container = new ElasticsearchContainer(ELASTICSEARCH_IMAGE) …
4
votes
0 answers

How do I parameterize property values for spring boot test

I am working on an aggregation framework using spring boot, which can be used to aggregate multiple kinds of I/O operations' results. Additionally, it has support for both non-blocking (reactive) & blocking aggregation layer, which is controlled by…
aksh1618
  • 2,245
  • 18
  • 37
4
votes
1 answer

How to execute code in a SpringBootTest before the Application is run?

I have a SpringBoot based command line application. The application creates or deletes some records in a database. It does so not directly via JDBC but rather through a special API (instance variable dbService). The application class looks like…
fml2
  • 190
  • 11
4
votes
1 answer

I can't autowire Service class in Spring Boot Test

I created Dao Repository that uses jdbc for working with DB. I autowired this repository in my Service class. Then I try to autowire my service class in my test class. @SpringBootTest public class ServiceTest { @MockBean private Dao dao; …
4
votes
2 answers

Exception "org.h2.jdbc.JdbcSQLSyntaxErrorException:Table "NAME" not found " during initialization H2 database for testing Spring Boot application

I write integration tests annotated with @SpringBootTest. To run tests I insert data to the H2 database from the data.sql in test resources. I have a situation when the firstly tests run successfully, and after a number of times, I have an error,…