Posts

Showing posts with the label Spring Test @TestPropertySource Example

Spring Testing Annotation - @TestPropertySource - Example

Image
In this section, we will learn about @TestPropertySource annotation. @TestPropertySource is a class level annotation.  It allows us to set test properties for a specific test, taking precedence over default property sources like Java system properties, operating system's environments etc. So the test property files or  inlined properties can be used to override properties defined in system and application property sources. If we don't want to use a file, we can specify  inlined properties . The inlined properties loaded from @TestPropertySource have higher precedence over the properties loaded from resource locations. 1. @TestPropertySource Property file location @SpringBootTest @TestPropertySource ( "/test.properties" ) class FilePropertyInjectionUnitTest { //todo } } Above we specified property file location.  2. @TestPropertySource Inline Properties @SpringBootTest @TestPropertySource (properties = { "demo.app.key=4567" }) public class...