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

Spring Boot: Disable security for Spring Boot Unit Test

Spring Boot version: 2.0.4.RELEASE For the Spring Boot Test below, the test returns an unwanted 401 response: "401" status, "error": "unauthorized" What is the best way to disable Spring Security for the tests? I tried a adding a configuration…
8
votes
1 answer

Testing Log Output Java Spring

I have a spring boot 2 application running on java 10 using SLF4J and logback as the underlying logger. Given the following spring component: import org.slf4j.Logger; import org.slf4j.LoggerFactory; import…
David
  • 7,652
  • 21
  • 60
  • 98
8
votes
2 answers

Exception using SpringRunner with PowermockRunner

I am trying to test a JavaMail api and using SpringRunner and PowerMockRunner but it is failing. @RunWith(PowerMockRunner.class) @PowerMockRunnerDelegate(SpringRunner.class) @PowerMockIgnore(value = {"javax.management.*"}) @SpringBootTest public…
Abhishek Kumar
  • 3,328
  • 2
  • 18
  • 31
8
votes
2 answers

Adding custom header using Spring Kafka

I am planning to use the Spring Kafka client to consume and produce messages from a kafka setup in a Spring Boot application. I see support for custom headers in Kafka 0.11 as detailed here. While it is available for native Kafka producers and…
Annu
  • 145
  • 1
  • 3
  • 10
8
votes
5 answers

package org.springframework.boot.test.context does not exist

I am using dependencies as below
Remigius
  • 107
  • 1
  • 1
  • 6
8
votes
2 answers

SpringBootTest with MockMvcBuilders stand alone setup is not loading my ControllerAdvice despite setting it

I am creating my controller and controller advice like this: Test class: @RunWith(SpringRunner.class) @SpringBootTest public class TestController { private MockMvc mockMvc; @Mock private MyService myService; @Autowired …
Don Rhummy
  • 24,730
  • 42
  • 175
  • 330
8
votes
0 answers

Configuring custom Json Serializer for Spring controller test

I'm testing a controller: @RestController() public class MessagesController { ... } Using @WebMvcTest annotation: @RunWith(SpringRunner.class) @WebMvcTest(value = {MessagesController.class}) public class MessagesControllerTest { private…
ElArbi
  • 1,145
  • 3
  • 13
  • 22
8
votes
1 answer

Unit Test with spring-boot-starter-test and cassandra

My spring boot web application uses Cassandra DB via the Datastax client and the connection occurs as follow: public CassandraManager(@Autowired CassandraConfig cassandraConfig) { config = cassandraConfig; cluster = Cluster.builder() …
louis amoros
  • 2,418
  • 3
  • 19
  • 40
8
votes
1 answer

How to prevent spring boot from auto creating instance of bean 'entityManagerFactory' at startup?

I am working on a Spring boot application that uses Spring JPA with PostgreSQL. I am using @SpringBootTest(classes = .Application.class) to initialize my unit test for a controller class. The problem is that this is causing the…
rmulay
  • 135
  • 1
  • 3
  • 15
7
votes
4 answers

How to pass multiple parameters to @ValueSource

I'm trying to perform a parametrized JUnit 5 test, how to accomplish the following? @ParametrizedTest @ValueSource //here it seems only one parameter is supported public void myTest(String parameter, String expectedOutput) I know I could use…
Phate
  • 6,066
  • 15
  • 73
  • 138
7
votes
2 answers

Spring Boot Aspectj test with MockMvc

I have a Spring boot code with Aspectj. This code has written with basic MVC architecture. Then I just try to test it with MockMVC. But when I try to test it, Aspectj doesn't interrupted. Is there a special configuration about…
Sha
  • 921
  • 17
  • 46
7
votes
2 answers

WireMock behaves weird sometime

In most of the integration tests I'm using spring-boot-test(2.1.9.RELEASE) and spring-cloud-contract-wiremock(2.0.2.RELEASE). The test is starting up WireMock server based on : @AutoConfigureWireMock(port = 0), so I'm not using any WireMockRule or…
brebDev
  • 774
  • 10
  • 27
7
votes
1 answer

Spring Cloud Contract - is it Consumer Driven?

I'm just starting with Consumer-Driven Contracts for our microservice setup, and as they are mainly Spring Boot apps natural choice is Spring Cloud Contract. But then comes the confusion. In all examples in docs contracts are defined on the producer…
Vuk Djapic
  • 816
  • 13
  • 29
7
votes
2 answers

SpringbootTest + TestContainers: how do I refresh the database after tests pollute the database

I am using an abstract class like this: @SpringBootTest(classes = MyAppApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT) @ActiveProfiles("test") public abstract class AbstractIntegrationTest { static { PostgreSQLContainer…
user952342
  • 2,602
  • 7
  • 34
  • 54
7
votes
2 answers

MockHttpServletRequestBuilder - how to change remoteAddress of remoteHost of HttpServletRequest?

I'm trying to create mock request for integration test (@SpringBootTest). //given MockHttpServletRequestBuilder requestBuilder = get("/users/register/user1"); What I want to check is the remote of this request. In my controller Im getting…