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

Using annotation @Sql, is it possible to execute scripts in Class level before Method level?

I want to execute two scripts before every test method, but i also want to define some scripts to execute before specific methods. Using Spring framework and @Sql annotations, is it…
Sandro Simas
  • 1,268
  • 3
  • 21
  • 35
7
votes
3 answers

How to unit test an Spring @Bean CommandLineRunner?

I'm using Spring Boot in a little PoC, and I'm trying to test a @Bean implementation. I have this code: @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,…
edwise
  • 869
  • 2
  • 9
  • 18
7
votes
3 answers

How to only autowire a specific class in JUnit spring test?

I want to write a kind of integration test, but only for a specific class. Which means I want all fields in that class to be automatically wired, but neglect any classes inside the same directory. Is that…
membersound
  • 81,582
  • 193
  • 585
  • 1,120
7
votes
8 answers

org.hibernate.service.UnknownServiceException: Unknown service requested

I am writing a unit test to for my AbstractHibernateRepository save method. I'm using spring test runner but I get the following exception when it runs: org.hibernate.service.UnknownServiceException: Unknown service requested…
user2054833
  • 2,115
  • 7
  • 24
  • 39
7
votes
1 answer

@ActiveProfiles in meta annotation and on test class not working

I created a meta annotation @EmbeddedMongoDBUnitTest that activates two profiles to be used in spring based unit tests. The basic setup…
James
  • 11,654
  • 6
  • 52
  • 81
7
votes
2 answers

TestNG - @BeforeMethod for specific methods

I'm using Spring Test with TestNG to test our DAOs, and I wanted to run a specific text fixture script before certain methods, allowing the modifications to be rolled back after every method so that the tests are free to do anything with the fixture…
user2479523
  • 103
  • 1
  • 1
  • 5
6
votes
3 answers

Mocking external dependencies in Spring Boot integration tests

Main question: Is there any way to replace a bean with a mock object in the whole context of Spring and inject the exact bean to the test to verify method calls? I have a Spring Boot application, and I'm trying to write some integration tests in…
Reza Ebrahimpour
  • 814
  • 6
  • 20
6
votes
1 answer

Can I run a `@SpringBootTest` class multiple times with different configurations?

I have an integration test that's using @SpringBootTest to spin up a Spring application context that's testing a simple Spring Boot app. I'm using Spock for writing the tests, my build tool is Maven. I'm looking for a way to run the same test class…
l7r7
  • 1,134
  • 1
  • 7
  • 23
6
votes
3 answers

Spring Boot test tries to initialize cache2k for the 2nd time and fails

After adding cache2k to my project some @SpringBootTest's stopped working with an error: java.lang.IllegalStateException: Cache already created: 'cache' Below I provide the minimal example to reproduce: Go to start.spring.io and create a simplest…
Kirill
  • 6,762
  • 4
  • 51
  • 81
6
votes
2 answers

How to disable Redis cache with Spring Boot?

With Spring Boot 2.1 I am defining a RedisCacheManager bean in a configuration file, with Java configuration. Everything works correctly but I would like sometimes to disable it, for instance in the tests. Spring Boot provides the…
6
votes
2 answers

Spring Data + SpringBootTest: How to mock SecurityContext for JPA auditing?

In a Spring Boot application I want to test (JUnit 5) the persistence layer with enabled auditing (@EnableJpaAuditing). I use Liquibase to setup a H2 db and Hibernate as the JPA…
Puce
  • 37,247
  • 13
  • 80
  • 152
6
votes
0 answers

InstanceAlreadyExistsException for EnvironmentManager when trying to start Spring Boot application in plain Spring test

I have simple web application: @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } @RestController public class SimpleController { …
6
votes
4 answers

Spring Boot test doesn't find bean from sibling package

I have a @SpringBootTest annotated test class which wants to make use of a test utility: package org.myproject.server; // ... @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) public class ServerITest { …
oberlies
  • 11,503
  • 4
  • 63
  • 110
6
votes
2 answers

Spring, webflux: The getRemoteAddress method of the ServerHttpRequest object returns null when request performed from WebTestClient

I have a controller @RestController public class NameController { @Autowired private NameService nameService; @GetMapping("/name") public Mono getName(ServerHttpRequest request) { return…
user10785909
  • 111
  • 5
6
votes
1 answer

Could not find an 'annotation declaring class' for unit tests

I am setting up an Spring boot application on Jenkins. For the unit tests i am getting below error. This error is not particular to one test cases. Every time I run it is giving me error for different test. I am not sure what is wrong. Same…
Kiran
  • 839
  • 3
  • 15
  • 45