Test-driven development (TDD) combat experience

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/wangchengming1/article/details/101293043

Wrote a theory of TDD in the previous article, the junior partner interested can go read it . This article today with a simple example to experience the TDD process.

surroundings
  • Java 8
  • Junit 5
demand

We have in this way a requirement: customer needs a rectangle, the width and height can be set to a rectangular shape, and the area can be calculated

1. write test cases
  • At this time, the following class Rectangle
class Rectangle {

    private double width;

    private double height;

    public void setWidth(double width) {
        this.width = width;
    }

    public void setHeight(double height) {
        this.height = height;
    }
}
  • Written test case
public class RectangleTest {

    @Test
    void should_return_20_when_width_2_and_height_10() {

        double width = 2;

        double height = 10;

        Rectangle rectangle = new Rectangle();

        rectangle.setWidth(width);
        rectangle.setHeight(height);

        assert (rectangle.count(width, height) == 20);
    }
}
2. Run the test case

You will see the test case fails to run (because you did not write function code)

3. write business code
class Rectangle {

    private double width;

    private double height;

    public void setWidth(double width) {
        this.width = width;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public double count(double width, double height) {
		 return width * height;
    }
}
4. Run the test cases, test cases and then saw through the
5. Network access code, reconstituted
  • Supplementary test cases
public class RectangleTest {

    @Test
    void should_return_20_when_width_2_and_height_10() {

        double width = 2;

        double height = 10;

        Rectangle rectangle = new Rectangle();

        rectangle.setWidth(width);
        rectangle.setHeight(height);

        assert (rectangle.count(width, height) == 20);
    }

    @Test
    void should_throw_exception_when_width_given_error_value() {

        double width = -10;

        double height = 10;

        Rectangle rectangle = new Rectangle();

        assertThrows(IllegalArgumentException.class, () -> rectangle.count(width, height));
    }
}
  • Run the test case, the failure found
  • Then supplemental code, in this case the following class Rectangle
class Rectangle {

    private double width;

    private double height;

    public void setWidth(double width) {
        this.width = width;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public double count(double width, double height) {
        if (width <= 0 || height <= 0) {
            throw new IllegalArgumentException();
        }
        return width * height;
    }
}

TDD is more than a simple use case process, but it is still very pleasant.

Guess you like

Origin blog.csdn.net/wangchengming1/article/details/101293043
TDD