2.3.4 Hands-on: Running the Parameterised Tests
Course subject(s)
Module 2. Functional Testing
Let’s do some boundary testing and implement it using the Parameterized Tests feature from JUnit 5 that we saw in the video.
- Create a ChocolateBagsTest class, under the tudelft.chocolate package.
- Replace its code by the one with all the boundary tests:
package tudelft.chocolate; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; public class ChocolateBagsTest { @ParameterizedTest(name = "small={0}, big={1}, total={2}, result={3}") @CsvSource({ "1,1,5,0", "1,1,6,1", "1,1,7,-1", "1,1,8,-1" }) public void totalIsTooBig(int small, int big, int total, int expectedResult) { ChocolateBags bags = new ChocolateBags(); int result = bags.calculate(small, big, total); Assertions.assertEquals(expectedResult, result); } @ParameterizedTest(name = "small={0}, big={1}, total={2}, result={3}") @CsvSource({ "4,0,10,-1", "4,1,10,-1", "5,2,10,0", "5,3,10,0" }) public void onlyBigBars(int small, int big, int total, int expectedResult) { int result = new ChocolateBags().calculate(small, big, total); Assertions.assertEquals(expectedResult, result); }
-
@ParameterizedTest(name = "small={0}, big={1}, total={2}, result={3}") @CsvSource({ "0,3,17,-1", "1,3,17,-1", "2,3,17,2", "3,3,17,2", "0,3,12,-1", "1,3,12,-1", "2,3,12,2", "3,3,12,2"}) public void bigAndSmallBars(int small, int big, int total, int expectedResult) { int result = new ChocolateBags().calculate(small, big, total); Assertions.assertEquals(expectedResult, result); } @ParameterizedTest(name = "small={0}, big={1}, total={2}, result={3}") @CsvSource({ "4,2,3,3", "3,2,3,3", "2,2,3,-1", "1,2,3,-1" }) public void onlySmallBars(int small, int big, int total, int expectedResult) { int result = new ChocolateBags().calculate(small, big, total); Assertions.assertEquals(expectedResult, result); } }
- Stop for a second and read all the tests. Make sure you understand how each part of the code works.
- Notice that in our code, different from the video, we have a name in the Parameterized Tests. With this parameter, we tell JUnit what name to display for each execution . In this case, we are telling JUnit that the display name should be “small={0}, big={1}, total={2}, result={3}” for each test it executes. The numbers in between brackets will be replaced by the parameters in the CsvSource.
- You can better see it when you run the tests:
- If you want to explore a bit more: 1) add some other inputs to the methods and see them executing (for now, you should not mind that some of them might look repetitive), 2) change the display name.
-
Automated Software Testing: Practical Skills for Java Developers by TU Delft OpenCourseWare is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Based on a work at https://online-learning.tudelft.nl/courses/automated-software-testing-practical-skills-for-java-developers/.