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
130
votes
11 answers

How to set environment variable or system property in spring tests?

I'd like to write some tests that check the XML Spring configuration of a deployed WAR. Unfortunately some beans require that some environment variables or system properties are set. How can I set an environment variable before the spring beans are…
Dr. Hans-Peter Störr
  • 25,298
  • 30
  • 102
  • 139
124
votes
11 answers

Overriding beans in Integration tests

For my Spring-Boot app I provide a RestTemplate though a @Configuration file so I can add sensible defaults(ex Timeouts). For my integration tests I would like to mock the RestTemplate as I dont want to connect to external services - I know what…
mvlupan
  • 3,536
  • 3
  • 22
  • 35
122
votes
19 answers

JUnit tests pass in Eclipse but fail in Maven Surefire

I have written some JUnit tests using JUnit 4 and spring-test libraries. When I run the tests inside Eclipse then run fine and pass. But when I run them using Maven (during the build process), they fail giving a spring related error. I am not sure…
Abhinav Sarkar
  • 23,534
  • 11
  • 81
  • 97
115
votes
8 answers

Rollback transaction after @Test

First of all, I've found a lot of threads on StackOverflow about this, but none of them really helped me, so sorry to ask possibly duplicate question. I'm running JUnit tests using spring-test, my code looks like…
Jan Vorcak
  • 19,261
  • 14
  • 54
  • 90
114
votes
10 answers

Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

I am using Spring Data JPA and Spring Boot. The layout of the application is this main +-- java +-- com/lapots/game/monolith +-- repository/relational +--RelationalPlayerRepository.java +-- web …
lapots
  • 12,553
  • 32
  • 121
  • 242
110
votes
12 answers

How to re-create database before each test in Spring?

My Spring-Boot-Mvc-Web application has the following database configuration in application.properties…
Dims
  • 47,675
  • 117
  • 331
  • 600
102
votes
2 answers

What is the difference between SpringJUnit4ClassRunner and SpringRunner

Whenever I see a blog post related to Spring testing I see either of these classes but do not understand the real difference: @RunWith(SpringRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
Humoyun Ahmad
  • 2,875
  • 4
  • 28
  • 46
97
votes
5 answers

Reuse spring application context across junit test classes

We've a bunch of JUnit test cases (Integration tests) and they are logically grouped into different test classes. We are able to load Spring application context once per test class and re-use it for all test cases in a JUnit test class as mentioned…
Ramesh
  • 3,841
  • 5
  • 22
  • 28
88
votes
15 answers

Disable security for unit tests with spring boot

I'm trying to create a simple spring boot web project with security. I can launch the application fine and the security is working fine. However, I have some components that I want to test without security (or test at all -- I cant get the test…
steve_ash
  • 1,184
  • 1
  • 10
  • 16
86
votes
9 answers

Spring Boot: @TestConfiguration Not Overriding Bean During Integration Test

I have a Bean defined in a class decorated with @Configuration: @Configuration public class MyBeanConfig { @Bean public String configPath() { return "../production/environment/path"; } } I have a class decorated with…
The Head Rush
  • 3,157
  • 2
  • 25
  • 45
72
votes
13 answers

Spring Boot properties in 'application.yml' not loading from JUnit Test

What am I doing wrong? I'm using this little standalone App which runs and finds my src/main/resources/config/application.yml. The same configuration doesn't work from JUnit, see…
aliopi
  • 3,682
  • 2
  • 29
  • 24
70
votes
15 answers

Mockito Exception - when() requires an argument which has to be a method call on a mock

I have a very simple test case that is using Mockito and Spring Test framework. When I do when(pcUserService.read("1")).thenReturn(pcUser); I get this exception. org.mockito.exceptions.misusing.MissingMethodInvocationException: when() requires…
jsf
  • 2,851
  • 9
  • 30
  • 33
67
votes
5 answers

How to run JUnit SpringJUnit4ClassRunner with Parametrized?

The following code is invalid due to duplicate @RunWith annotation: @RunWith(SpringJUnit4ClassRunner.class) @RunWith(Parameterized.class) @SpringApplicationConfiguration(classes = {ApplicationConfigTest.class}) public class ServiceTest { } But how…
membersound
  • 81,582
  • 193
  • 585
  • 1,120
67
votes
5 answers

How to access Spring context in jUnit tests annotated with @RunWith and @ContextConfiguration?

I have following test class @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"/services-test-config.xml"}) public class MySericeTest { @Autowired MyService service; ... } Is it possible to access…
Vladimir
  • 12,753
  • 19
  • 62
  • 77
62
votes
5 answers

Error: Unable to find @SpringBootConfiguration when doing @WebMvcTest for Spring Controller

I am testing my controller given below @Controller public class MasterController { @GetMapping("/") public String goLoginPage(){ return "index"; } } I am following this Spring documentation to test my controller. Now, I want to test my…
Meena Chaudhary
  • 9,909
  • 16
  • 60
  • 94
1
2 3
99 100