Test boot system properties

I recently found a solution that allows loading system properties for my unit tests. It works fine if I run the test separately, but if I want to run the entire test suite, it fails. Can someone tell me why?

The first step is to load the context of the test application:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/applicationContext-test.xml")

The next step is to create a class that will load the properties of the system:

import java.io.InputStream;
import java.util.Properties;

import javax.annotation.PostConstruct;

import org.springframework.core.io.Resource;

public class SystemPropertiesLoader{

    private Resource resource;

    public void setResource(final Resource resource){
        this.resource = resource;
    }

    @PostConstruct
    public void applyProperties() throws Exception{

        final Properties systemProperties = System.getProperties();

        final InputStream inputStream = resource.getInputStream();

        try{
            systemProperties.load(inputStream);
        } finally{
            inputStream.close();
        }
    }
}

The final step is to display this as a bean in my test application context:

<bean class="com.foo.SystemPropertiesLoader">
    <property name="resource" value="classpath:localdevelopment_Company.properties" />
</bean>

, , , . , . , , SystemPropertiesLoader , beans . , null, . ?

+3
3

, Properties . , , :

  • A. A applicationContext-test.xml, , Properties.
  • Properties .
  • B. B applicationContext-test.xml.
  • SystemPropertiesLoader, .
  • Properties, , .

Properties.

+2

:

  • , . spring .
  • . spring , beans. appContext.xml, (. PropertyPlaceHolderConfigurer), System. spring.
  • . System .
+3

, JVM, System ?

Possible, try to use the methods setUp()and tearDown()in your JUnit test class.

0
source

Source: https://habr.com/ru/post/1777715/


All Articles