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 - web mvc test does not find controller mapping

In this spring-boot project the following WebMvcTest fails because the GET /items mapping from the ItemController is not found @RunWith(SpringRunner.class) @WebMvcTest(ItemController.class) @AutoConfigureMockMvc public class Test_ItemController { …
tenticon
  • 2,639
  • 4
  • 32
  • 76
4
votes
2 answers

Do not want to load application.yml when testing with Spring Boot

How can I tell Spring to load only application-test.yml and not application.yml file ? I have a config class: @ActiveProfiles("test") @SpringBootConfiguration public class MongoTestConfig { @Autowired MongoOperations operations; ... } And…
ElArbi
  • 1,145
  • 3
  • 13
  • 22
4
votes
1 answer

spring boot test loads schema.sql defined in java/resources instead of test/resources

I have the following Test: @RunWith(SpringRunner.class) @ContextConfiguration @SpringBootTest(classes = {AccountsServerTest.class, PostgresSQLTestDbConfig.class}) @ActiveProfiles("test") public class NodeRepositoryTest { ...... } and also I have…
Andrey Yaskulsky
  • 2,458
  • 9
  • 39
  • 81
4
votes
0 answers

GenericContainer of SpringBoot Application: Timed out waiting for container port to open

I would test a spring boot application with TestContainers but i don't understand the right way. This is my controller: @RestController @RequestMapping(value = "snip") public class Controller { @RequestMapping(method = RequestMethod.GET, path…
4
votes
0 answers

Using @DataJpaTest with multiple DataSource and custom repository implementation

I need your help, please. I'm trying to setup the following repository test: @RunWith(SpringRunner.class) @ActiveProfiles("test") @DataJpaTest public class ErrorClassRepositoryCustomImplTest { @Autowired private TestEntityManager…
Joe
  • 287
  • 3
  • 13
4
votes
2 answers

Exclude ApplicationStartup Event listener when testing

I recently added an ApplicationStartup class to my SpringBoot project @Component public class ApplicationStartup implements ApplicationListener { ... It implements ApplicationListener. Now when I run my old JUNit tests…
ed4becky
  • 1,488
  • 1
  • 17
  • 54
4
votes
3 answers

Running tests in Spring Boot fails in context

I'm writing a Spring Boot (1.5.7) REST application, which is working nice without problems. Now I'm trying to write some unit/integration tests but I can't figure how to configure them. This is my…
HelLViS69
  • 299
  • 1
  • 4
  • 15
4
votes
2 answers

How to set environment variable dynamically in spring test

I am trying to run spring-test cases with spring-boot. My test class looks like as follows @ContextConfiguration(initializers = TestContextInitializer.class) @RunWith(SpringRunner.class) @SpringBootTest(classes =…
Ashish
  • 14,295
  • 21
  • 82
  • 127
4
votes
2 answers

Failed to start spring boot integration test when setting the application type as reactive

I have an application using Spring Webflux via Spring boot 2.0.0.M3. The application is set as REACTIVE type when running it. public static void main(String[] args) { SpringApplication application = new…
Kane
  • 8,035
  • 7
  • 46
  • 75
4
votes
1 answer

How to configure HandlerMethodArgumentResolver in spring boot test

I am writing a unit for a controller with spting boot @WebMvcTest. Using @WebMvcTest, I will be able to inject a MockMvc object as given below :- @RunWith(SpringRunner.class) @ContextConfiguration(classes = {TestConfig.class}) @WebMvcTest class…
Manu
  • 3,467
  • 3
  • 29
  • 28
4
votes
1 answer

Unit Test Spring Integration flow DSL

I am trying to unit test a simple flow where it is checking for existence of a file and then do some additional task. IntegrationFlow @Bean public IntegrationFlow initiateAlarmAck() { return IntegrationFlows.from("processAckAlarmInputChannel") …
nkare
  • 397
  • 6
  • 17
4
votes
1 answer

@SpringBootTest and @Transactional causes test to freeze

When combining @SpringBootTest and @Transactional my test freezes and never recovers. I've built a demonstration of the problem here. getAgain fails because of a unique constraint violation caused by the @Before. To solve this, I would normally…
Corey
  • 1,133
  • 4
  • 17
  • 30
4
votes
1 answer

Sharing common Springboot integration tests with jar?

Can I have a jar containing Springboot integration tests & use this jar in other modules to execute the common integration tests?
Mohan Mahajan
  • 1,501
  • 3
  • 12
  • 19
4
votes
2 answers

@WithUserDetails does not seem to work

I have an application in which I use Spring Social Security for authentication and authorization. Unfortunately I am having some problems with mocking Spring Security. It seems that it does not work at all. I have a REST controller that returns 404…
3
votes
0 answers

Spring Boot 3 @SpringBootTest-annotated tests does not execute gateway auto-configuration

I have a very simple test: @SpringBootTest(properties = {"instance.role=GATEWAY", "instance.name=test-instance", "debug=true"}) @TestPropertySource(locations = {"classpath:application.yaml"}) class GatewayWiringTest { @Autowired private…
user625488
  • 336
  • 1
  • 13