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
6
votes
3 answers

How to use Testcontainers Cassandra with Spring Data for Apache Cassandra?

I develop integration tests for my spring boot application, which works with Cassandra. I use CassandraTemplate for communication with Cassandra. I have the following config for the database…
6
votes
2 answers

how to run springboot test without run tomcat?

I am developing a spring boot application and write some junit test. But I find when I run any tests, tomcat is also started up, It makes those tests very slow and waste many times. When I develop a SpringMvc application, junit test can run without…
Shuai Li
  • 2,426
  • 4
  • 24
  • 43
6
votes
3 answers

Junit5- jupiter all tests suite @BeforeAll @AfterAll not working

The before and after methods not working in JUnitPlatform. The code is below. import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.platform.runner.JUnitPlatform; import…
6
votes
2 answers

Spring boot test: @Sql annotation Unable to locate sql files placed in src/test/resources

I didn't want to load the entire Spring Boot configuration for unit-testing my DAO layer, and therefore created a nested configuration class to suppress default configurations. But when I try to specify SQL scripts for it to run before tests, its…
Daud
  • 7,429
  • 18
  • 68
  • 115
6
votes
1 answer

Appropriate usage of TestPropertyValues in Spring Boot Tests

I came across TestPropertyValues, which is briefly mentioned in the Spring Boot docs here:…
Ted M. Young
  • 1,052
  • 11
  • 24
6
votes
3 answers

Active profile in SpringBootTest based on system variable

As the host of Redis is different in local and CI, my @Tests can pass locally, they can't pass in CI. Firstly, I tried to mock the RedisTemplate like this: RedisTemplate redisTemplate = mock(RedisTemplate.class); ValueOperations valueOperations =…
6
votes
1 answer

@EnableAutoConfiguration on AbstractIntegrationTest possible?

Having lots of Integration-Test Implementations like this: // no @Annotations at all class SomeIntegrationTest extends AbstractIntegrationTest { ... } using (Spring Boot 1.5, JUnit 5) @SpringBootTest(classes = {CoreConfiguration.class,…
hotzen
  • 2,800
  • 1
  • 28
  • 42
6
votes
1 answer

@DataJpaTest how to specify repositories and entities to scan

By default @DataJpaTest scans all jpa repositories and @Entity. In my case I have 5 repositories package and 5 entities package. E.g com.acme.product.entity is associated with com.acme.product.repository com.acme.users.entity is associated with…
akuma8
  • 4,160
  • 5
  • 46
  • 82
6
votes
2 answers

How to replace a @MockBean?

Is it possible to replace an inherited @MockBean with the real @Bean? I have an abstract class that defines many configurations and a setup for all ITests. Only for one single test I want to make use of the real bean, and not used the mocked one.…
membersound
  • 81,582
  • 193
  • 585
  • 1,120
6
votes
7 answers

Spring Boot Test failing to autowire port with LocalServerPort annotation

I am running Spring Boot 2.0.1 and Junit 5. I am trying to get port within an integration test . However the port value is always zero. I am not sure what could be causing it. I have tried changing web environment enum to random port but nothing…
atkawa7
  • 461
  • 1
  • 6
  • 13
6
votes
2 answers

How to disable csrf checks for integration tests in springboot

I want to disable csrf when doing integration tests for my springboot application. I have already tried security.enable-csrf=false but that doesn't seem to have any effect. I have also tried passing csrf token in my tests via…
Divick
  • 1,213
  • 1
  • 20
  • 44
6
votes
5 answers

Mock Repository From Controller class using Spring Boot and JUnit

I am writing the test cases for my controller class which is a Spring Boot Application and I want to write the test cases only for controller class which invokes Service and Service to Repository. I am using SpringBootTest which is used for to…
6
votes
1 answer

@WebMvcTest giving 'Error creating bean with name' error for different service in spring boot test

I am trying to write test for my spring boot application. For the independent controller test, i have used @WebMvcTest but ran into some issues. Here is the basic structure of the code. UserController has UserService class…
Mcoder
  • 299
  • 2
  • 9
  • 33
6
votes
2 answers

How to disable Spring SQS handler in module/integration test

I have a spring boot application that does not much more than listening to an SQS queue via a component "MessageHandler" that has a @SqsListener-annotated method, and start some work when a message arrives. There is also a boot-starter-web…
Bernhard
  • 665
  • 1
  • 6
  • 22
6
votes
2 answers

SpringApplicationConfiguration cannot be resolved in a Spring Boot test

I've generated a Spring Boot web application using Spring Initializr, using embedded Tomcat + Thymeleaf template engine, and package as an executable JAR file. Technologies used : Spring Boot 1.4.2.RELEASE, Spring 4.3.4.RELEASE, Thymeleaf…
Nuñito Calzada
  • 4,394
  • 47
  • 174
  • 301