Questions tagged [spring-boot-test]

Spring Boot provides utilities and annotations to help when testing your application.

Spring Boot provides a @SpringBootTest annotation which can be used as an alternative to the standard spring-test @ContextConfiguration annotation when you need Spring Boot features. The annotation works by creating the ApplicationContext used in your tests via SpringApplication.

Utilities: ConfigFileApplicationContextInitializer,EnvironmentTestUtils,OutputCapture and TestRestTemplate

for example

EnvironmentTestUtils.addEnvironment(env, "org=Spring", "name=Boot");

Docs: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html

1399 questions
12
votes
2 answers

How can I combine @DataJpaTest @SpringBootTest in one MVC application for testing every layer?

Here https://stackoverflow.com/a/52968130/10894456 is well explained why @DataJpaTest @SpringBootTest shouldn't be mixed in one application. But barely explained the case when anyway need to test every layer of MVC SpringBoot application (from my…
J.J. Beam
  • 2,612
  • 2
  • 26
  • 55
12
votes
1 answer

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: FQPropretyName, could not initialize proxy - no Session

I am trying a ManyToOne Bi-Directional Association using SpringBoot, SpringDataJpa along with a unit test using SpringBootTest. However the test fails with the stacktrace shown below. However I am Unable to find the reason. Any pointers will be…
zikzakjack
  • 972
  • 2
  • 9
  • 20
11
votes
5 answers

Disable config service client activated with spring.config.import in SpringBootTest

I have a bare Spring Boot application @SpringBootApplication public class ClientApplication { public static void main(String[] args) { SpringApplication.run(ClientApplication.class, args); } } which connects to a Spring Cloud Config…
Henning
  • 3,055
  • 6
  • 30
11
votes
4 answers

@webMvcTest is not excluding and loading beans marked as @Repository

I've a @RestController which has only one dependency in field @Autowire that dependency is @component, that component Class definition has some autowired fields which are @service and those services have some @repositories. In this whole flow I've…
ThrowableException
  • 1,168
  • 1
  • 8
  • 29
11
votes
4 answers

MockMVC is not autowired, it is null

I am having an issue with injecting MockMvc to my test class. I tried a few options, but none of them works. 1 option: basically the same as here, manually creating MockMvc @Autowire MockMvc - Spring Data Rest 2 option: package…
bogdasha
  • 153
  • 1
  • 2
  • 11
11
votes
1 answer

java.lang.NoSuchMethodError: org.mockito.MockingDetails.getMockCreationSettings()Lorg/mockito/mock/MockCreationSettings

After migrating an application to springboot, I'm facing these issues while running the jUnits. It's issue with the version conflicts. These are the dependencies and versions I'm using : mockito-core - 2.2.7 mockito-all - 2.0.2-beta…
Praveesh P
  • 1,399
  • 2
  • 25
  • 42
11
votes
2 answers

Spring Boot 2.1.0 has JUnit5 dependencies, but how to get rid of it?

I've just upgraded my projects to use Spring Boot 2.1.0 (before it was 2.0.x) and i have compilation WARNINGS: [WARNING] Cannot find annotation method 'value()' in type 'org.junit.jupiter.api.extension.ExtendWith': class file for…
razor
  • 2,727
  • 6
  • 33
  • 45
11
votes
3 answers

Spring Context Test With Just One Bean

What's the recommended way to run a spring boot test where only the one subject under test is configured in the context. If I annotate the test with @RunWith(SpringRunner.class) @SpringBootTest(properties =…
David
  • 7,652
  • 21
  • 60
  • 98
10
votes
3 answers

Strict @MockBean in a Spring Boot Test

I am developing a Spring Boot application. For my regular service class unit tests, I am able to extend my test class with MockitoExtension, and the mocks are strict, which is what I want. interface MyDependency { Integer execute(String…
Joseph K. Strauss
  • 4,683
  • 1
  • 23
  • 40
10
votes
3 answers

IntegrationTest isolation fails in springboot 2.2.2.RELEASE (Error dopping tables after each SpringBootTest)

Our app is working in 2.0.4 release. After upgrade to 2.2.2.RELEASE we see integration tests failing. I suspect that there is some misconfiguration, and each integration test simply does not clean after itself or there is extra initialization which…
Martin Mucha
  • 2,385
  • 1
  • 29
  • 49
10
votes
2 answers

How to use @MockBean with JUnit 5 in Spring Boot?

I'm trying to wirte a unit test with @MockBean and JUnit 5 in a @WebMvcTest. Unfortunately it looks like @MockBean is ignored and it tries to set-up the full persistence layer, which fails and which is not what I want for a unit test. As far as I…
BetaRide
  • 16,207
  • 29
  • 99
  • 177
10
votes
2 answers

@WebMvcTest No qualifying bean of type repository

I'm writing a controller test where controller looks like @RestController public class VehicleController { @Autowired private VehicleService vehicleService = null; ... } While the test class looks like…
Ganesh Satpute
  • 3,664
  • 6
  • 41
  • 78
10
votes
1 answer

How to access H2 console in Spring Boot 2.0 test that uses DataJpaTest

When using @DataJpaTest, how can I configure the test class to run with the bits needed to handle http requests for the H2 console? I am running a Spring Boot 2.0 test that uses H2. I want to set a breakpoint in a test and view the contents of a…
Mark Norman
  • 2,271
  • 1
  • 13
  • 17
10
votes
1 answer

Accessing H2 Console While Running SpringBootTest

If I'm running a test with @SpringBootTest is there any way to access the H2 console? I have a test that accesses an H2 database (successfully), but if I want to inspect the database myself, how can I? I started by running the test with a…
Geoffrey Wiseman
  • 5,459
  • 3
  • 34
  • 52
10
votes
1 answer

@ComponentScan in application class breaks @WebMvcTest and @SpringBootTest

I am creating tests with @WebMvcTest annotation and found that if I have a @ComponentScan annotation in the application class it will break the expected behavior of the tests. According to the WebMvcTest javadoc: Using this annotation will disable…
1 2
3
93 94