Mini project: a ShopLite test plan and first tests
60 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)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.
$ 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
- 1
TEST-PLAN.mdin the repository root lists unit, integration and end-to-end tests for products, orders and security, and what already exists. - 2
mvn testruns every*Testclass andmvn verifyalso runs the*ITclasses against theshoplite_testdatabase. - 3
OrderTotalsTestis insrc/test/java/com/shoplite/orderand 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
BigDecimalwritten with the same scale as the result, such as1799.00. - 6One test is made to fail on purpose, the failure is read in the console and in
target/surefire-reports, and then fixed. - 7
mvn testends 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 oneWrite a one-page test plan
TEST-PLAN.mdlists the tests for products, orders and security at each pyramid level.Confirm the build runs the tests
mvn testshows 20 tests andmvn verifyshows 70 integration tests, all green.Unit test the line totals
OrderTotalsTesthas three green tests forOrderItem.lineTotal().Unit test the order total
Five green tests, including the total of two lines and an empty order.
Break one test on purpose, read the report, fix it
You have seen and read a real failure, and
mvn testends 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.
Add a test that an order with three lines of different quantities totals correctly, using
ACC-AD141at 1299.00.· easyAdd a
TEST-PLAN.mdrow for every endpoint group inAPI-DESIGN.mdand mark which ones have no end-to-end coverage yet.· mediumAdd a test that changing a product's price with
setPriceafter 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