Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different expectations of JUnit test under different OS environment

Tags:

java

junit

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

like image 496
saurav Avatar asked Apr 29 '19 15:04

saurav


People also ask

What is JUnit testing explain different types of testing?

JUnit is a unit testing open-source framework for the Java programming language. Java Developers use this framework to write and execute automated tests. In Java, there are test cases that have to be re-executed every time a new code is added. This is done to make sure that nothing in the code is broken.

Which function is used in JUnit to compare expected and actual result?

You use an assert method, provided by JUnit or another assert framework, to check an expected result versus the actual result. These method calls are typically called asserts or assert statements.

What are the reasons and benefits for using JUnit for testing?

Benefits of using JUnitJUnit can help you keep your code organized and easy to read. JUnit can help you detect and fix errors in your code. JUnit can help you improve the quality of your software. JUnit can help you work more efficiently and improve your testing process.


1 Answers

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

like image 52
gazzo Avatar answered Oct 23 '22 20:10

gazzo