课程: Complete Guide To Java Testing with JUnit 5 & Mockito

今天就学习课程吧!

今天就开通帐号,24,600 门业界名师课程任您挑!

Conditional test execution based on custom conditions

Conditional test execution based on custom conditions

- [Instructor] If you want to run a test conditionally but not based on an environment variable, there is an option to create a more custom condition. With the EnabledIf and DisabledIf annotations, you can pass in a condition and that condition is used to determine whether a given test should be run. Let's take a look at a code example. Here, we have a series of tests verifying functionality on weekends and weekdays. We only want the weekdays test to run on weekdays, and the weekend test to run on weekends. Towards the end of the class, we have a method that determines whether a given day is a weekday. Let's use this in our tests. For the first test, we'll only enable it if the isWeekday method returns true. We can use the EnabledIf annotation and pass in the name of the method, isWeekday. Then for the second test, we'll want to run it if it's not a weekday, so we'll use the DisabledIf annotation, passing in that method name. This will disable the test if isWeekday returns true. Now…

内容