I have a class that calls native Windows API calls through JNA. How can I write JUnit tests that will run on a Windows development machine, but will be ignored on a Unix build server?
I can easily get the main OS using System.getProperty("os.name")
I can write protection blocks in my tests:
@Test public void testSomeWindowsAPICall() throws Exception { if (isWindows()) {
This additional boiler plate code is not ideal.
As an alternative, I created a JUnit rule that runs only a test method on Windows:
public class WindowsOnlyRule implements TestRule { @Override public Statement apply(final Statement base, final Description description) { return new Statement() { @Override public void evaluate() throws Throwable { if (isWindows()) { base.evaluate(); } } }; } private boolean isWindows() { return System.getProperty("os.name").startsWith("Windows"); } }
And this can be accomplished by adding this annotated field to my test class:
@Rule public WindowsOnlyRule runTestOnlyOnWindows = new WindowsOnlyRule();
Both of these mechanisms, in my opinion, are insufficient in that they will pass silently on a Unix machine. It would be better if they could be marked somehow at runtime with something similar to @Ignore
Does anyone have an alternative suggestion?
java junit junit-rule
darrenmc
source share