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
24
votes
3 answers

Could not load TestContextBootstrapper - Spring Unit testing

I have to execute Unit test on one of my Dao classes using Spring. Here is my unit test: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:app-config.xml"}) @ActiveProfiles("local") public class HouseDaoTest { …
Tariq
  • 2,229
  • 9
  • 28
  • 59
24
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 and org.junit.Before Like the follow code: @Test @Sql(scripts = "dml-parametro.sql") public void testData(){ Iterable parametros =…
23
votes
5 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
  • 9,619
  • 22
  • 96
  • 201
23
votes
2 answers

Using Spring mockMvc to test optional path variables

I have a method in Spring MVC with optional path variable. I am trying to test it for a scenario when optional path variable is not provided. Snippet from Controller, resource URI to invoke- @RequestMapping(value = "/some/uri/{foo}/{bar}", method…
thisdotnull
  • 672
  • 1
  • 4
  • 18
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
  • 544
  • 1
  • 6
  • 14
23
votes
5 answers

Inject @AuthenticationPrincipal when unit testing a Spring REST controller

I am having trouble trying to test a REST endpoint that receives an UserDetails as a parameter annotated with @AuthenticationPrincipal. It seems like the user instance created in the test scenario is not being used, but an attempt to instantiate…
andrucz
  • 1,871
  • 2
  • 16
  • 28
22
votes
5 answers

Spring-Autowiring happens after @BeforeClass when running test with maven-surefire

I have some problems with dependency injection (Spring autowiring) and maven-surefire. The following test works without problems when run in eclipse with TestNG: The service-object is injected, then the @BeforeClass-method is…
thofoer
  • 221
  • 1
  • 2
  • 3
22
votes
2 answers

Spring overriding primary bean with non-primary bean

I am trying to override a Spring bean during a test declared in a test configuration with the use of @Primary. One declaration is in the src/main/java path, the other, the primary, is in src/test/java path. However, Spring is intentionally…
22
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
  • 1,550
  • 1
  • 14
  • 19
22
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,073
  • 2
  • 38
  • 52
22
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
  • 10,931
  • 5
  • 39
  • 64
22
votes
5 answers

How to populate database only once before @Test methods in spring test?

My next problem testing spring service layer with junit4 is: How to call script that populates database only once before all @Test methods: I want to execute this once before all @Tests: JdbcTestUtils.executeSqlScript(jdbcTemplate(), new…
Volodymyr Levytskyi
  • 3,064
  • 9
  • 42
  • 75
21
votes
3 answers

Can Spring Boot test classes reuse application context for faster test run?

@ContextConfiguration location attribute does not make sense for Spring Boot integration testing. Is there any other way for reusing application context across multiple test classes annotated with @SpringBootTest ?
vicusbass
  • 1,394
  • 1
  • 15
  • 29
21
votes
2 answers

Spring Boot. @DataJpaTest H2 embedded database create schema

I have couple of entities in my data layer stored in particular schema. For example: @Entity @Table(name = "FOO", schema = "DUMMY") public class Foo {} I'm trying to setup H2 embedded database for integration testing of my data layer. I'm using…
21
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
  • 31,683
  • 83
  • 267
  • 574