|
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
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.
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.
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.)
Note that
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.
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
©2007, Ian Barland, Radford University Last modified 2007.Sep.21 (Fri) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |