Skip to lesson

Mini project: a ShopLite test plan and first tests

A test suite starts with a plan: which rules get fast unit tests, which work needs the real database and which few journeys go end to end. Then the first unit tests prove the plan works in the real build.

What you'll build

You add testing to your shoplite-api repository. First you write TEST-PLAN.md, one page that lists the tests ShopLite needs at each level of the pyramid for products, orders and security, and marks what already exists (20 Surefire tests, 70 integration tests).

Then you add the first plain unit tests: OrderTotalsTest checks OrderItem.lineTotal() and Order.total() with real BigDecimal prices from the seed data, without Spring or MySQL. Finally you break one test on purpose, read the Surefire output and report file, and fix it, so you know exactly what a failure looks like before you meet a real one.

Sample run (times will differ)
$ mvn test
...
[INFO] --- surefire:3.5.6:test (default-test) @ shoplite-api ---
[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.shoplite.auth.AuthControllerTest
[INFO] Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 5.712 s -- in com.shoplite.auth.AuthControllerTest
[INFO] Running com.shoplite.auth.JwtPropertiesTest
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.043 s -- in com.shoplite.auth.JwtPropertiesTest
[INFO] Running com.shoplite.order.OrderControllerTest
[INFO] Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.604 s -- in com.shoplite.order.OrderControllerTest
[INFO] Running com.shoplite.order.OrderTotalsTest
[INFO] Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.031 s -- in com.shoplite.order.OrderTotalsTest
[INFO] Running com.shoplite.product.ProductControllerTest
[INFO] Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.847 s -- in com.shoplite.product.ProductControllerTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 25, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] BUILD SUCCESS

Requirements

  • 1TEST-PLAN.md in the repository root lists unit, integration and end-to-end tests for products, orders and security, and what already exists.
  • 2mvn test runs every *Test class and mvn verify also runs the *IT classes against the shoplite_test database.
  • 3OrderTotalsTest is in src/test/java/com/shoplite/order and uses only plain objects: no Spring annotations, no database.
  • 4It has five tests, one behaviour each, named as sentences: one item, quantity, the saved order price, the order total and an empty order.
  • 5Every expected amount is a BigDecimal written with the same scale as the result, such as 1799.00.
  • 6One test is made to fail on purpose, the failure is read in the console and in target/surefire-reports, and then fixed.
  • 7mvn test ends with 25 tests and BUILD SUCCESS, and the work is pushed on a branch with a pull request.

Milestones

0/5 done · commit and push after each one
  1. Write a one-page test plan

    TEST-PLAN.md lists the tests for products, orders and security at each pyramid level.

  2. Confirm the build runs the tests

    mvn test shows 20 tests and mvn verify shows 70 integration tests, all green.

  3. Unit test the line totals

    OrderTotalsTest has three green tests for OrderItem.lineTotal().

  4. Unit test the order total

    Five green tests, including the total of two lines and an empty order.

  5. Break one test on purpose, read the report, fix it

    You have seen and read a real failure, and mvn test ends with 25 tests and BUILD SUCCESS.

Build it in your own GitHub repository and push after each milestone. When everything works, mark the project as complete.

Extra challenges (optional)

Do any you like, each on its own branch, so your main project keeps working.

  1. Add a test that an order with three lines of different quantities totals correctly, using ACC-AD141 at 1299.00.· easy

  2. Add a TEST-PLAN.md row for every endpoint group in API-DESIGN.md and mark which ones have no end-to-end coverage yet.· medium

  3. Add a test that changing a product's price with setPrice after the order was placed does not change the order item's line total.· medium

Solution

Opens when you add your repository link.

Finish up

Mark complete & continue

Next: Test structure: Arrange, Act, Assert