Different expectations of JUnit test under different OS environment

saurav :

I have a client side java application which runs on Multiple OS.

I want to test a logic which gives different expectations when run in different OS.

For e.g when i run on Windows the logic will pass and when i run on OSX the logic will throw an exception.

I can write two separate tests and execute only one using separate build profiles for different OS but is it possible to write a single junit test which will behave differently under different OS. ?

For OSX i want to annotate the test with exception as expectation and For Windows normal logic should work. Is it possible in Junit that at runtime the expectations differ under certain circumstances ?

Best Regards,

Saurav

gazzo :

with System.getProperty("os.name") you can check which operating system you are on, then you can use conditional running to exclude the test cases you don't need on a specific operating system, e.g.

public class SomeTest {
  @Rule
  public ConditionalIgnoreRule rule = new ConditionalIgnoreRule();

  @Test
  @ConditionalIgnore( condition = NotRunningOnWindows.class )
  public void testFocus() {
    // ...
  }
}

public class NotRunningOnWindows implements IgnoreCondition {
  public boolean isSatisfied() {
    return !System.getProperty( "os.name" ).startsWith( "Windows" );
  }
}

source: code affine

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=77087&siteId=1