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
4 answers

java.lang.AssertionError: Content type not set even after setting content type as json/application

This question has been asked before and I have tried their solution but that doesn't work for me, I am using MockMvc to unit test content type of my rest call. I am getting this exception: java.lang.AssertionError: Content type not set While I'm…
jack
  • 313
  • 2
  • 10
8
votes
0 answers

Single server instance should run all the integration test in spring boot application

I am writing a set of integration tests and each test class start the server and stop the server, which takes long time. Is it possible to create a test suite, where a single instance of server can be used for a set of test classes? Thanks, Manish
krmanish007
  • 6,749
  • 16
  • 58
  • 100
8
votes
1 answer

How to populate test data programmatically for integration tests in Spring?

I am looking for the recommended approach for populating test data programmatically in integration tests using spring / spring boot. I am using HSQLDB (inmemory). There is the possibility to execute SQL scripts in spring for integration tests like…
fischermatte
  • 3,327
  • 4
  • 42
  • 52
8
votes
1 answer

How to configure EntityManagerFactoryBuilder bean when testing Spring Boot Batch application?

I have a Spring Boot Batch application that I'm writing integration tests against. However, I'm getting the following error about the EntityManagerFactoryBuilder bean missing when running a…
James
  • 2,876
  • 18
  • 72
  • 116
8
votes
2 answers

Spring Testing - java.lang.IllegalArgumentException: Not enough variable values available to expand

I am writing Unit Tests for the below REST Controller which takes a UserID and grants a List of Authorities to that user. @RestController @RequestMapping("/user") @Api(value = "User", description = "User API") public class…
Sai Upadhyayula
  • 2,404
  • 3
  • 23
  • 35
8
votes
2 answers

How to test Spring HandlerInterceptor Mapping

I am implementing a HandlerInterceptor that needs to execute business logic prior/after requests to several paths are handled. I want to test the execution of the Interceptor by mocking the request handle lifecycle. Here is how the interceptor is…
kpentchev
  • 3,040
  • 2
  • 24
  • 41
8
votes
1 answer

Why can't my test inherit its ContextConfiguration location from its parent?

In the interest of DRY, I want to define my ContextConfiguration in a parent class and have all my test classes inherit it, like so: Parent class: package org.my; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations =…
Evan Haas
  • 2,524
  • 2
  • 22
  • 34
8
votes
1 answer

Spring testing framework - JNDI resource

I am toying a bit with Spring testing framework, but I have one problem. Normally when application is deployed on Tomcat we have
Andna
  • 6,539
  • 13
  • 71
  • 120
8
votes
6 answers

Spring context tests can't find config locations

I have a large application spread across multiple Spring bean definition xml files. In my test suite I manually load up the XML files I need using a FileSystemXmlApplicationContext to perform the tests I want to run. This reduces test set up time…
Adam B
  • 1,724
  • 3
  • 14
  • 29
7
votes
1 answer

Got an error with Junit platform v.5.8.1 java.lang.NoSuchMethodError: org.junit.platform.commons.util.AnnotationUtils.findAnnotation

I run into an issue with this code: @ExtendWith(MockitoExtension.class) class ApiRestControllerTest { private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); private MockMvc mockMvc; @InjectMocks private ApiRestController…
Volodymyr
  • 91
  • 1
  • 4
7
votes
1 answer

How to combine Testcontainers with @DataJpaTest avoiding code duplication?

I want to use Testcontainers with @DataJpaTest (and @SpringBootTest) using JUnit 5. I have the basic setup working using the @Testcontainers and @Container annotation like this: import org.junit.jupiter.api.Test; import…
Wim Deblauwe
  • 25,113
  • 20
  • 133
  • 211
7
votes
1 answer

Spring boot H2 db mode oracle

Hi I have a spring boot app (2.3.1.RELEASE, ojdbc8) and basically it is connected to an oracle database. The spring boot app starts find when it is connected to the oracle db.However fails to start when the integration test is connected to a H2…
user1999453
  • 1,297
  • 4
  • 29
  • 65
7
votes
3 answers

Test Unit Spring boot: Unable to register mock bean

I have two classes of tests: 1- for the unit test of the Controller class and 2- for the unit test of the Service class as below: 1- Test class controller: @RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureMockMvc public class…
7
votes
2 answers

Integration tests on springboot with constructor injection pattern

I am trying to use Constructor Injection dependency pattern. I wonder what is the correct approach to inject JPA Repositories on Integration test classes: I have my source code: RepoClass @Repository public interface MyClassRepo extends…
Mayday
  • 4,680
  • 5
  • 24
  • 58
7
votes
1 answer

Is a mocked bean (@MockBean) mocked before the Spring Context loads the actual Spring Bean?

Let's use the following as an example. @Autowired @MockBean private Foo foobar; Does the Spring Context load the class Foo first, and then apply the mock? Or does the @Mockbean get detected somehow, and Spring creates and applies the mock instead…
JCB
  • 341
  • 5
  • 18