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
19
votes
6 answers

Spring boot test @Transactional not saving

I'am trying to do a simple Integration test using Spring Boot Test in order to test the e2e use case. My test does not work because I'am not able to make the repository saving data, I think I have a problem with spring contexts ... This is my…
Hedi Ayed
  • 371
  • 2
  • 3
  • 12
19
votes
7 answers

how to run/turn off selective tests based on profiles in spring boot

I have a spring-boot application for which I am writing IT tests. The data for the tests comes from application-dev.properties when I activate dev profile Here is what I have for…
brain storm
  • 30,124
  • 69
  • 225
  • 393
19
votes
7 answers

Maven: NoClassDefFoundError: org.springframework.test.context.junit4.SpringJUnit4ClassRunner

I have a classpath issue. Background: Building and running a Spring WebApp. Originally it was one big project including dao/service/controller/webapp. I have just broken my project into a maven module project essentially splitting the webapp from…
Jake
  • 4,322
  • 6
  • 39
  • 83
19
votes
4 answers

How do you mock Environment interface?

I am trying to test my service which looks like : import org.springframework.core.env.Environment; @Service public class MyService { @Autowired Environment env; ... ... } How do I mock Environment Interface, or else how do I create one?
storm_buster
  • 7,362
  • 18
  • 53
  • 75
18
votes
2 answers

Add one additional bean to "@WebMvcTest"

I have a controller and a test using @WebMvcTest and its running fine. Now i needed to add a little validation logic and for this i @Autowired an additional bean (a @Component, a MapstructMapper). As expected now the test is failing due to…
dermoritz
  • 12,519
  • 25
  • 97
  • 185
18
votes
6 answers

How can Spring's test annotation @Sql behave like @BeforeClass?

How can I tell the @Sql annotation to run only once for the class, and not for each @Test method? Like having the same behaviour as @BeforeClass? @org.springframework.test.context.jdbc.Sql( scripts = "classpath:schema-test.sql", …
membersound
  • 81,582
  • 193
  • 585
  • 1,120
18
votes
3 answers

Mockito Tests Pass individually but fail as part of a suite

I have a case where my mockito tests all pass individually but can fail when run as part of a testing suite. I am using mockito version 2.9.0 My test class is as follows import static…
Damien
  • 4,081
  • 12
  • 75
  • 126
18
votes
6 answers

How to debug a Spring Boot application via SpringBootTest

I'm new to Spring Boot and I really like it especially when it comes to eliminate the boilerplate code. I have created a test class to test my NBRController: @RunWith(SpringRunner.class) @SpringBootTest(classes =…
carlitos081
  • 645
  • 2
  • 7
  • 19
18
votes
1 answer

Make ApplicationContext dirty before and after test class

I have a particular class (let's say MyTest) in my Spring integration tests that is using PowerMock @PrepareForTest annotation on a Spring component: @PrepareForTest(MyComponent.class). This means that PowerMock will load this class with some…
dty
  • 466
  • 2
  • 6
  • 17
18
votes
1 answer

Testing Spring Framework 4 with Servlet 2.5

I'm having problem getting Spring Framework 4 to work with my existing project using Servlet 2.5. My web project actually runs fine, but my testcases are failing and it is caused by MockHttpServletRequest, which throws this…
limc
  • 39,366
  • 20
  • 100
  • 145
17
votes
5 answers

Create @MockBean with qualifier by annotating class?

In my Spring Boot test I'm using 2 mock beans with different qualifiers: @RunWith(SpringRunner.class) @SpringBootTest class HohoTest { @MockBean @Qualifier("haha") IHaha ahaha; @MockBean @Qualifier("hoho") IHaha ohoho; } Since I'm not using…
maslick
  • 2,903
  • 3
  • 28
  • 50
17
votes
3 answers

Eclipse Photon does not resolve imports in test sources

I have moved to Eclipse Photon with an existing workspace. I have some Maven projects in this workspace. All projects did not have any errors in Eclipse Oxygen. After opening my workspace in Eclipse Photon all test-classes which import…
LuCio
  • 5,055
  • 2
  • 18
  • 34
17
votes
4 answers

Error:java: java.lang.ExceptionInInitializerError IntelliJ

Heyo! I've ran into some troubles when attempting to boot up Intellij (ultimate) with JDK 10.0.1 and "spring-boot-starter-test". If I run the main method, regardless of its content and regardless of if I have any active tests I always receive…
Kavzor
  • 185
  • 1
  • 1
  • 10
17
votes
2 answers

Are Spring's MockMvc used for unit testing or integration testing?

Spring has 2 setups for the MockMvc: Standalone setup WebApplicationContext setup In general what kind of testing is MockMvc used for? Unit or Integration? or Both? Am i right in saying that using the standalone setup (running outside the Spring's…
SoftwareDeveloper
  • 1,094
  • 2
  • 15
  • 26
17
votes
3 answers

Reload or refresh a Spring application context inside a test method?

I need to change the Spring profiles that are active in my applicationContext within a single method of my test class, and to do so I need to run one line of code before refreshing the contest because I am using a ProfileResolver. I have tried the…
David E
  • 888
  • 2
  • 8
  • 13