Questions tagged [spring-test]

`spring-test` is the testing module of the Spring Framework, providing support for unit and integration testing with JUnit and TestNG, including various mocks for unit testing and the Spring TestContext Framework & the Spring MVC Test Framework for integration testing Spring-based applications.

spring-test is the testing module of the core Spring Framework, providing support for and and based applications with and , including mocks for things like the and APIs, the Spring TestContext Framework, and the Spring MVC Test Framework.

Online Resources:

Related Tags:

2041 questions
8
votes
1 answer

How to override/replace @MockBean field declared in the parent class with @Autowired real bean

To avoid the Spring context reloading again and again, I have moved @MockBean annotated injection to a parent class, something like this. @SpringBootTest ....... public abstract BaseTest { @MockBean protected OneService oneService; This…
user1619397
  • 680
  • 2
  • 11
  • 23
8
votes
4 answers

How to disable crsf protection in @WebFluxTest?

Why am I receiving a 403 FORBIDDEN for the following test? @RestController public class MyServlet { @PostMapping("/") public Mono accept(Authentication authentication) {} } @WebFluxTest(MyServlet.class) @WithMockUser public class…
membersound
  • 81,582
  • 193
  • 585
  • 1,120
8
votes
2 answers

SpringBootTest and MockMvc creating an enormous amount of console logging - how to disable?

We're using @SpringBootTest with an @Autowired private MockMvc mockMvc attribute to mock HTTP requests to our controller classes. For some reason that we can't seem to determine, this is creating enormous amounts of logging for each test case, and…
Craig Otis
  • 31,257
  • 32
  • 136
  • 234
8
votes
1 answer

How to override Spring Bean in integration test with custom bean definition?

I would like to reuse Spring production context configuration, but replace a few beans with another ones. If I would like to override them with a mock, I would use @MockBean, which does exactly what I need (overrides bean), but does not allow me to…
sinedsem
  • 5,413
  • 7
  • 29
  • 46
8
votes
4 answers

How to retrieve data directly from Spring test MvcResult json response?

I want to retrieve a value from json response in order to use in the rest of my test case, here's what I'm doing now: MvcResult mvcResult = super.mockMvc.perform(get("url").accept(MediaType.APPLICATION_JSON).headers(basicAuthHeaders())) …
Mohamed Ibrahim Elsayed
  • 2,734
  • 3
  • 23
  • 43
8
votes
5 answers

WebTestClient check that jsonPath contains sub string

In MockMvc there is an ability to assert that jsonPath contains substing .andExpect(jsonPath("$.error.message") .value(containsString("message"))) I wonder if there a nice way to do the same for WebTestClient, the syntax is a bit…
Evgen
  • 1,278
  • 3
  • 13
  • 25
8
votes
3 answers

How to get ResponseBody of @RestController as object in a MockMvc junit test?

I have a simple junit test that verifies the response of a servlet endpoint. Problem: I want to obtain the response as java object Person, and not as string/json/xml representation. Is that possible? @RestController public class PersonController { …
membersound
  • 81,582
  • 193
  • 585
  • 1,120
8
votes
2 answers

JDK 9: JUnit 5 test compile with SpringExtension produces java.lang.NoClassDefFoundError: org/w3c/dom/ls/DocumentLS

I believe this problem not to be related to module exclusions in JDK 9 (as with java.se.ee), but rather with the fact that JDK 9 includes a newer version of org.w3c.dom.ls in the java.xml module that does not have the DocumentLS class. The…
8
votes
2 answers

Spring Boot - Test Cases - Dont Load All Components

I am trying to to rest my rest classes in Spring MVC If I run the following code (ran fine when the project was small but now fails) it tries to load all the different components in my application. This includes beans which interact with external…
Damien Gallagher
  • 535
  • 1
  • 6
  • 16
8
votes
3 answers

Test the SpringBoot application startup

I have a JUnit test that starts an spring-boot application (in my case, the main class is SpringTestDemoApp) after the test: @WebIntegrationTest @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes =…
Boni García
  • 4,618
  • 5
  • 28
  • 44
8
votes
1 answer

How to use nested tests with spring-test-junit5?

I've been playing with JUnit 5 and spring-test-junit5. I then tried to use the nested tests feature, but my tests fail. This also happens when I run directly from the command line using…
Christo
  • 1,802
  • 4
  • 20
  • 31
8
votes
1 answer

spring integration test fail to load context "Another resource already exists with name dataSource"

I am using test annotation introduced in spring-boot 1.4.3 for my integration tests @RunWith(SpringRunner.class) @SpringBootTest public class MyServiceIT { } According to documentation, test context is cached and reused to speed up integration…
ltfishie
  • 2,917
  • 6
  • 41
  • 68
8
votes
4 answers

Spring Batch - How to prevent database commits with jobLauncherTestUtils

I have Spring Batch job that writes to the database (it has a step with a JpaItemWriter). I have an integration test such as follows: @RunWith(SpringRunner.class) @SpringBootTest @ActiveProfiles("integrationTest") public class LoadApplicationTests…
James
  • 2,876
  • 18
  • 72
  • 116
8
votes
1 answer

Spring boot test configuration not being picked

I am writing an integration test for my application, and want to use a custom webmvc configuration for my tests I have three classes in my base package com.marco.nutri: Application(which is annotated with…
Marco Prado
  • 1,208
  • 1
  • 12
  • 23
8
votes
2 answers

why properties from appliction.properties not available in Junit test in spring boot application?

I have a spring boot application. I am writing Junit tests. I am trying to inject values from application.properties (defined in src/main/resources/application.properties) and a Status bean configured in AppConfig…
brain storm
  • 30,124
  • 69
  • 225
  • 393