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

Authentication should not be null in unit tests with spring-session

I have a spring boot (version 1.5.9.RELEASE) application which uses spring-session to store sessions on Redis. It also uses spring-security to authenticate users. When running the application, after a successful login, the security context contains…
7
votes
1 answer

Testing spring-boot @service class

I want to test a @Service class that is normally called with SpringApplication.run(). The service class is: @Service @EnableConfigurationProperties(AppProperties.class) public class MongoService { private static final Logger logger =…
Andrea Girardi
  • 4,337
  • 13
  • 69
  • 98
7
votes
3 answers

How to mock http header in MockRestServiceServer?

I'm using MockRestServiceServer to mock an external webservice xml response. That already works fine, but how can I also mock the http header inside the response, not only the response body? @MockBean private RestTemplate restTemplate; …
membersound
  • 81,582
  • 193
  • 585
  • 1,120
7
votes
2 answers

Using RestTemplateBuilder with @ContextConfiguration in IT tests

I am running into problems while using RestTemplateBuilder with @ContextConfiguration in a Spring boot application (I have tried to add @SpringBootTest, @RunWith(SpringRunner.class) annotation without any luck). Any help is appreciated. Here is the…
7
votes
3 answers

Exclude a particular class from Spring Component scan while writing Spring Integration Test

My classes are.. lies in src/intregation-test/java @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = StoreOrderFulfillmentApplication.class) @ActiveProfiles("Test") public class…
Shobhit Pal
  • 83
  • 1
  • 1
  • 8
7
votes
2 answers

When should I use TypeExcludeFilters in Spring?

Recently, Spring Boot added TypeExcludeFilters. One prominent use case is the SpringBootApplication annotation. Before Spring Boot 1.4: // ... @ComponentScan public @interface SpringBootApplication { // ... Since Spring Boot 1.4: //…
Philipp Claßen
  • 41,306
  • 31
  • 146
  • 239
7
votes
1 answer

H2: generate insert scripts initialization script

I have full h2 database with lots data in it. I want to launch integration tests agains that data. Question1: Is it possible to generate *.sql insert files/scripts from full h2 database? I've trie SCRIPT TO 'fileName' as described here. But it…
VB_
  • 45,112
  • 42
  • 145
  • 293
7
votes
3 answers

How to drop in-memory h2db between Spring Integration tests?

I am using Liquibase in my Spring web application. I have a bunch of entities with hundreds of tests for REST APIs in the integration tests for each entity like User, Account, Invoice, License etc. All of my integration tests pass when run by class…
TechCrunch
  • 2,924
  • 5
  • 45
  • 79
7
votes
2 answers

How to use random port for JUNIT Testcases using Spring Boot 1.4.0 +

I am using spring boot 1.3.6 and my JUNIT Test cases are running fine, after upgrading to spring boot 1.4.0 and attempting to remove deprecated classes is throwing me error My JUNITCLASS with…
rajadilipkolli
  • 3,475
  • 2
  • 26
  • 49
7
votes
0 answers

Camel integration spring test without @DirtiesContext

When I run an integration test of camel route processing as a standalone test, it passes. When I run all the tests, it fails with assertion errors of expected values. When I add @DirtiesContext to the abstract test class, they all pass ok. I checked…
redhead
  • 1,264
  • 1
  • 17
  • 32
7
votes
2 answers

SpringSecurity WithSecurityContext MockMvc OAuth2 always unauthorised

I have followed the following links to try and test OAuth2 @PreAuthorise(hasAnyRole('ADMIN', 'TEST') for example but I can't any of the tests to pass or even authenticate. When I try to access the end point with admin (or any role) it will never…
7
votes
2 answers

How to test Aspect in Spring MVC application

I have a Spring MVC application where I use an Aspect to catch exceptions in all controller methods @Component @Aspect public class ControllerExceptionAspect { private Logger logger; public ControllerExceptionAspect() { logger =…
Vadim Dissa
  • 961
  • 3
  • 13
  • 32
7
votes
3 answers

Speed Up Spring MockMvc Integration Test with Embedded Cassandra

We are using MockMvc Framework to test Spring Controllers with JUnit. Controller returns a DefferedResult. The mockmvc.perform looks like bellow mockMvc.perform(post("/customer") .accept(APPLICATION_JSON) …
Mritunjay
  • 25,338
  • 7
  • 55
  • 68
7
votes
1 answer

Order of bean destruction in Spring context hierarchy

Is it correct to say that when a Spring context hierarchy is closed, there is no guaranteed order in which the beans will be destroyed? E.g. the beans in the child context will be destroyed before the parent context. From a minimal example the…
7
votes
0 answers

BeanCreationException on Spring TestNG PowerMock test

I'm getting BeanCreationException when using @AutoWired (Spring Annotation) and @PrepareForTest (PowerMock) and running my Spring enabled TestNG test. I have a Spring controller, picked up via component scan. I'm testing with TestNG, Mockito, and…
Thomas Kessler
  • 1,717
  • 3
  • 16
  • 27