RU beehive logo ITEC dept promo banner
ITEC 120
2007fall
ibarland,
jdymacek

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lab05b
Unit testing

Happy aproaching solstice! approaching solstice (Sunday morning … at 05:51)

For today's lab, we'll all follow along together. We'll learn about writing automated test suites, so that after writing a test case once, you can press a single button to run all existing tests.

Unit Testing

  1. Open your PizzaServer-with-fields, from last lab.
  2. Write a stub function for one of the tasks you hadn't yet finished -- say, spend. (What is the signature for spend? As for its test cases -- that's what we're about to do in the next few steps of today's lab.)
  3. If you don't already see a greyed-out “record test cases” panel on your screen, then you'll need to turn on BlueJ's test-case tool: To do this, select Options > Preferences… > Miscellaneous > Show Unit Testing Tools.
    (On Mac, the Preferences… item is under the main BlueJ menu.)
  4. Now right-click on your class: In addition to the options like compile and new PizzaServer(), there is now an option Create Test Class.
    Choose this option. This will create a khaki “unit-test” class which is attached to your primary class. (We won't edit the test class manually; instead, as we use BlueJ to record tests, BlueJ will put code inside this (currently empty) unit-test class.)
  5. Right-click on the test class, and select Create Test Method…. Give it a name, testSpend. (We might make several test suites, each stressing a certain feature or set-of-features of our program.)

    By convention, each test begins with the four letters “test”. This begins recording your actions, which can be replayed in the future.

  6. Go ahead and test your method: Create a new PizzaServer instance, and right-click on the red instance to call setBalance, setting the balance to (say) $100. Then, call spend, spending $25. How do we test if it worked? Well, call getBalance.

    As we've already seen, BlueJ will return the answer from our function (currently just an uninformative stub function). However there is also something new, since we are recording a test case: we can assert that the result should be equal to 75.0. That is, we get a chance to enter what the intended result is. Always use the equal to assertion. (For doubles, use equal to (float or double). doubles require this special comparison, since we need to allow a small amount of tolerance, to compensate for round-off error.)

  7. Still recording, run a couple more test cases: call spend again with 0, then check the balance is still as expected, then call spend with (say) the remaining $75.00, and check that the balance is now zero.
    What if we try to spend into a negative balance -- should this be allowed? Make a test case, and you get to assert what the intended answer should be!
  8. When you think you have a sufficient set of tests for spend, go ahead and click to End recording.
  9. Now, whenever you want to run that entire suite of tests, you can click Run Tests. Try it now. Presumably your tests will fail (at least, most of them).
  10. Let's go write the function! Once you write it, click on Run Tests, and if everything is green, then you're happy!
  11. Note that

  12. Run your test cases. Your old testIsSecure should still pass, but your new testMakeSecureVersion had better fail, as our stub function doesn't do any actual work yet.

Optional
When done with the tutorial, look inside the unit-test file which BlueJ created for you. You will find the actual Java code corresponding to your creating a new object and calling a method and testing against various outcomes.
Other IDEs may not have BlueJ's “record” feature, but they do provide other tools to help you write the unit-testing code you see inside that file.

Here's a different tutorial you can look at, if you like: Unit Testing with BlueJ tutorial.

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


©2007, Ian Barland, Radford University
Last modified 2007.Sep.21 (Fri)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme