CSEN401 - JUnit Testing
CSEN401 - JUnit Testing
A unit test is a piece of code written by a developer that executes a specific functionality in the code
to be tested. The percentage of code which is tested by unit tests is typically called test coverage.
A unit test targets a small unit of code, e.g., a method or a class, (local tests).
Unit tests ensure that code works as intended. They are also very helpful to ensure that the code still
works as intended in case you need to modify code for fixing a bug or extending functionality. Having
a high test coverage of your code allows you to continue developing features without having to perform
lots of manual tests.
1 Using JUnit
1.1 The JUnit framework
JUnit in version 4.x is a test framework which uses annotations to identify methods that specify a test.
Typically a JUnit test is a method contained in a class which is only used for testing. This is called a
Test class.
To write a test with the JUnit 4.x framework you annotate a method with the @org.junit.Test annotation.
In this method you use a method provided by the JUnit framework to check the expected result of the
code execution versus the actual result.
We would like to make sure that the code does output Ḧello World.̈
import org.junit.*;
import static org.junit.Assert.*;
MyAddingAlgorithm instance;
@Before
public void setUp(){
instance= new MyAddingAlgorithm();
1
}
@Test
public void test1(){
assertEquals("The output should be 5", 5, instance.add(2,3));
}
@Test
public void test2(){
assertEquals("The output should be -6", -6, instance.add(-2,-4));
}
1.3 Annotation
JUnit 4.x uses annotations to specify what a method in the test class does. Annotation are used to mark
methods and to configure the test run. The following table gives an overview of the most important
available annotations.
Annotation Description
@Test public void The @Test annotation identifies a method as a test method.
method()
@Test (expected = Fails if the method does not throw the named exception.
Exception.class)
@Test(timeout=100) Fails if the method takes longer than 100 milliseconds.
@Before public void This method is executed before each test. It is used to prepare the test
method() environment (e.g., read input data, initialize the class).
@After public void This method is executed after each test. It is used to cleanup the test
method() environment (e.g., delete temporary data, restore defaults). It can also
save memory by cleaning up expensive memory structures.
@BeforeClass
public static void This method is executed once, before the start of all tests. It is used to
method() perform time intensive activities, for example, to connect to a database.
Methods marked with this annotation need to be defined as static to
work with JUnit.
@AfterClass
public static void This method is executed once, after all tests have been finished. It is
method() used to perform clean-up activities, for example, to disconnect from a
database. Methods annotated with this annotation need to be defined as
static to work with JUnit.
@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
Assertion 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.
assertTrue([message], boolean condition) Checks that the boolean condition is true.
assertFalse([message], boolean condition) Checks that the boolean condition is false.
assertEquals([String message], expected,
actual) Tests that two values are the same. Note: for arrays
the reference is checked not the content of the arrays.
assertEquals([String message], expected,
actual, tolerance) Test that float or double values match. The tolerance
is the number of decimals which must be the same.
assertNull([message], object) Checks that the object is null.
assertNotNull([message], object) Checks that the object is not null.
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.
• Right click on the project and choose the “Configure Build Path..” menu.
3
• Choose JUnit 4 from the drop down list and click finish