Presentation is loading. Please wait.

Presentation is loading. Please wait.

MAHDI OMAR JUNIT TUTORIAL. CONTENTS Installation of Junit Eclipse support for Junit Using Junit exercise JUnit options Questions Links and Literature.

Similar presentations


Presentation on theme: "MAHDI OMAR JUNIT TUTORIAL. CONTENTS Installation of Junit Eclipse support for Junit Using Junit exercise JUnit options Questions Links and Literature."— Presentation transcript:

1 MAHDI OMAR JUNIT TUTORIAL

2 CONTENTS Installation of Junit Eclipse support for Junit Using Junit exercise JUnit options Questions Links and Literature

3 JUNIT A unit testing framework for the Java Programming Language Used for writing & running tests http://junit.org/ Versions: Junit3.*, Junit4.*

4 TERMINOLOGIES Test Case Part of code which ensures that the another part of code (method) works as expected Characterized by a known input (precondition ) and by an expected output (postcondition) Positive test case: verifies a correct functionality Negative test case: verifies that a function will fail or throw an exception Test Suite Contains test cases and run them together Can contain other test suites

5 INSTALLATION OF JUNIT Eclipse: JUnit integrated into Eclipse Downloading the JUnit library Download JUnit4.x.jar from http: //junit.org/ Add this library to your Java project and add it to the classpath Use the following command lines: To compile: javac “class path” To execute: java org.junit.runner.JUnitCore “test class name”

6 ECLIPSE SUPPORT FOR JUNIT Creating JUnit tests Manually JUnit test Case creation wizard Running JUnit tests Select Run as → JUnit Test

7 CREATING JUNIT TESTS

8 JUNIT EXAMPLE Rational numbers program Create a simple test case Use assertions Equals() method

9 ASSERT STATEMENTS StatementDescription 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.

10 JUNIT 4.X ANNOTATIONS AnnotationDescription @Test public void method() The @Test annotation identifies a method as a test method. @Test (expected = Exception.class)Fails, if the method does not throw the named exception. @Test(timeout=100)Fails, if the method takes longer than 100 milliseconds. @Before public void method() This method is executed before each test. It is used to can prepare the test environment (e.g. read input data, initialize the class). @After public void method() This method is executed after each test. It is used to cleanup the test environment (e.g. delete temporary data, restore defaults). It can also save memory by cleaning up expensive memory structures. @BeforeClass public static void method() This method is executed once, before the start of all tests. It is used to perform time intensive activities, for example to connect to a database. Methods annotated with this annotation need to be defined as static to work with JUnit. @AfterClass public static void method() This method is executed once, after all tests have been finished. It is 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. @IgnoreIgnores 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.

11 CREATING & RUNNING TEST SUITES Options of running multiple test suites: TestRunner Result TestSuite

12 HTTP://WWW.CS.COLOSTATE.EDU/~ELMAN/JUNITDEMO.HTML QUESTIONS? You can download today’s slides and materials from the link below THANK YOU!


Download ppt "MAHDI OMAR JUNIT TUTORIAL. CONTENTS Installation of Junit Eclipse support for Junit Using Junit exercise JUnit options Questions Links and Literature."

Similar presentations


Ads by Google