Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run JUnit test only on Linux

Tags:

java

junit

I have a basic JUnit test which I want to run only on Linux. How I can skip the test if I build the code on Windows?

For example can I get the OS platform from Java?

like image 685
Peter Penzov Avatar asked Oct 17 '14 14:10

Peter Penzov


People also ask

How do I run JUnit test cases without Eclipse?

Yes, you have an option to run your test cases in command line, Example: As you are running the mvn clean install for building your application, In the same way, you can also run your test cases as well by using the command.


2 Answers

System.getProperty("os.name") will give you the name of the OS. You can then use the Assume class to skip a test if the OS is Windows:

@Test
public void testSomething() {
    Assume.assumeFalse
        (System.getProperty("os.name").toLowerCase().startsWith("win"));
    // test logic
}

Edit:
The modern JUnit Jupiter has a built-in capability for this with the @EnableOnOs and @DisableOnOs annotations:

@Test
@EnabledOnOs(LINUX)
public void testSomething() {
    // test logic
}
like image 120
Mureinik Avatar answered Oct 16 '22 05:10

Mureinik


You can also use @Before to bypass all tests contained in the class:

@Before
public void beforeMethod()
{
    Assume.assumeFalse(System.getProperty("os.name").toLowerCase().startsWith("win"));
    // rest of the setup
}
like image 3
ToYonos Avatar answered Oct 16 '22 05:10

ToYonos