Testing / JUnit6 Interview Questions
What is conditional test execution in JUnit 6 and what built-in conditions are available?
JUnit 6 provides condition annotations that skip or enable tests based on runtime conditions without writing assumption code inside the test body. These are implemented as extensions using the ExecutionCondition interface.
| Annotation | Skips test when... |
|---|---|
| @EnabledOnOs / @DisabledOnOs | Running on a specific OS (e.g. WINDOWS, LINUX, MAC) |
| @EnabledOnJre / @DisabledOnJre | Running on a specific Java version range |
| @EnabledForJreRange / @DisabledForJreRange | JRE is inside or outside a specified version range |
| @EnabledIfSystemProperty / @DisabledIfSystemProperty | A system property matches (or doesn't match) a regex |
| @EnabledIfEnvironmentVariable | An environment variable matches a regex |
| @EnabledIf / @DisabledIf | A static boolean method returns false / true |
import org.junit.jupiter.api.condition.*; class ConditionalTests { @Test @EnabledOnOs(OS.LINUX) void linuxOnlyTest() { ... } @Test @DisabledOnOs({OS.WINDOWS, OS.MAC}) void notOnWindowsOrMac() { ... } @Test @EnabledOnJre(JRE.JAVA_21) void java21Feature() { ... } @Test @EnabledForJreRange(min = JRE.JAVA_17, max = JRE.JAVA_21) void java17to21Only() { ... } @Test @EnabledIfEnvironmentVariable(named = "ENV", matches = "CI") void ciOnlyTest() { ... } @Test @EnabledIfSystemProperty(named = "db.available", matches = "true") void databaseTest() { ... } // @EnabledIf: call a custom static method @Test @EnabledIf("isWeekend") void weekendTest() { ... } static boolean isWeekend() { DayOfWeek day = LocalDate.now().getDayOfWeek(); return day == DayOfWeek.SATURDAY || day == DayOfWeek.SUNDAY; } }
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
