Specifying a Java path for a properties file

I have a Java Spring project configured using Maven. Since the number of unit tests and their configuration files quickly add up, I try to centralize the test configurations into one properties file (the same one that is used when building the project).

Unit tests are located (relative to the project path, of course) in the tree

src / test / java / com (...)

The resource files for these tests are in

src / test / resources (...)

And finally, the properties file that the resource file should read is in the directory

src / main / filters

Now I have a Junit class, where I specify the location of the configuration file as follows:


@ContextConfiguration(locations = { "classpath:com/initrode/quartz/SyncManagerJobTest-context.xml"})

There is a line in the configuration file SyncManagerJobTest-context.xml


<context:property-placeholder location="/src/main/filters/deploy.local.properties"/>

, . , src/main/filters. ../../ , . classpath: . "file:", , .

, : src/test/resources/ src/main/filters?

: Java , "file:" "classpath:"?

+5
3

src/main/filters , Maven target/classes, . , , . - , .

. , / , , src/main/filters target/test-classes -. , .

, build-helper-maven-plugin, .

:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <version>1.3</version>
  <executions>
    <execution>
      <id>add-resource</id>
      <phase>process-test-sources</phase>
      <goals>
        <goal>add-test-resource</goal>
      </goals>
      <configuration>
        <resources>
          <resource>
            <directory>scr/main/filters</directory>
          </resource>
        </resources>
      </configuration>
    </execution> 
  </executions>
</plugin>
+5

src/main/filters, ... . , src/test/resources? , ? - :

<build>
  <!-- Filter resources -->
  <filters>
    <filter>src/main/filters/my-filter.properties</filter>
  </filters>
  <!-- Resources for src/main -->
  <resources>
    <resource>
      <directory>src/main/resources</directory>
      <filtering>true</filtering>
    </resource> 
  </resources>
  <!-- Resources for src/test -->
  <testResources>
    <testResource>
      <directory>src/test/resources</directory>
      <filtering>true</filtering>
    </testResource>
  </testResources>
</build> 

, - , , . ( , ), maven . :

+5

Spring , . @TestPropertySource("/test.properties") , .

@TestPropertySource - , , PropertySources ApplicationContext, .

, . :

@Component
public class ClassUsingProperty {

@Value("${testpropertysource.one}")
private String propertyOne;

public String retrievePropertyOne() {
    return propertyOne;
 }
}

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = ClassUsingProperty.class)
@TestPropertySource
public class DefaultTest {


@Autowired
ClassUsingProperty classUsingProperty;

@Test
public void givenDefaultTPS_whenVariableRetrieved_thenDefaultFileReturned() {
    String output = classUsingProperty.retrievePropertyOne();

    assertThat(output).isEqualTo("default-value");
    }
 }

, , :

@TestPropertySource(locations = "/other-location.properties",
  properties = "baeldung.testpropertysource.one=other-property-value")
0

All Articles