SlideShare a Scribd company logo
UNITTESTING WITH JUNIT
Software Engineering Class
Valerio Maggio, Ph.D. Candidate
valerio.maggio@unina.it
Prof.Adriano Peron
May, 28th 2013
BRIEF INTROTOTHE CLASS
It’s about UnitTesting It’s about JUnit+
It’s about
Unit Testing
with JUnit
You say?!
DISCLAIMER
• At the end of the class you (should)...
1. ..have learnt something more about unit testing;
2. ..have learnt what is JUnit, how to use it and when;
3. ..have realized how much important are testing activities!
This is not a Tutorial Class
( maybe you already noticed
that slides are in English...)
JUNIT PRELIMINARIES
• Q: How many “types” of testing do you know?
• A: SystemTesting, IntegrationTesting, UnitTesting....
• Q: How many “testing techniques” do you know?
• A: Black Box and White BoxTesting
Which is the difference?
• Q:What type and technique do you think JUnit covers?
JUNIT WORDS CLOUD
a.k.a. some random words (almost) related to JUnit
UnitTesting
JavaTesting
Black BoxTesting
Testing framework
xUnit
Test Suite
Test Fixtures
Test RunnersSimpleTest Program
Testing Automation
JUNIT JUMPSTART
Never in the field of software
development was so much owed
by so many to so few lines of
code
Martin Fowler
THE IMPORTANCE OFTESTING
THE IMPORTANCE OFTESTING
• During development, the first thing we do is run our own
program
• This is (sometimes) called Acceptance Testing
Code Compile Run
Test/Debug
WHO ARETHESETWO GUYS?
Erich Gamma Kent Beck
Creators of the
xUnit
Framework
XUNIT FRAMEWORK
• A framework is a semi-complete application that provides a
reusable, common structure that can be shared between
applications.
• Developers incorporate the framework in their own
application an extend it to meet their specific needs.
• Unit Test:A unit test examines the behavior of a
distinct unit of work.
• The “distinct unit of work” is often (but not always) a
single method.
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
- .....
WHY A FRAMEWORK IS NEEDED?
Let’s do a very dummy example...
Q: How would you test this method?
VERY SIMPLETESTING STRATEGY
Q: How would you improve it?
IMPROVED (NAIVE) SOLUTION
LESSON LEARNED
ObjectiveTest + RepeatableTest = SimpleTest Program
Disclaimer:
The previous example
showed a naive way to test
(a.k.a. the wrong one)
That was not JUnit!!
JAVA UNITTESTING 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 fixtures for sharing common test data
• Test runners for running tests
JUNIT 3.X DESIGN RULES
• All theTest classes must extendTestCase
• Functionalities by inheritance
• All the test method's names must start with the “keyword”
test in order to be executed by the framework
• testSomething(...)
• testSomethingElse()
JUNITTEST EXAMPLE
JUNIT 4.X DESIGN
• Main features inspired from other Java UnitTesting Frameworks
• TestNG
• Test Method Annotations
• Requires Java5+ instead of Java 1.2+
• Main Method Annotations
• @Before, @After
• @Test, @Ignore
• @SuiteClasses, @RunWith
JUNITTEST ANNOTATIONS
• @Test public void method()
• Annotation @Test identifies 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()
JAVA ANNOTATIONS AT GLANCE
• Meta DataTagging
• 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
@TEST ANNOTATION
JUNIT ANNOTATIONS (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.
JUNIT 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 isTrue
• 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
TESTING EXCEPTION HANDLING
try-catch trick!
TESTINGTHE EXCEPTION HANDLING
THE NEW WAY!
Use the expected parameter of @Test annotation
TESTCALCULATOR JUNIT 4
TestAnnotation
JUnit Assert
THETEST FAILS!
(as expected)
IDE: IntelliJ IDEA 12 CE
http://www.jetbrains.com/idea/
REFERENCES 1/2
Professional Java JDK 5 Edition
Richardson et. al.,Wrox Publications 2006
JUnit in Action, 2nd Ed.
Massol et al. , Manning Pubs 2009
REFERENCES 2/2
Unit Test Frameworks
Tools for High-Quality Software Development
Paul Hamill, O’Reilly Media 2004
Kent Beck’s Original Testing
Framework Paper
http://www.xprogramming.com/testfram.htm

More Related Content

PDF
Test driven development - JUnit basics and best practices
PPTX
PPSX
PDF
Unit testing best practices
PDF
Unit testing with JUnit
PPTX
Introduction to JUnit
PPT
05 junit
PPTX
JUnit- A Unit Testing Framework
Test driven development - JUnit basics and best practices
Unit testing best practices
Unit testing with JUnit
Introduction to JUnit
05 junit
JUnit- A Unit Testing Framework

What's hot (20)

PPTX
Testing with Junit4
PPTX
Unit Testing with JUnit4 by Ravikiran Janardhana
PPTX
Java Unit Testing
PDF
Unit testing best practices with JUnit
PPTX
JUNit Presentation
PPT
PPT
Simple Unit Testing With Netbeans 6.1
PDF
Unit testing with JUnit
PPT
Unit testing
PPT
JUnit 4
PDF
PPTX
Introduction To J unit
PPT
Automated Unit Testing
PDF
Workshop unit test
PPS
JUnit Presentation
PPT
PPTX
JUnit 5 - The Next Generation of JUnit - Ted's Tool Time
PPTX
Unit testing
PPT
PPTX
Unit Testing in Java
Testing with Junit4
Unit Testing with JUnit4 by Ravikiran Janardhana
Java Unit Testing
Unit testing best practices with JUnit
JUNit Presentation
Simple Unit Testing With Netbeans 6.1
Unit testing with JUnit
Unit testing
JUnit 4
Introduction To J unit
Automated Unit Testing
Workshop unit test
JUnit Presentation
JUnit 5 - The Next Generation of JUnit - Ted's Tool Time
Unit testing
Unit Testing in Java
Ad

Viewers also liked (16)

PPT
Test Driven Development and JUnit
PPT
Tech talks #1- Unit testing and TDD
PDF
Unit testing and scaffolding
PDF
Improving Software Maintenance using Unsupervised Machine Learning techniques
ODP
Embrace Unit Testing
PPTX
JUnit 5 - from Lambda to Alpha and beyond
PDF
JUnit & Mockito, first steps
PDF
JUnit Kung Fu: Getting More Out of Your Unit Tests
PDF
Web frameworks
PDF
Unsupervised Machine Learning for clone detection
PDF
快钱移动网关支付接口文档 V3 0 3
PPTX
Apresentação1
PDF
Junit tutorial
PDF
Is this how you hate unit testing?
PPTX
PDF
JUnit PowerUp
Test Driven Development and JUnit
Tech talks #1- Unit testing and TDD
Unit testing and scaffolding
Improving Software Maintenance using Unsupervised Machine Learning techniques
Embrace Unit Testing
JUnit 5 - from Lambda to Alpha and beyond
JUnit & Mockito, first steps
JUnit Kung Fu: Getting More Out of Your Unit Tests
Web frameworks
Unsupervised Machine Learning for clone detection
快钱移动网关支付接口文档 V3 0 3
Apresentação1
Junit tutorial
Is this how you hate unit testing?
JUnit PowerUp
Ad

Similar to Unit testing with Junit (20)

PDF
junit-160729073220 eclipse software testing.pdf
PPTX
8-testing.pptx
PDF
JUnit with_mocking
PPT
3 j unit
PDF
JUnit Testing Framework A Complete Guide.pdf
PDF
JUnit Testing Framework A Complete Guide.pdf
PDF
Introduction To UnitTesting & JUnit
PPTX
unit 1 (1).pptx
PDF
Introduction to test automation in java and php
PPTX
Junit4&testng presentation
PPS
J unit presentation
DOCX
JUnit_Guide_Expanded_Presentation[1].docx
DOCX
JUnit_Guide_Expanded_Presentation[1].docx............................
DOCX
JUnit_Guide_Expanded_Presentation[1].docx
PPT
Junit Interview Questions-ppt
PDF
L08 Unit Testing
PPTX
The Test way
PPTX
Unit testing by Svetlin Nakov
junit-160729073220 eclipse software testing.pdf
8-testing.pptx
JUnit with_mocking
3 j unit
JUnit Testing Framework A Complete Guide.pdf
JUnit Testing Framework A Complete Guide.pdf
Introduction To UnitTesting & JUnit
unit 1 (1).pptx
Introduction to test automation in java and php
Junit4&testng presentation
J unit presentation
JUnit_Guide_Expanded_Presentation[1].docx
JUnit_Guide_Expanded_Presentation[1].docx............................
JUnit_Guide_Expanded_Presentation[1].docx
Junit Interview Questions-ppt
L08 Unit Testing
The Test way
Unit testing by Svetlin Nakov

More from Valerio Maggio (10)

PDF
Number Crunching in Python
PDF
Clone detection in Python
PDF
Machine Learning for Software Maintainability
PDF
LINSEN an efficient approach to split identifiers and expand abbreviations
PDF
A Tree Kernel based approach for clone detection
PDF
Refactoring: Improve the design of existing code
PDF
Scaffolding with JMock
PDF
Junit in action
PDF
Design patterns and Refactoring
PDF
Test Driven Development
Number Crunching in Python
Clone detection in Python
Machine Learning for Software Maintainability
LINSEN an efficient approach to split identifiers and expand abbreviations
A Tree Kernel based approach for clone detection
Refactoring: Improve the design of existing code
Scaffolding with JMock
Junit in action
Design patterns and Refactoring
Test Driven Development

Recently uploaded (20)

PDF
Encapsulation theory and applications.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
cuic standard and advanced reporting.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Empathic Computing: Creating Shared Understanding
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Modernizing your data center with Dell and AMD
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Machine learning based COVID-19 study performance prediction
PPT
Teaching material agriculture food technology
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Approach and Philosophy of On baking technology
Encapsulation theory and applications.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
cuic standard and advanced reporting.pdf
Electronic commerce courselecture one. Pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Understanding_Digital_Forensics_Presentation.pptx
MYSQL Presentation for SQL database connectivity
NewMind AI Monthly Chronicles - July 2025
Spectral efficient network and resource selection model in 5G networks
Empathic Computing: Creating Shared Understanding
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Modernizing your data center with Dell and AMD
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Machine learning based COVID-19 study performance prediction
Teaching material agriculture food technology
NewMind AI Weekly Chronicles - August'25 Week I
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Approach and Philosophy of On baking technology

Unit testing with Junit

  • 1. UNITTESTING WITH JUNIT Software Engineering Class Valerio Maggio, Ph.D. Candidate valerio.maggio@unina.it Prof.Adriano Peron May, 28th 2013
  • 2. BRIEF INTROTOTHE CLASS It’s about UnitTesting It’s about JUnit+ It’s about Unit Testing with JUnit You say?!
  • 3. DISCLAIMER • At the end of the class you (should)... 1. ..have learnt something more about unit testing; 2. ..have learnt what is JUnit, how to use it and when; 3. ..have realized how much important are testing activities! This is not a Tutorial Class ( maybe you already noticed that slides are in English...)
  • 4. JUNIT PRELIMINARIES • Q: How many “types” of testing do you know? • A: SystemTesting, IntegrationTesting, UnitTesting.... • Q: How many “testing techniques” do you know? • A: Black Box and White BoxTesting Which is the difference? • Q:What type and technique do you think JUnit covers?
  • 5. JUNIT WORDS CLOUD a.k.a. some random words (almost) related to JUnit UnitTesting JavaTesting Black BoxTesting Testing framework xUnit Test Suite Test Fixtures Test RunnersSimpleTest Program Testing Automation
  • 7. Never in the field of software development was so much owed by so many to so few lines of code Martin Fowler THE IMPORTANCE OFTESTING
  • 8. THE IMPORTANCE OFTESTING • During development, the first thing we do is run our own program • This is (sometimes) called Acceptance Testing Code Compile Run Test/Debug
  • 9. WHO ARETHESETWO GUYS? Erich Gamma Kent Beck Creators of the xUnit Framework
  • 10. XUNIT FRAMEWORK • A framework is a semi-complete application that provides a reusable, common structure that can be shared between applications. • Developers incorporate the framework in their own application an extend it to meet their specific needs. • Unit Test:A unit test examines the behavior of a distinct unit of work. • The “distinct unit of work” is often (but not always) a single method.
  • 11. 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 - .....
  • 12. WHY A FRAMEWORK IS NEEDED? Let’s do a very dummy example... Q: How would you test this method?
  • 13. VERY SIMPLETESTING STRATEGY Q: How would you improve it?
  • 15. LESSON LEARNED ObjectiveTest + RepeatableTest = SimpleTest Program Disclaimer: The previous example showed a naive way to test (a.k.a. the wrong one) That was not JUnit!!
  • 16. JAVA UNITTESTING 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 fixtures for sharing common test data • Test runners for running tests
  • 17. JUNIT 3.X DESIGN RULES • All theTest classes must extendTestCase • Functionalities by inheritance • All the test method's names must start with the “keyword” test in order to be executed by the framework • testSomething(...) • testSomethingElse()
  • 19. JUNIT 4.X DESIGN • Main features inspired from other Java UnitTesting Frameworks • TestNG • Test Method Annotations • Requires Java5+ instead of Java 1.2+ • Main Method Annotations • @Before, @After • @Test, @Ignore • @SuiteClasses, @RunWith
  • 20. JUNITTEST ANNOTATIONS • @Test public void method() • Annotation @Test identifies 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()
  • 21. JAVA ANNOTATIONS AT GLANCE • Meta DataTagging • 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
  • 23. JUNIT ANNOTATIONS (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.
  • 24. JUNIT 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 isTrue • 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
  • 26. TESTINGTHE EXCEPTION HANDLING THE NEW WAY! Use the expected parameter of @Test annotation
  • 28. THETEST FAILS! (as expected) IDE: IntelliJ IDEA 12 CE http://www.jetbrains.com/idea/
  • 29. REFERENCES 1/2 Professional Java JDK 5 Edition Richardson et. al.,Wrox Publications 2006 JUnit in Action, 2nd Ed. Massol et al. , Manning Pubs 2009
  • 30. REFERENCES 2/2 Unit Test Frameworks Tools for High-Quality Software Development Paul Hamill, O’Reilly Media 2004 Kent Beck’s Original Testing Framework Paper http://www.xprogramming.com/testfram.htm