Spring boot @Value property is null in test cases

Федор Николаев

I want to get values from application.properties.

@ExtendWith(MockitoExtension.class)
public class CurrencyServiceTest {

    @Mock
    private OpenExchangeRatesClient openExchangeRatesClient;

    @Value("${openexchangerates.app.id}")
    private String appId;

    @Value("${openexchangerates.currency.base}")
    private String base;

...

The thing is that appId and base are null in tests. What is the reason for that?

Deadpool

@Value is spring annotation and spring injects the value for properties into Spring-managed beans

This annotation can be used for injecting values into fields in Spring-managed beans, and it can be applied at the field or constructor/method parameter level.

If you want this functionality working, you have to use @SpringBootTest annotation to load ApplicationContext, which is typically writing integration test

The @SpringBootTest annotation is useful when we need to bootstrap the entire container. The annotation works by creating the ApplicationContext that will be utilized in our tests.

You can also inject value while writing unit test cases using ReflectionTestUtils, like example shown here

@Before
public void setUp() {
    ReflectionTestUtils.setField(springJunitService, "securityKey", "it's a security key");
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Not-null property references a null or transient value - Spring Boot - Thymeleaf

Replace @Value property within @Configuration during Spring Boot test

Write Spock test cases for Spring boot application

Getting null value in property list from yaml in spring boot

Spring boot @Value is null

Spring Boot - Test Cases - Dont Load All Components

ObjectId automatically changes when writing test cases in Spring-boot

How to write Junit test cases for spring boot application?

Writing JUnit test cases for a Spring Boot Service Application with autowired components

my unit test cases for api's are failing in spring boot

Unit test cases in Spring boot. Service methods or Http Urls

Override a property for a single Spring Boot test

Spring Boot Test does not use jmx property

Unit Test in Spring Boot for multiple values of property

Autowired property is null - Spring Boot Configuration

Spring Boot Controller Test, Null Pointer Exception

Spring Boot MVC Test - MockMvc is always null

Spring boot application is not picking all the test cases for execution after migrating spring boot 2.5.12

Status expected:<200> but was:<404> in spring test while testing my spring boot test cases

curly brace in spring boot default property value

Get the value of a property of an object Spring Boot Java

@Value "Could not resolve placeholder" in Spring Boot Test

@Value property is returning null in the spring component

Why is the entity manager not null in the spring boot test parent but it is null in the repository?

Spring Boot request header return null value

Spring Boot: @Value returns always null

Spring Boot - Configuration Value from yml is null

Spring Boot @Value is null, cant be solved with @Component

Test Cases Fail On Null Assertion