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

WebMvcTest with Spring Security seems to incorrectly return 404 - possible bug? or am I doing it wrong?

When testing a controller with WebMvcTest and Spring Security (using Boot 3 and Security 6), the authorization seems to be failing. Testing the endpoint with no user returns a 401 as expected, but when a valid user is provided, it returns 404, not…
-1
votes
1 answer

Mocking propertysource for integration test

I have a configuration class as @Configuration @Getter @Setter @PropertySource(value = { "file:/config/appconfig.properties" }) public class ExternalProperties { // varibles which get initize with @Value } which reads value from a file. But When I…
-1
votes
1 answer

Write Junit 5 for below code in springboot

public URL generateSignedUrl( String fileName) { Optional url = Optional.empty(); try { long startTime = System.currentTimeMillis(); Storage storage =…
-1
votes
2 answers

SpringBootTest : Autowired Repositories are not working

I have a test class @SpringBootTest @ActiveProfiles("test") @ExtendWith(SpringExtension.class) @AutoConfigureMockMvc public class ApplicationIntegrationTest { @Autowired MockMvc mvc; @Autowired MyRepository myRepository; @Test public void…
javaDeveloper
  • 1,403
  • 3
  • 28
  • 42
-1
votes
2 answers

How to make @SpringBootTest use application.properties from external config folder (inside project root)?

I have a multi-module Spring Boot Gradle project (Kotlin) with the following directory structure. root |- domain (Module containing entities and interfaces) |- application (Spring boot Kotlin application) |- src/main |- kotlin (app…
-1
votes
1 answer

Catching an exception RESTEASY004645: templateValues entry was null

I am in the process of creating tests for AdminClientService, which whould create users into Keycloak database. In the main processing it works as it should, but while creating tests i bumped into this exception: java.lang.NullPointerException:…
MrMRas
  • 11
  • 2
-1
votes
1 answer

Junit test case for WebClient Builder for spring framework for reactive

Code for Get call using web client which I am trying to unit test webClient.get().uri("http://localhost:8080/employee/{id}", xyz).retrieve().bodyToMono(String.class);
-1
votes
1 answer

Unable to find org.junit.Test in spring boot 2.7.0

I am unable to find org.junit.Test in spring boot 2.7.0 I am using eclipse and when I press ctrl+space after writing @Test, I can see only option for import that is org.junit.jupiter.api.Test as shown in screenshot below. 2.7.0 doesn't support…
Rishiraj Fasalkar
  • 115
  • 1
  • 2
  • 8
-1
votes
1 answer

Spring Boot test - Is there any way to run ALL test code on production server startup?

I have several test codes for APIs that are important than others. They do the test with MockMvc and are placed in 'test' package, seperated from the original codes. Is there any way to run all of the test codes when production server (the…
Flippingflop
  • 187
  • 1
  • 1
  • 11
-1
votes
1 answer

Create reusable mocks for model in junit test and spring boot

I am writing junit test cases for a spring boot application. I am having lot of doubts and I have listed them below. Is it enough to write unit test cases only for service layer? How to re-use the stubs / mocks created for the models. Each model…
-1
votes
1 answer

What is difference here - @Autowired and @MockBean

I was writing unit tests for our services classes in spring boot project. Where tests were properly running when I'am autowiring the class which am testing and it fails when I use @MockBean insead of @Autowire. @SpringBootTest class…
Venkat
  • 139
  • 1
  • 8
-1
votes
2 answers

Is there any way to extend the @Configuration class to include the constructor parameter?

I am writing integration tests for a Spring Batch Project, which has the following configuration @Configuration @EnableScheduling @PropertySource("application.properties") public class BatchConfig( private JobBuilderFactory factory; public…
-1
votes
1 answer

@Autowired object is null when working with @SpringBootTest

So I am trying to autowire my http object in my test class and I have tried to integrate with @SpringBootTest however my http object still remains null. My test class looks like this.…
Edward Muldrew
  • 253
  • 1
  • 6
  • 20
-1
votes
1 answer

JUnit 5 Search Rest api testing

How to test findByTargetProject method using JUnit 5. @Repository public interface suiteRepository extends JpaRepository { suiteInformations findByTargetProject(String url); } I tried this way but i am getting 404…
-1
votes
1 answer

Spring Boot Project test failed Mockito cannot mock this class or interface UserRepository

I'm working on a spring boot project and its controller, service, repositories are working fine. I tested them with POSTMAN. I'm new to mockito and trying to test my project. I have following test class: package com.spr; import static…
Ashutosh
  • 4,371
  • 10
  • 59
  • 105