I saw this question: Repeating code in JUnit tests earlier today. How do you write this code when you are starting? You see that there is a method addDrivingRecord(...). This method does not exist when you first start writing so do you make that test, ensure that it works, then proceed with setUp() method, or do you instead wait until you have written the addDrivingRecord(...) method and then refactor it to the @Before? I will explain further if needed.
I saw this question: Repeating code in JUnit tests earlier today. How do you
Share
If I understood well your asking if you should:
or
If it’s your question I should go for the first option: first use method, then implement and go green, then refactor your test.
Because two reasons:
You should test/implement one thing at a time, so you will write one test method. Then you will make it green. Only then you should write another method and realize that code can be refactored in a @Before
A good practice is write test methods and only when you realize there are common things move them to @Before. That way you don’t enforce innecesary things in initialization. Moreover, if you find that another test needs a very different @Before method it probably belongs to another test class.