Unit Testing with TestNG

This training covers Unit Testing with TestNG.


  • Current release of the LDSTech IDE, or an IDE you are familiar with
  • Java JDK 1.6+

Slide Deck

Section 1: What is Unit Testing? What should I Test?


Lab 1

  • Download the TestNG project template
  • Unzip the artifact where you typically keep training or other projects
  • Open the testng-training folder as a workspace in your LDS Tech IDE
    • if you end up with nested folders... Unit_Testing_w_TestNG/testng-training, open the testng-training one as your workspace
    • NOTE: you can close the "Welcome" screen by clicking on the "X" in the upper-corner of the "Welcome" screen
  • Import the testng project
    • File -> Import
    • Expand Maven
    • Choose "Existing Maven Projects"
    • point it to ...testng-training/testng as the root directory
    • click finish and wait for it to complete (activity is shown in the lower right corner)
  • Complete the first unit test in MoneyClipTest using what you have learned so far about assertions
    • Open MoneyClipTest.
    • Add an assertion to the first test, getTotalEmptyWallet(), so that it verifies that an empty wallet returns a value of 0.00 from getTotal().
    • An assertion that would successfully test this could look like the following:
assertEquals(wallet.getTotal(), 0.00);
  • Test that the first test works by running it.
    • Right-click on the file and choose "run as" -> testNG test
    • verify that the test ran with zero failures
  • Complete the rest of the unit tests in MoneyClipTest
    • Add the necessary setup code and assertions to complete each test with a TODO comment in it.
    • After adding the code for each test, run the tests again to ensure that they are passing.
    • NOTE: you can obtain instances of PaperMoney by calling PaperMoney.aOneDollarBill(), PaperMoney.aFiveDollarBill(), and PaperMoney.aTenDollarBill() (if only it was that easy in real life!)
  • BONUS: Implement the getNumberOf(PaperMoney) method in MoneyClip.
    • Try to write tests first that verify/assert correct behavior.
    • Implement the method, fixing the method until it passes your tests.

Lab 1 Solution

Section 2: TestUtils, Testing for Exceptions and Temporal Values, Fixing Bugs


Lab 2

  • The Wallet.withdraw(amount) method has a bug in it. Fix the bug using unit tests.
    • Open WalletTest.
    • Review the code in the Wallet class. What could the bug be?
    • Once you have an idea of what the bug might be, write at least one test that demonstrates the issue.
    • Items to test might include:
      • adding money to the wallet in a certain order
      • trying to withdraw more money than is in the wallet
      • trying to withdraw an amount that the wallet cannot provide (such as six dollars when all the wallet has is a ten dollar bill)
      • trying to withdraw from an empty wallet
    • If the first test does not expose the issue, review the code, then add another test that might hit the bug.
  • Once you find the bug, fix the code.
    • Decide where (and how) you are going to fix the code. Apply your fix.
    • Run the unit tests to verify that you have fixed the issue.
    • Still not fixed? Tweak the code and re-run the test.

Lab 2 Solution

Section 3: Writing Clean and Maintainable Tests, Tips and Tricks


This page was last modified on 7 November 2012, at 12:48.

Note: Content found in this wiki may not always reflect official Church information. See Terms of Use.