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.


Web Apps, SPA, PWA with vanilla Java Script (ES 6+), CSS 3 and WebStandards only. As simple as possible, but not simpler. See you at: (Progressive) Web apps, Single Page Apps and WebStandards airhacks workshops at MUC airport, Winter Edition

airhacks.fm the podcast:

Stay in touch: airhacks.news.

Comments:

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