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

How to disable Eureka and Spring Cloud Config in a WebMvcTest?

I play with a simple Spring Boot application which registers itself in Eureka and uses spring cloud configuration to read some properties. Here it goes: @SpringBootApplication @EnableEurekaClient public class Application { public static void…
22
votes
3 answers

Spring Integration Testing: Could not detect default resource locations

I am running integration tests for my Spring Boot application with Maven's Failsafe plugin. When I create a simple test such as this one: @RunWith (SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(App.class) public class MyTestIT { …
lanoxx
  • 12,249
  • 13
  • 87
  • 142
22
votes
3 answers

Is there a way to use AssertJ assertions with Spring MVC Test?

I have been using AssertJ for some time in my projects. Recently I started using Spring MVC Test for testing Spring MVC controllers. But I am not getting how to use AssertJ with it. All examples I see online all use Hamcrest with Spring MVC…
ajm
  • 12,863
  • 58
  • 163
  • 234
22
votes
3 answers

@TestPropertySource with dynamic properties

I am using @TestPropertySource to overwrite application.yml properties in my integration test for a spring boot app. @TestPropertySource(properties = { "repository.file.path=src/test/resources/x" }) I was wondering if there was some way to make the…
Vladimir
  • 1,120
  • 2
  • 11
  • 18
22
votes
6 answers

How to disable Spring autowiring in unit tests for @Configuration/@Bean usage

I want configure a component test using spring-test configuration inner class (@Configuration). Tested components has some services which I'd like to mock for the test. These services are classes (no interface used) and have spring annotations…
vuk
  • 501
  • 1
  • 5
  • 9
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,364
  • 9
  • 46
  • 83
21
votes
4 answers

How to flush data into db inside active spring transaction?

I want to test hibernate session's save() method using spring testing framework. @Test method is : @Test @Transactional public void testSave() { User expected = createUser(); getGenericDao().currentSession().save(expected); User actual =…
21
votes
5 answers

Ehcache shutdown causing an exception while running test suite

I'm experiencing the following problem. I have a test suit in my project and each individual test runs fine. However when I run them as a suite I some of them fails with the following exception: Caused by: java.lang.IllegalStateException: The dao…
genjosanzo
  • 3,264
  • 2
  • 16
  • 21
20
votes
3 answers

Using @EnabledIf with spring.profiles.active property in Spring Environment within tests

According to the documentation (https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/context/junit/jupiter/EnabledIf.html#expression--) you can use the @EnabledIf annotation on a test class or test method like…
vab2048
  • 1,065
  • 8
  • 21
20
votes
14 answers

Why does my flapdoodle Embedded MongoDB test fail to run? (creating 'embeddedMongoServer' could not start process EOF)

I'm having trouble getting my brand new project to build. I used https://start.spring.io/ to generate a fresh new Spring 2.0 MongoDB Maven project, and I want to have an embedded MongoDB database for my integration tests. The spring initializer…
SnoopDougg
  • 1,467
  • 2
  • 19
  • 35
20
votes
1 answer

spring-boot testing - Could multiple test share a single context?

I created multiple spring-boot testing class, (with spring-boot 1.4.0). FirstActionTest.java: @RunWith(SpringRunner.class) @WebMvcTest(FirstAction.class) @TestPropertySource("classpath:test-application.properties") public class FirstActionTest { …
Eric
  • 22,183
  • 20
  • 145
  • 196
20
votes
2 answers

How to force `.andExpect(jsonPath()` to return Long/long instead int for int number for jackson parser

I have a simple test to my RestController. I expect that $[1].parent_idreturns Long as an object and not integer primitive. It will return Long if parent_id is in a long number range and > integer number range (such as :…
slawalata
  • 389
  • 1
  • 2
  • 14
20
votes
1 answer

How can I mock db connection in Spring Boot for testing purpose?

Situation: I am using Spring Cloud with Spring Boot in a microservice, that microservice is loading a DB config information to configure a connection. I created a test to get the rest interfaces using Swagger for documentation. I want to disable…
Rys
  • 4,934
  • 8
  • 21
  • 37
19
votes
2 answers

Spring boot ComponentScan excludeFIlters not excluding

I am having a SimpleTest : @RunWith(SpringRunner.class) @SpringBootTest(classes = SimpleTestConfig.class) public class SimpleTest { @Test public void test() { assertThat(true); } } and a configuration for this test :…
Daniel Taub
  • 5,133
  • 7
  • 42
  • 72