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

Spring boot test H2 sql script not being loaded

So i'm having problems trying to make my test loading the .sql script for schema and data. By default my scripts are not being loaded, cause when my test try to get the data from the H2 db, show the message "Table "" not found". The scripts are in…
Stranger
  • 73
  • 6
4
votes
2 answers

How to set up different responses for the same request to MockServer?

i'm having an issue when setting up the MockServerClient for multiple responses with the exact same request. I read that with expectations with "Times" this might be done, but i coulnd't make it work with my scenario. If you call the service with…
Gabriel Vitale
  • 163
  • 1
  • 3
  • 10
4
votes
1 answer

Reusing expensive beans in Spring Boot Tests

I am trying to improve performance of medium tests in Spring Boot. I am using the Spring Boot - testcontainers library. For an individual test this works really well, with a few annotations I can get access to kafka, zookeeper, and schema-registry.…
Michael
  • 3,498
  • 5
  • 27
  • 32
4
votes
1 answer

How can I use MockWebServer for Junit test cases of webclient?

I have a spring-boot application which calls some third party URL (let's say http://example.com/someUri) using webclient(I have used application-dev.properties for injecting this url in my application to achieve loose coupling) and consumes the…
4
votes
0 answers

How I can clean Embedded Mongodb before each test class

I am writing tests for my application by class and have order method to do a specific scenario for each class, if I test each class separately it's work perfect but when I run mvn verfiy only the first-class success and the others filled because all…
Mohamd Ali
  • 2,146
  • 4
  • 23
  • 30
4
votes
1 answer

How to disable Spring Security on Tests for Webflux Functional Endpoints

I would like to disable security on endpoints during some unit tests. I am using webflux functional endpoints so the below configuration does not work. @AutoConfigureMockMvc(secure = false) I desactivated it by using profiles, but that means I…
Özgün
  • 318
  • 3
  • 11
4
votes
3 answers

"No converter found for return value" when running Spring Boot MockMvc test

I'm trying to create a MockMvc test of a Spring Boot controller. I specifically do not want the entire application context to be spun up, so I am restricting the context to the controller in question. However, the test fails with a 500 with the…
user2615350
  • 263
  • 2
  • 13
4
votes
1 answer

Insert data in h2 database through data.sql file before performing unit testing in spring boot

I want to perform unit testing in spring boot+ JPA. For that I created configuration file to create bean for dataSource, all hibernate properties, entityManagerFactory and transactionManager. Everything going perfect. Tables are getting created by…
Omkar Patil
  • 203
  • 1
  • 3
  • 10
4
votes
2 answers

@SpyBean with few Integration tests doesn't work correctly

I have a strange behavior of @SpyBean field in my integration tests. For example, I have a few integration tests: package a; @SpringBootTest public class A { @SpyBean public MySpyBeanCandidate spyBean; @Test public void test1 { // some…
4
votes
2 answers

Spring Boot Unit Test @Value from .properties File gives NullPointerException

I am trying to read a value from a properties file for a unit test case in Spring Boot. I have two config.properties files, one in src/main/resources: prop = some-value and one in src/test/resources: prop = some-test-value Main Application…
shinvu
  • 601
  • 2
  • 7
  • 23
4
votes
3 answers

Merge multiple @TestExecutionListeners from different declarations?

For the integration tests of my Spring Boot app, I have declared custom meta-annotations (a bit like Spring Boot's test slice annotations). How can I declare different TestExecutionListeners in each meta-annotation, and have all of them merged when…
jhyot
  • 3,733
  • 1
  • 27
  • 44
4
votes
0 answers

integration test with SpringBoot with both MockMvc and PowerMock on static methods

Can someone provide any sample on this scenario combination. I gave a sample snippet on the lines below, which is failing while trying to mock, intent is to combine both MockMVC with PowerMock Create a spring Boot Test App for integration testing…
4
votes
1 answer

H2 SQL Syntax Error `expected "., COMMENT, ("` using Flyway w/ Spring Boot Test

I am using Spring Boot 2 with Data JPA and Flyway on a Postgres database. Everything works fine in production. Now I'm trying to write tests that will run on an H2 Embedded Database for testing purposes. But, the tests encounter a SQL syntax error.…
Adam
  • 43,763
  • 16
  • 104
  • 144
4
votes
1 answer

Attribute ServerContainer not found in ServletContext

In my Spring boot application i use also websockets. Everything works fine, as expected in production. Now i started to create UnitTests with Spring-Boot-Test. Every time i start a @SpringBootTest , i get following exception…
4
votes
2 answers

Create a librairie for Springboot using gradle without doing bootJar

I developed a controllerAdvice that I want to put it in a librairie that will be used by several microservices; Since my library is using spring annotations I m adding plugins and dependencies for springboot. However when I add apply plugin:…
Mohamed Taboubi
  • 6,663
  • 11
  • 55
  • 87