ST Lab 01 2
ST Lab 01 2
Experiment 07
Introduction to white box testing using Junit in
eclipse
(Part 1)
JUnit promotes the idea of "first testing then coding", which emphasis on setting up the
test data for a piece of code which can be tested first and then can be implemented. This
approach is like "test a little, code a little, test a little, code a little..." which increases
programmer productivity and stability of program code that reduces programmer stress
and the time spent on debugging.
1.1Features
• JUnit is an open source framework which is used for writing & running tests.
• Provides Annotation to identify the test methods.
• Provides Assertions for testing expected results.
• Provides Test runners for running tests.
• JUnit tests allow you to write code faster with increasing quality
• JUnit is elegantly simple. It is less complex & takes less time.
• JUnit tests can be run automatically and they check their own results and
provide immediate feedback. There's no need to manually go through a report of
test results.
• JUnit tests can be organized into test suites containing test cases and even
other test suites.
• Junit shows test progress in a bar that is green if test is going fine and it turns red
when a test fails.
Eclipse
Ant
Meaven
2-Using JUnit
2.1. Unit testing with JUnit
JUnit is a test framework which uses annotations to identify methods that specify a test.
Typically these test methods are contained in a class which is only used for testing. It is
typically called a Test class.
The following code shows a JUnit test method . It can be created via
File → New → JUnit → JUnit Test case.
@Test
To write a test with JUnit you annotate a method with the @org.junit.Test annotation
and use a method provided by JUnit to check the expected result of the code execution
versus the actual result.
You can use the Eclipse user interface to run the test, via right-click on the test class
and selecting Run→ Run As → JUnit Test. Outside of Eclipse you can use
org.junit.runner.JUnitCore class to run the test. You can also mark a test method in a
class and select to run the test. Only the selected method will be executed.
2.2. Available JUnit annotations
The following table gives an overview of the available annotations in
JUnit
Table 1. Annotations
Annotation Description
@Ignore Ignores the test method. This is useful when the underlying
code has been changed and the test case has not yet been
adapted. Or if the execution time of this test is too long to be
included.
2.3. Assert statements
JUnit provides static methods in the Assert class to test for certain conditions. These
assertion methods typically start with assert and allow you to specify the error
message, the expected and the actual result. An assertion method compares the actual
value returned by a test to the expected value, and throws an AssertionException if the
comparison test fails.
The following table gives an overview of these methods. Parameters in [] brackets are
optional.
Statement Description
fail(String) Let the method fail. Might be used to check that a certain part
of the code is not reached. Or to have a failing test before the
test code is implemented. The String parameter is optional.
assert False([message], boolean condition) Checks that the boolean condition is false.
assertEquals([String message], expected, Tests that two values are the same. Note: for arrays the
actual) reference is checked not the content of the arrays.
assertEquals([String message], expected, Test that float or double values match. The tolerance is the
actual, tolerance) number of decimals which must be the same.
assertSame([String], expected, actual) Checks that both variables refer to the same object.
assertNotSame([String], expected, actual) Checks that both variables refer to different objects.
JUnit isn't part of the standard Java class libraries, but it does come included with
Eclipse.
/*
* This class prints the given message on console.
*/
publicclassHelloWorld {
privateStringmessage
;
//Constructor
//@param message to be printed
publicHelloWorld(String message){
this.message= message;
}
publicclass HelloWorldTest {
@Test
publicvoid testPrintMessage() {
assertEquals(message
,messageUtil
.printMessage());
}
}
Press the Green Play button in menu bar or Right Click->Select Run As-> JUnit Test.
Example 2:
publicclassCalculator {
publicint multi
ply(int value1,int value2) {
returnvalue1 * value2;
}
publicint subtract(
int value1,int value2) {
returnvalue1- value2;
}
publicint divide(
int value1,int value2) {
returnvalue1 / value2;
}
}
3. Now Right Click on class and Select New ->JUnit Test Case
4. In Option Name -> Specify the test class name and in Option Class under test -
>Specify the Class which you want to test(class calculator in this case).
5. Click Next -> New JUnit Test Case Dialog opens . You can select methods which you
want to test.
6. Click Next.
7. At this point Eclipse will ask whether you want it to automatically attach the JUnit
library to your project. Yes, you do. Select "Perform the following action: Add JUnit 4
library to the build path" and press OK.
8. Now it will show the Test Class CalculatorTest.java and the methods which you are
selected.
9. Fail(“Not yet implemented”) message in each test because you haven’t implemented
any method.
10. Now change the code until it matches the following code:
13. Test Methods testAdd and testDivide are pass while testSubtract and testMultiply
are fail.
LAB TASKS
1-Carefully examine the above code, correct the errors and then
again run the test class CalculatorTest.java until all the tests are
pass.