My team is using JUnit to launch tests from eclipse.
Before executing these tests from eclipse, we have to specify the env variable LD_LIBRARY_PATH to load dependencies at run time. The correct value of LD_LIBRARY_PATH can change when one of my team's member commits a change in the project. We have a script which computes its correct value.
Today, we have two solutions to set the variable :
First method is unproductive, and with second method, we have to restart eclipse each time we update the project.
A better solution would be to write a script which would set the variable and which would be called by eclipse each time a test is run.
Is there a way to call a script before each test with Eclipse ? Any other idea would be also greatly appreciated.
Following the idea of the SO queston How to run Unix shell script from Java code? you could execute a script before each test or each test class as follows
@Before // could also be @BeforeClass
public void setupEnvViaScript() throws IOException, InterruptedException {
ProcessBuilder processBuilder = new ProcessBuilder("my-script.cmd", "arg-1", "arg-2");
processBuilder.environment().put("JAVA_HOME", "value");
processBuilder.environment().put("ENV_VAR", "value");
processBuilder.directory(new File("d:/working/path/script/shall/be/executed/in"));
Process process = processBuilder.start();
int exitValue = process.waitFor();
if (exitValue != 0) {
// check for errors
new BufferedInputStream(process.getErrorStream());
throw new RuntimeException("execution of script failed!");
}
}
This has some caveats
You could tackle this in several ways, like
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With