adam bien's blog

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.