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
14
votes
2 answers

SpringRunner unable to detect configuration

I have a spring-boot application for which am trying to create unit testcases. Below is the code that I am trying to run, I don't have any configuration file that I have (used only annotations) so the main class that loads all the configuration is…
Raghuveer
  • 2,859
  • 7
  • 34
  • 66
14
votes
4 answers

@SpringBootTest + @BeforeAll

I have a small spring boot app with database and rabbitmq usages. So I would like to test with integration test (H2 + apache qpid). @ExtendWith(SpringExtension.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,…
Alex
  • 967
  • 4
  • 15
  • 38
14
votes
2 answers

Use different Spring test context configuration for different test methods

We have a Spring based JUnit test class which is utilizing an inner test context configuration class @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = ServiceTest.Config.class) public class ServiceTest { @Test public…
hammerfest
  • 2,203
  • 1
  • 20
  • 43
14
votes
2 answers

How to execute JUnit tests in parallel for a single test class in Gradle

We have a lot of integration tests that are using Spring. We'd like not to create separate JVM processes per tests (maxParallelForks option) or only run parallel builds in the multimodule project (--parallel). We would like for a single test class…
Marcin Grzejszczak
  • 10,624
  • 1
  • 16
  • 32
13
votes
7 answers

ClassNotFoundException on org.springframework.test.context.TestContextAnnotationUtils when upgrading Spring Boot to 2.4.2

In my POM I upgraded the version of Spring Boot from 2.3.5.RELEASE to 2.4.2. As a result, mvn clean test now fails with error java.lang.NoClassDefFoundError: org/springframework/test/context/TestContextAnnotationUtils Caused by:…
Ivan dal Bosco
  • 3,233
  • 4
  • 19
  • 24
13
votes
2 answers

Test configuration for Jpa auditing

I am trying to use Spring Data, Hibernate Envers and auditing in Spring Boot application. I have configured AuditorAwareImpl public class AuditorAwareImpl implements AuditorAware { @Override public Optional
ketrab321
  • 541
  • 2
  • 12
  • 22
13
votes
5 answers

Spring Boot Test MalformedURLException: unknown protocol: classpath

If java.net.URL is used in a Spring Boot application, with classpath protocol, it works as expected, because Spring Boot registers URLStreamHandlerFactory. e.g. new URL("classpath: someFile.whatever"). But when this code is executed as JUnit test…
Evgeni Dimitrov
  • 21,976
  • 33
  • 120
  • 145
13
votes
2 answers

How to test Spring @Conditional beans

I have a @Conditional bean - @RestController("/user") @ConditionalOnProperty(prefix = "user-controller", name = "enabled", havingValue = "true") public void UserController { @GetMapping public String greetings() { return "Hello User"; } } it…
idmitriev
  • 4,619
  • 4
  • 28
  • 44
13
votes
2 answers

Spring Test MockMvc perform request on external URL

I'm trying to perform a POST request on URL outside the current context, and it looks like Spring cannot understand it. Test code: String content = mvc .perform(post("http://some-external-url.com:8080/somepath) …
Mikhail Kholodkov
  • 23,642
  • 17
  • 61
  • 78
13
votes
3 answers

Test maximum upload file size with MockMultipartFile

I create a file upload service with Spring Boot and test it with Spring Mock Mvc and MockMultipartFile. I want to test if an error is thrown when the maximum file size is exceeded. The following test fails because it receive a 200. RandomAccessFile…
fabwu
  • 642
  • 6
  • 19
13
votes
3 answers

What is the best way to test that a spring application context fails to start?

I use the spring-boot-starter-web and spring-boot-starter-test. Let's say I have a class for binding configuration properties: @ConfigurationProperties(prefix = "dummy") public class DummyProperties { @URL private String url; //…
Roland Weisleder
  • 9,668
  • 7
  • 37
  • 59
13
votes
5 answers

@SQL one time per class

I'm writing some integration test using spring framework. I have different SQL scripts for different integration test classes. Something like this: @ContextConfiguration(classes = ...) @Sql("classpath:sportCenter-test.sql") public class…
Mihai238
  • 1,087
  • 13
  • 26
13
votes
1 answer

How to set servlet path for every request through MockMvc

Is it possible to set the servlet path for all requests (get, post, put, delete) which go through the MockMvc? The Spring dispatch servlet is mapped to /rest/* But in my test I have to remove the /rest part in the url otherwise Spring test does not…
Marcel Overdijk
  • 11,041
  • 17
  • 71
  • 110
13
votes
3 answers

Spring @Transactional does not work in JUnit test?

I am using Spring 3.2, Hibernate and JUnit 4. My Dao class is as follows: @Transactional public class SomeDaoImpl implements SomeDao { The update operations on this work if executed directly from web application. However, I am seeing that junit…
Nilesh
  • 4,137
  • 6
  • 39
  • 53
12
votes
5 answers

NoSuchFieldError when trying to run a jUnit test with Spring

So far I have two tests. One uses only jUnit framework and works fine. The other one uses spring-test library and creates this exception every time I try to run it. Any ideas what may cause the problem? Error java.lang.NoSuchFieldError: NULL at…
user219882
  • 15,274
  • 23
  • 93
  • 138