Adam Bien's Weblog

Integration Testing: Setting System Properties Before DeltaSpike

DeltaSpike's Test-Control Module loads the unit test after the injected classes. Therefore any configured system properties set in @Before or even @BeforeClass are not going to be considered.

To test the underlying class:


public class SystemPropertyExposer {

    @Produces
    public String expose() {
        return System.getProperty("dev");
    }

}

with e.g:


@RunWith(PropertiesLoaderTestRunner.class)
public class SystemPropertyExposerIT {

    @Inject
    String developer;

    @Test
    public void developerInjection() {
        assertThat(developer, is("duke"));
    }

}

You will have to set the property, before the initialization of SystemPropertyExposer.

This can be achieved with the following workaround:


import org.apache.deltaspike.testcontrol.api.junit.CdiTestRunner;
import org.junit.runners.model.InitializationError;

public class PropertiesLoaderTestRunner extends CdiTestRunner {

    static {
        System.setProperty("dev", "duke");
    }

    public PropertiesLoaderTestRunner(Class testClass) throws InitializationError {
        super(testClass);
    }

}

The sample above was pushed into: github.com/AdamBien/javaeetesting.

See you at Java EE Workshops at Munich Airport, Terminal 2 and particularly at Continuous Java EE 7 Testing and Quality.


NEW workshop: HTML 5 and JavaScript Essentials, Three days in April 2017: from Java EE 7 Architectures over Microservices to Performance, Troubleshooting and Monitoring
On demand workshops: Java EE 7: Bootstrap, Effective, Testing and Microservices available for streaming.

Newsletter: airhacks.news

A book about rethinking Java EE Patterns

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
realworldpatterns.com
Online Workshops
...the last 150 posts
...the last 10 comments
License