From the course: Java: Testing with JUnit

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Solution: Refactoring

Solution: Refactoring

- [Instructor] I'm going to refactor the code so that I don't need the initialization in both the methods as the first line. The first thing I'm going to do is I'm going to add the calculator as a property of our test class. Then I'm going to annotate the setup @BeforeEach, but please mind, in order to use BeforeEach, I have to go ahead and add an import. Inside my for each, I'm going to initialize the calculator. And I'm going to remove this initialization from both the methods. Now, I go ahead and test my code, and as you can see, it is correct, and I have correctly refactored this code.

Contents