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:

1677 questions
110
votes
18 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
  • 22,313
  • 10
  • 78
  • 95
100
votes
7 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…
Hans-Peter Störr
  • 22,852
  • 27
  • 96
  • 134
92
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
  • 6,560
  • 4
  • 41
  • 49
92
votes
7 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
  • 17,297
  • 13
  • 46
  • 85
87
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,581
  • 5
  • 20
  • 28
77
votes
11 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
  • 37,353
  • 77
  • 251
  • 478
67
votes
4 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
  • 66,525
  • 139
  • 452
  • 886
66
votes
8 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
  • 2,786
  • 2
  • 19
  • 32
64
votes
4 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
  • 11,563
  • 18
  • 58
  • 76
58
votes
11 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
  • 874
  • 1
  • 9
  • 15
56
votes
4 answers

java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling

I have the following test class: @ActiveProfiles({ "DataTC", "test" }) @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {BaseTestConfiguration.class, DataTestConfiguration.class, JpaConfiguration.class,…
balteo
  • 20,469
  • 52
  • 196
  • 362
55
votes
10 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,126
  • 1
  • 26
  • 23
54
votes
3 answers

Override a single @Configuration class on every spring boot @Test

On my spring boot application I want to override just one of my @Configuration classes with a test configuration (in particular my @EnableAuthorizationServer @Configuration class), on all of my tests. So far after an overview of spring boot testing…
NotGaeL
  • 7,842
  • 3
  • 35
  • 64
53
votes
7 answers

How to unit test a Spring MVC controller using @PathVariable?

I have a simple annotated controller similar to this one: @Controller public class MyController { @RequestMapping("/{id}.html") public String doSomething(@PathVariable String id, Model model) { // do something return "view"; } } and I…
martiner
  • 677
  • 1
  • 6
  • 10
52
votes
13 answers

Spring beans redefinition in unit test environment

We are using Spring for my application purposes, and Spring Testing framework for unit tests. We have a small problem though: the application code loads a Spring application context from a list of locations (XML files) in the classpath. But when we…
Stas
  • 1,019
  • 2
  • 15
  • 25
1
2 3
99 100