Refactoring tests

Yesterday I finished my Bowling kata in java and I asked my practice tutor to give me feedback on the kata. He suggested doing one more refactor, but this time instead of doing it to the code, do it to the tests using Parameterized Tests.

What are Parameterized Tests?

Parameterized test is to execute the same test over and over again using different values. It helps developer to save time in executing same test which differs only in their inputs and expected results.

Now that we know what is a parameterized tests, let’s take a look at the test before making a refactor:

As we can see, the test bodies are the same. The only thing that changes is the data that we enter in the roll function and the expectation that we expect to obtain.

Let’s start by extracting the data and expect:

Now let’s create a single test that covers all cases:

As we can see, the test has been greatly simplified. I also removed the roll function and made a refactor of the saveRolls function. The refactor is instead of saving the rolls one by one, I save all the rolls.

As we can see we have reduced the size of BowlingGameShould quite a lot:

The use of Parameterized test can’t always be used and sometimes generates more noise than benefit. But in this case the test process is always the same, so it is acceptable.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store