0

I followed the below StackOverflow page and wrote test case for Application class How to test main class of Spring-boot application

When I run my test case I get the following error

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'http.client.connection.timeout' in value "${http.client.connection.timeout}" ..... 

I have added @TestPropertySource("classpath:test-manifest.yml") in my test case.

test-manifest.yml has 'http.client.connection.timeout'

My Testcase

import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.mypackage.Application; @RunWith(SpringJUnit4ClassRunner.class) @TestPropertySource("classpath:test-manifest.yml") @SpringBootTest public class MainTest { @Test public void main() { Application.main(new String[] {}); } } 

How to make it work? Any help is appreciated.

1 Answer 1

2

TestPropertySource does not support yaml configuration files.

Check https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/test/context/TestPropertySource.html

Supported File Formats

Both traditional and XML-based properties file formats are supported — for example, "classpath:/com/example/test.properties" or "file:/path/to/file.xml".

See also TestPropertySourceUtils.addPropertiesFilesToEnvironment():

try { for (String location : locations) { String resolvedLocation = environment.resolveRequiredPlaceholders(location); Resource resource = resourceLoader.getResource(resolvedLocation); environment.getPropertySources().addFirst(new ResourcePropertySource(resource)); } } 

ResourcePropertySource can only deal with .properties files and not .yml. In regular app, YamlPropertySourceLoader is registered and can deal with .

Possible solutions:

Either change your config to .properties or rely on profiles to load your test config.

Sign up to request clarification or add additional context in comments.

2 Comments

I replaced "test-manifest.yml" with "application.properties" file. It is working as expected but my other test cases are working with ".yml" TestPropertySource. Why it is working on my traditional test cases with MockMvc?
If younuse application.properties (or even application.yml) you don’t need TestPropertySource. It is difficult to guess what happens in your other tests without seeing them.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.