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
39
votes
21 answers

org.junit.platform.commons.JUnitException: TestEngine with ID 'junit-jupiter' failed to discover tests

I wan to implement a Junit 5 test into Gradle project. I tried this: Gradle configuration: plugins { id 'org.springframework.boot' version '2.5.5' id 'io.spring.dependency-management' version '1.0.11.RELEASE' id 'java' } group =…
Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
38
votes
2 answers

@Import vs @ContextConfiguration in Spring

Is there any difference in usage of the annotations? Both the annotations allow to use multiple @Configuration classes to create an ApplicationContext. From their docs @ContextConfiguration seems to be more suitable for test configuration and comes…
Dmitry Senkovich
  • 5,521
  • 8
  • 37
  • 74
37
votes
5 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,714
  • 2
  • 19
  • 33
35
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
  • 812
  • 1
  • 7
  • 20
34
votes
3 answers

Spring Test session scope bean using Junit

I have a session scoped bean which holds user data per http session. I would like to write a Junit test case to test the session scoped bean. I would like to write the test case such that it can prove that the beans are getting created per…
Shamik
  • 6,938
  • 11
  • 55
  • 72
33
votes
3 answers

@SpringBootTest vs @ContextConfiguration vs @Import in Spring Boot Unit Test

I'm working on a Spring Boot project. I'm writing a Unit Test code based on TDD which is a little bit difficult. @SpringBootTest loaded all beans, which led to longer test times. So I used the @SpringBootTest's class designation. I completed the…
33
votes
8 answers

request scoped beans in spring testing

I would like to make use of request scoped beans in my app. I use JUnit4 for testing. If I try to create one in a test like this: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {…
harschware
  • 13,006
  • 17
  • 55
  • 87
32
votes
4 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…
30
votes
2 answers

Configuring base package for component scan in Spring boot test

When I launch my test with the following annotations: package com.hello.package.p1; @RunWith(SpringRunner.class) @DataMongoTest @SpringBootTest public class ClassATest { @Autowired Service1 serivce1; //fqn = com.hello.package.p1.Service1…
ElArbi
  • 1,145
  • 3
  • 13
  • 22
29
votes
7 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,971
  • 2
  • 18
  • 30
28
votes
9 answers

Tests fail when executed from maven but not from Intellij

I'm writing my unit tests using JUnit, PowerMock, Spring Test and an in-memory H2 database. When I run the tests from Intellij everything runs correctly. But when I run the tests from maven (either from Intellij or from the command line) they fail.…
Neets
  • 4,094
  • 10
  • 34
  • 46
28
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…
28
votes
5 answers

Spring Test returning 401 for unsecured URLs

I am using Spring for MVC tests Here is my test class @RunWith(SpringRunner.class) @WebMvcTest public class ITIndexController { @Autowired WebApplicationContext context; MockMvc mockMvc; @MockBean …
Marco Prado
  • 1,208
  • 1
  • 12
  • 23
27
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,489
  • 11
  • 36
  • 61
27
votes
1 answer

What are TestExecutionListeners, and what do they do?

As far as I understand, TestExecutionListeners act like @BeforeClass methods in JUnit. What I don't understand is why I need to use DependencyInjectionTestExecutionListener, TransactionalTestExecutionListener and DirtiesContextTestExecutionListener…
Ali Arda Orhan
  • 764
  • 2
  • 9
  • 24