Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit Testing in Eclipse Presented by David Eisler 08/09/2014.

Similar presentations


Presentation on theme: "Unit Testing in Eclipse Presented by David Eisler 08/09/2014."— Presentation transcript:

1 Unit Testing in Eclipse Presented by David Eisler 08/09/2014

2 Agenda  Pre-Requirements  Unit Testing  JUnit  Multiply Demo  For a more comprehensive overview of Junit in Eclipse see: http://www.vogella.com/tutorials/JUnit/articl e.html http://www.vogella.com/tutorials/JUnit/articl e.html

3 Pre-Requirements  Eclipse. I am using Eclipse Standard 4.4 (Luna) for 64 bit version in this tutorial.  You can download Eclipse at: https://www.eclipse.org/downloads/ https://www.eclipse.org/downloads/  Eclipse will run on most Windows and Linux OS’s (See previous link)

4 What is Unit Testing  Testing bits of code in isolation with test code.

5 Why Do We Care about Unit Testing?  Regression testing  Allows you to make big changes to code quickly with confidence.  Documented proof of testing.  Ultimate goal is to save time and money in the long run by catching bugs sooner.

6 Cons of Unit Testing  All code is subject to bugs.  Big time investment. You have to write the tests and maintain them.  Additional Complexity. So is it worth it?

7 JUnit  JUnit in version 4.x (what we are using) is a test framework which uses annotations to identify methods that specify a test.

8 JUnit Annotations 1/2 AnnotationDesciption @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 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.

9 @BeforeClass public 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 marked with this annotation need to be defined as static to work with JUnit. @AfterClass public 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. JUnit Annotations 2/2

10 Assertions 1/2 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.

11 Assertions 2/2 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.

12 Multiply Demo Create a Java Class:

13 Create new Package called tests:  New>Package  Name new package tests  Right click new package tests>Build Path>Use as source folder

14 Create a JUnit test  Right click project  New>JUnit Test Case

15 Wizard allows you to auto-create method stubs:

16 Prompts to add Junit 4 library jar to build path:

17 Resulting Class:

18 testExceptionIsThrown & testMultiply

19 Run your test in Eclipse:  Right click test class>Run As>JUnit Test

20 Fix the error:

21 Passed!


Download ppt "Unit Testing in Eclipse Presented by David Eisler 08/09/2014."

Similar presentations


Ads by Google