junit: java testing framework
TRANSCRIPT
![Page 1: JUnit: Java Testing Framework](https://reader035.vdocuments.us/reader035/viewer/2022071601/613d3d71736caf36b75afb94/html5/thumbnails/1.jpg)
JUnit: Java Testing Framework
Course of Software Engineering IIA.A. 2011/2012
Valerio Maggio, PhD StudentProf. Marco Faella
![Page 2: JUnit: Java Testing Framework](https://reader035.vdocuments.us/reader035/viewer/2022071601/613d3d71736caf36b75afb94/html5/thumbnails/2.jpg)
2Outline
‣Brief Introduction to Junit 4.x
‣Main di!erences with JUnit 3.x
‣JUnit Examples in practice
‣Further Insights •(Extensions and compatibilities)
![Page 3: JUnit: Java Testing Framework](https://reader035.vdocuments.us/reader035/viewer/2022071601/613d3d71736caf36b75afb94/html5/thumbnails/3.jpg)
3JUnit Preliminaries
‣Q: How many “types” of testing do you know? A: System Testing, Integration Testing, Unit
Testing....
‣Q: How many “testing techniques” do you know? A: Black Box and White Box Testing
•Which is the di!erence?
‣Q: What type and technique do you think JUnit covers?
![Page 4: JUnit: Java Testing Framework](https://reader035.vdocuments.us/reader035/viewer/2022071601/613d3d71736caf36b75afb94/html5/thumbnails/4.jpg)
4
JUnit: Java Unit Testing framework
‣JUnit is a simple, open source framework to write and run repeatable tests. •It is an instance of the xUnit architecture for unit testing
frameworks.
•(source: http://junit.org)
‣JUnit features include:•Assertions for testing expected results
•Test "xtures for sharing common test data
•Test runners for running tests
‣Originally written by Erich Gamma and Kent Beck.
![Page 5: JUnit: Java Testing Framework](https://reader035.vdocuments.us/reader035/viewer/2022071601/613d3d71736caf36b75afb94/html5/thumbnails/5.jpg)
5
xUnit Design JUnit 3.x design was compliant with
xUnit framework guidelines
<<interface>>Test
TestCase TestSuite
MyTestCase
*• JUnit• CppUnit• PyUnit• NUnit• XMLUnit• PHPUnit• RUnit• SUnit• .....
![Page 6: JUnit: Java Testing Framework](https://reader035.vdocuments.us/reader035/viewer/2022071601/613d3d71736caf36b75afb94/html5/thumbnails/6.jpg)
6JUnit 3.x Design Rules
‣All the Test classes must extend TestCase•Functionalities by inheritance
‣All the test method's names must start withtest to be executed by the framework•TestSomething(...)•TestSomethingElse(...)
‣Let's do an example...
![Page 7: JUnit: Java Testing Framework](https://reader035.vdocuments.us/reader035/viewer/2022071601/613d3d71736caf36b75afb94/html5/thumbnails/7.jpg)
7
JUnit 3.x Typical Example
![Page 8: JUnit: Java Testing Framework](https://reader035.vdocuments.us/reader035/viewer/2022071601/613d3d71736caf36b75afb94/html5/thumbnails/8.jpg)
8JUnit 4.x Design
‣Main features inspired from other Java Unit Testing Frameworks•TestNG
‣Test Method Annotations•Requires Java5+ instead of Java 1.2+
‣Main Method Annotations•@Before, @After•@Test, @Ignore•@SuiteClasses, @RunWith
![Page 9: JUnit: Java Testing Framework](https://reader035.vdocuments.us/reader035/viewer/2022071601/613d3d71736caf36b75afb94/html5/thumbnails/9.jpg)
9Java5 Annotations at glance
‣Meta Data Tagging•java.lang.annotation•java.lang.annotation.ElementType
-FIELD-METHOD-CLASS-…
‣Target•Specify to which ElementType is applied
‣Retention•Specify how long annotation should be available
![Page 10: JUnit: Java Testing Framework](https://reader035.vdocuments.us/reader035/viewer/2022071601/613d3d71736caf36b75afb94/html5/thumbnails/10.jpg)
10
JUnit Test Annotation
![Page 11: JUnit: Java Testing Framework](https://reader035.vdocuments.us/reader035/viewer/2022071601/613d3d71736caf36b75afb94/html5/thumbnails/11.jpg)
11JUnit Testing Annotation (1)
‣@Test public void method()•Annotation @Test identi"es that this method is a
test method.
‣@Before public void method()•Will perform the method() before each test.
•This method can prepare the test environment
•E.g. read input data, initialize the class, ...
‣@After public void method()
![Page 12: JUnit: Java Testing Framework](https://reader035.vdocuments.us/reader035/viewer/2022071601/613d3d71736caf36b75afb94/html5/thumbnails/12.jpg)
12JUnit Testing Annotation (2)
‣@Ignore•Will ignore the test method
•E.g. Useful if the underlying code has been changed and the test has not yet been adapted.
‣@Test(expected=Exception.class)•Tests if the method throws the named exception.
‣@Test(timeout=100)•Fails if the method takes longer than 100 milliseconds.
![Page 13: JUnit: Java Testing Framework](https://reader035.vdocuments.us/reader035/viewer/2022071601/613d3d71736caf36b75afb94/html5/thumbnails/13.jpg)
13JUnit Assert Statements
‣assertNotNull([message], object)•Test passes if Object is not null.
‣assertNull([message], object)•Test passes if Object is null.
‣assertEquals([message],expected, actual)•Asserts equality of two values
‣assertTrue(true|false)•Test passes if condition is True
‣assertNotSame([message], expected, actual)•Test passes if the two Objects are not the same Object
‣assertSame([message], expected, actual)•Test passes if the two Objects are the same Object
![Page 14: JUnit: Java Testing Framework](https://reader035.vdocuments.us/reader035/viewer/2022071601/613d3d71736caf36b75afb94/html5/thumbnails/14.jpg)
14
Testing Exception Handling
Test anything that could possibly fail
![Page 15: JUnit: Java Testing Framework](https://reader035.vdocuments.us/reader035/viewer/2022071601/613d3d71736caf36b75afb94/html5/thumbnails/15.jpg)
15
New way of Testing exception handling
Test anything that could possibly fail
![Page 16: JUnit: Java Testing Framework](https://reader035.vdocuments.us/reader035/viewer/2022071601/613d3d71736caf36b75afb94/html5/thumbnails/16.jpg)
16
JUnit Example: TestCase and ClassUnderTest
![Page 17: JUnit: Java Testing Framework](https://reader035.vdocuments.us/reader035/viewer/2022071601/613d3d71736caf36b75afb94/html5/thumbnails/17.jpg)
17
JUnit Example: TestCase and ClassUnderTest
![Page 18: JUnit: Java Testing Framework](https://reader035.vdocuments.us/reader035/viewer/2022071601/613d3d71736caf36b75afb94/html5/thumbnails/18.jpg)
18
JUnit Example: Execution
![Page 19: JUnit: Java Testing Framework](https://reader035.vdocuments.us/reader035/viewer/2022071601/613d3d71736caf36b75afb94/html5/thumbnails/19.jpg)
Further Insights
![Page 20: JUnit: Java Testing Framework](https://reader035.vdocuments.us/reader035/viewer/2022071601/613d3d71736caf36b75afb94/html5/thumbnails/20.jpg)
20
JUnit 4.x backward compatibility
JUnit provides a façade class which operates with any of the test runners.org.junit.runner.JUnitCore
![Page 21: JUnit: Java Testing Framework](https://reader035.vdocuments.us/reader035/viewer/2022071601/613d3d71736caf36b75afb94/html5/thumbnails/21.jpg)
21JUnit 4.x Extensions
![Page 22: JUnit: Java Testing Framework](https://reader035.vdocuments.us/reader035/viewer/2022071601/613d3d71736caf36b75afb94/html5/thumbnails/22.jpg)
22JUnit Matchers: Hamcrest
‣Junit 4.4+ introduces matchers •Imported from Hamcrest project
•http://code.google.com/p/hamcrest/
‣Matchers improve testing code refactoring•Writing more and more tests assertion became hard
to read
•Remember:-Documentation purposes
‣Let's do an example …
![Page 23: JUnit: Java Testing Framework](https://reader035.vdocuments.us/reader035/viewer/2022071601/613d3d71736caf36b75afb94/html5/thumbnails/23.jpg)
23
Matchers Example
![Page 24: JUnit: Java Testing Framework](https://reader035.vdocuments.us/reader035/viewer/2022071601/613d3d71736caf36b75afb94/html5/thumbnails/24.jpg)
24
References 1/2Professional Java JDK 5 EditionRichardson et. al., Wrox Publications 2006
JUnit in Action, 2nd Ed.Massol et al. , Manning Pubs 2009
![Page 25: JUnit: Java Testing Framework](https://reader035.vdocuments.us/reader035/viewer/2022071601/613d3d71736caf36b75afb94/html5/thumbnails/25.jpg)
25
References 2/2
Unit Test FrameworksTools for High-Quality Software DevelopmentPaul Hamill, O’Reilly Media 2004
Kent Beck’s Original Testing Framework Paperhttp://www.xprogramming.com/testfram.htm