From the course: Java: Testing with JUnit
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Lifecyle control: Before and after
From the course: Java: Testing with JUnit
Lifecyle control: Before and after
- [Instructor] Many cases, you would want to do something before and after your tests. We can perform certain actions before and after our test methods using, well, of course, annotations. We can add the annotation @BeforeAll to a method, and then it will run before the test. Let me demonstrate this behavior, but first, I'm going to create a new class, and I'm going to be calling this, BankAccountBeforeAndAfterTest, like this. And in here, I'm just going to copy the code of our ordered execution class, but I'm going to be getting rid of the ordered part here. So I'm going to be copying this in and removing Order. And I'm just going to start the balance at 500 now, just for a second. So after the first method, it should be around 200. Adding 500 to it will give us 700. So if I run these tests, they should pass like this. And as you can see, they're passing fine. All the red in the middle is because the configuration…
Contents
-
-
-
-
-
(Locked)
Dependency injection in your tests5m 7s
-
Repeated tests2m 55s
-
(Locked)
Parameterized tests9m 29s
-
(Locked)
Timeouts4m 49s
-
(Locked)
Parallel execution3m 42s
-
(Locked)
Lifecyle control: Before and after5m 10s
-
(Locked)
Custom messages and reports4m 28s
-
(Locked)
Conditional test execution4m 19s
-
(Locked)
Disabling unit tests1m 45s
-
(Locked)
Solution: Refactoring1m 1s
-
(Locked)
-
-