Upload
phamthuy
View
225
Download
0
Embed Size (px)
Citation preview
BME Department of Control Engineering and Information Technology Software laboratory 3. 2011.
1
Java JUnit laboratory
Author: Péter Budai, BME IIT, 2011.
The first five tasks must be solved by the end of the lesson.
Please read java_10_junit_tutorial.pdf before solving the tasks.
The description of the annotations of JUnit can be found here:
http://junit.sourceforge.net/javadoc/
The most frequently asked questions about JUnit are answered here (especially sections 4, 5
and 7):
http://junit.sourceforge.net/doc/faq/faq.htm
The following JUnit Tutorial shows and example for parameterized testing:
http://www.mkyong.com/tutorials/junit-tutorials/
1 Opening the project
Download the initial project (JUnitLab.zip) for this laboratory from the website of the subject. Start
Eclipse and select the File > Import... menu item, then choose Existing projects into workspace.
Browse for the downloaded archive then click Finish:
BME Department of Control Engineering and Information Technology Software laboratory 3. 2011.
2
The imported project contains four Java files and a .jar library. The Bank.java file contains the
interface of an imaginary bank. The bank supports opening an account, closing an account, getting
the balance, depositing and withdrawing a certain amount of money, and transferring money from
one account to another. The money transfer may be any amount. The deposit and withdraw amount
will be rounded by the bank to 100s.
The different operations throw exceptions (AccountNotExistsException,
NotEnoughFundsException), if the account number does not exist or there is not enough funds on
the account for the transaction. These exception classes and their common superclass
(BankExceptiont) are defined in the other Java sources.
The .jar library contains two different implementations (FirstNationalBank and GreatSavingsBank)
of the Bank interface. These two classes have to be tested.
The class diagram above summarizes all of the classes and interfaces. Make sure to carefully read the
Javadoc comments in the Bank.java source file, since all of the tests will be based on these
comments!
BME Department of Control Engineering and Information Technology Software laboratory 3. 2011.
3
2 Creating simple test cases
(See the 3rd Chapter of java_10_junit_tutorial.pdf)
Prepare the project for running JUnit tests on it! At first add the JUnit4 Library to the project, then
create a new source folder (test) for the test classes.
In this and in the upcoming tasks we will test the FirstNationalBank class. Ignore the
GreatSavingsBank class for a while.
Create a test class junitlab.bank.BankTest and add the following test methods for testing the
openAccount, the getBalance and the deposit operations of FirstNationalBank:
testOpenAccount: Open an account, test whether it really exists and the initial balance is
zero (0).
testUniqueAccount: Open some new accounts one after the other and test whether their
account numbers are different.
testInvalidAccount: Test whether the expected exception (AccountNotExistsException) is
thrown if getting the balance of a non-existent account.
testDeposit: Deposit 2000 HUF into a new account then check whether the amount on the
account is correct.
The test methods must not write to the standard output, use the static methods of org.junit.Assert
instead! Do not handle any exceptions, leave everything to the JUnit framework!
If you did everything correctly, three of the four test cases will pass:
What is the problem in the one that failed?
BME Department of Control Engineering and Information Technology Software laboratory 3. 2011.
4
3 Test fixtures
(See the 4th Chapter of java_10_junit_tutorial.pdf)
The next few tests will require precreated accounts. Create a new test class
(junitlab.bank.BankTestFixture), which creates the following test fixture before running each test
method:
Creates and stores in an attribute a Bank object (in this case an instance of
FirstNationalBank)
Using the Bank object it opens two accounts:
o the first account has 1500 HUF
o the second one has 12000 HUF
The account numbers must be stored as attributes so that the test methods can access
them.
After these in this new test class create the following test methods to test the transfer operation:
testTransfer: Transfer 3456 HUF from the second account to the first, then check whether
the accounts contain the appropriate amount (4956 and 8544 HUF).
testTransferWithoutEnoughFunds: Transfer 3456 HUF from the first account to the second
one and check whether the expected exception (NotEnoughFundsException) is thrown!
The test methods must not write to the standard output, use the static methods of org.junit.Assert
instead! Do not handle any exceptions, leave everything to the JUnit framework!
If you did everything correctly, three of the four test cases will pass:
BME Department of Control Engineering and Information Technology Software laboratory 3. 2011.
5
4 Code coverage
(See the 5th Chapter of java_10_junit_tutorial.pdf)
The code coverage test should also include the junitlab-impl.jar library. To achieve this, create a new
Coverage Configuration. Right click on the project name, then select Coverage As > Coverage
Configurations.... In the dialog box create a new Launch Configuration inside the JUnit group using
the button circled in red in the figure below. Give a name (JUnitLab) to the test configuration, and
select JUnit 4 as test runner:
On the Coverage tab under Instrumented classes tick the src and the test source folders, and also
the lib/junitlab-impl.jar library:
BME Department of Control Engineering and Information Technology Software laboratory 3. 2011.
6
Finally, click the Apply button to save the changes, then click Coverage to execute the
measurement!
What results did you get? Try to open the FirstNationalBank.class by doubleclicking on it in the
Coverage view! Since the source code is not provided for this class you shoud see the Source not
found message and a lot of JVM bytecode:
BME Department of Control Engineering and Information Technology Software laboratory 3. 2011.
7
5 Replace the implementation class
Modify the test classes created in tasks 2 (BankTest) and 3 (BankTestFixture) so that instead of
FirstNationalBank they test the GreatSavingsBank class!
Execute the Coverage Configuration created in task 4 again! If you did everything correctly, then this
class will pass all the tests. In addition, the source of this class is included in the junitlab-impl.jar, so
you will be able to inspect the tested source codes if you double click on the GreatSavingsBank.class
in the Coverage window:
6 Parameterized testing
(See the 6th Chapter of java_10_junit_tutorial.pdf)
Create a new test class junitlab.bank.BankParamTest, which is able to test the rounding algorithm
of the bank using parameterized tests! To do this, the test should receive two inputs: an unrounded
amount (amount) and a rounded value (rounded). Add a constructor to the test class so that it
accepts these two arguments and stores them in attributes!
Add the static function to the class which returns the list of the following values and their correct
rounded pairs:
1100, 1101, 1149, 1150, 1151, 1199, 1200
BME Department of Control Engineering and Information Technology Software laboratory 3. 2011.
8
Create a test method (testWithdrawRounding) in this new test class that tests the (withdraw)
operation of the banks! The method should create a new bank instance, inside the bank a new
account and it should place 10000 HUF on the account. Now it should try to withraw the unrounded
amount (amount) received by the constructor of the test case, and then check whether the balance
of the account was reduced by the rounded value (rounded).
The test methods must not write to the standard output, use the static methods of org.junit.Assert
instead! Do not handle any exceptions, leave everything to the JUnit framework!
Execute the test twice: once for an instance of the class FirstNationalBank then for an instance of
the class GreatSavingsBank! You should receive the following results:
7 Creating other missing test cases
Knowing the source code of the GreatSavingsBank class, create new test cases so that every
statement inside the GreatSavingsBank class should be covered by the coverage test (i.e. you should
reach full coverage, or nearly 100% coverage). Create new test classes and test methods freely, as
you wish.