r/SpringBoot • u/Tony_salinas04 • Dec 29 '25
Question How can I make the tests complete faster?
I'm creating an API in Spring Boot and I've noticed that the tests I'm creating are taking a very long time to complete, even though I'm not loading much context or even making calls to the endpoints yet. I suppose when I do, each call will take about a minute. What do you recommend?
6
Upvotes
5
u/WaferIndependent7601 Dec 29 '25
How should we know without any code? Or what is taking so long.
3
u/g00glen00b Dec 30 '25
One issue is that you're using the SpringBootTest annotation for all your tests, which basically spins up your application completely and restarts it whenever necessary. It's likely that since you have about 11 tests (including parameterized ones), you're doing the equivalent of restarting your Spring Boot application 11 times.
A possible solution to that problem is to work with test slices. For example, you say that you're "not loading much context", but you are in fact loading all the beans from all the autoconfigurations you have. So controllers, security, bean validation, mail sending, ... all those beans are initialized on startup. However, your tests only seem to focus on database integration so far, so why load those beans?
Instead of using the SpringBootTest annotation, you could use the DataJpaTest annotation. This will load the JPA/DataSource related beans and not much else. It does mean that you need to manually import your service classes, but it should improve your testing performance.
1
u/Tony_salinas04 Dec 30 '25
https://github.com/tonysalinas-futdev/JavaEcomercceAPI
1
u/disposepriority Dec 30 '25
There is definitely something wrong, but obviously, paste the code if you want help with the code
1
u/rieckpil Dec 30 '25
Consider taking a look at how Spring TestContext caching works: https://rieckpil.de/spring-boot-test-context-caching-the-complete-guide/
That can drastically reduce the time of your integration tests. I've written a small utility to introspect your tests for that performance improvement: https://github.com/PragmaTech-GmbH/spring-test-profiler
Apart from that, you can start parallelizing your tests and/or reuse containers with Testcontainers