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
27
votes
1 answer

spring Should I use @DirtiesContext on every class

I have several junit tests, @ContextConfiguration(locations = { "file:../business/src/test/resources/application-context-test.xml", "file:src/main/webapp/WEB-INF/confA.xml",…
jpprade
  • 3,497
  • 3
  • 45
  • 58
26
votes
1 answer

@DataJpaTest needing a class outside the test

In a SpringBoot application, I would like to do some test about the repository layer. @RunWith(SpringRunner.class) @DataJpaTest public class VisitRepositoryTest { @Autowired private TestEntityManager entityManager; @Autowired …
robert trudel
  • 5,283
  • 17
  • 72
  • 124
26
votes
8 answers

how to mock spring amqp/rabbit in spring boot test

How to mock spring rabbitmq/amqp so it will not fail during a Spring Boot Test while trying to auto create exchanges/queues? Given I have a simple RabbitListener that will cause the queue and exchange to be auto created like…
domi
  • 2,167
  • 1
  • 28
  • 45
26
votes
1 answer

@TestPropertySource annotation alternative on a method level

@TestPropertySource(locations="classpath:test.properties") public class Aclass {} Is there an alternative that uses properties for the tested instance in a method. @Test public void aMethod(){}
kidwon
  • 4,448
  • 5
  • 28
  • 45
26
votes
1 answer

How to execute @Sql before a @Before method

I'm trying to combine the follow annotations: org.springframework.test.context.jdbc.Sql org.junit.Before Like the follow code: @Test @Sql(scripts = "dml-parametro.sql") public void testData(){ Iterable parametros =…
26
votes
4 answers

Spring Testing: How to enable auto-scan of beans

For example, now in each test class I have to do @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(loader=AnnotationConfigContextLoader.class) I want to get rid of @ContextConfiguration(loader=AnnotationConfigContextLoader.class) and…
user3594176
  • 263
  • 1
  • 4
  • 6
25
votes
4 answers

MockMvc configure a header for all requests

In my tests I setup the MockMvc object in the @Before like this mockMvc = MockMvcBuilders.webAppContextSetup(context) .apply(springSecurity()) .build(); In every request I do I always need to send the same…
isADon
  • 3,433
  • 11
  • 35
  • 49
24
votes
7 answers

SpringCloud 2020.0.2 upgrade generates testing error

I'm trying to upgrade a project from SpringCloud BOM 2020.0.1 to 2020.0.2 When I change the BOM and re-build, I get an error in JUnit tests, saying that the new parameter spring.config.import is not set for configserver. This project is not a…
Sourcerer
  • 1,891
  • 1
  • 19
  • 32
24
votes
2 answers

Spring test with @DataJpaTest can't autowire class with @Repository (but with interface repository works!)

I'm trying to understand why I can't autowire a class repository but I can autowire a interface repository in the same package for the same test. The same repository works as expected when I start the application. First, the error: Caused by:…
Dherik
  • 17,757
  • 11
  • 115
  • 164
24
votes
4 answers

How to use @ComponentScan together with test-specific ContextConfigurations in SpringJunit4TestRunner?

I am testing a Spring Boot application. I have several test classes, each of which needs a different set of mocked or otherwise customized beans. Here is a sketch of the setup: src/main/java: package…
Jonathan Fuerth
  • 2,080
  • 2
  • 18
  • 21
23
votes
3 answers

Using @MockBean in tests forces reloading of Application Context

I have several integration tests running on Spring Framework that extend the base class called BaseITCase. Like this: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {AppCacheConfiguration.class, TestConfiguration.class},…
dvelopp
  • 4,095
  • 3
  • 31
  • 57
23
votes
2 answers

Spring Boot Integration Testing with mocked Services/Components

Situation and Problem: In Spring Boot, how can I inject one or more mocked classes/beans into the application to do an integration test? There are a few answers on StackOverflow, but they are focused on the situation before Spring Boot 1.4 or are…
swaechter
  • 1,357
  • 3
  • 22
  • 46
23
votes
4 answers

Spring boot test configuration

I have a spring boot application with main class like below: @SpringBootApplication public class Application { public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); } } Now I want…
sansari
  • 558
  • 1
  • 7
  • 17
23
votes
2 answers

Cannot process locations AND classes for context configuration

I have wrote following test: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:META-INF/dataContext.xml"},classes = Configiuration.class) @ActiveProfiles("test") public class CityDaoImplTest { .... } I need to…
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
23
votes
5 answers

Spring integration tests with profile

In our Spring web applications, we use the Spring bean profiles to differentiate three scenarios: development, integration, and production. We use them to connect to different databases or set other constants. Using Spring bean profiles works very…
David V
  • 11,531
  • 5
  • 42
  • 66