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

What's hot

Testing with Junit4
Testing with Junit4Testing with Junit4
Testing with Junit4
Amila Paranawithana
 
Unit Testing with JUnit4 by Ravikiran Janardhana
Unit Testing with JUnit4 by Ravikiran JanardhanaUnit Testing with JUnit4 by Ravikiran Janardhana
Unit Testing with JUnit4 by Ravikiran Janardhana
Ravikiran J
 
Java Unit Testing
Java Unit TestingJava Unit Testing
Java Unit Testing
Nayanda Haberty
 
Unit testing best practices with JUnit
Unit testing best practices with JUnitUnit testing best practices with JUnit
Unit testing best practices with JUnit
inTwentyEight Minutes
 
Junit
JunitJunit
Simple Unit Testing With Netbeans 6.1
Simple Unit Testing With Netbeans 6.1Simple Unit Testing With Netbeans 6.1
Simple Unit Testing With Netbeans 6.1
Kiki Ahmadi
 
Unit testing with JUnit
Unit testing with JUnitUnit testing with JUnit
Unit testing with JUnit
Pokpitch Patcharadamrongkul
 
Unit testing
Unit testingUnit testing
Unit testing
Murugesan Nataraj
 
JUnit 4
JUnit 4JUnit 4
JUnit 4
Sunil OS
 
JUnit 5
JUnit 5JUnit 5
Introduction To J unit
Introduction To J unitIntroduction To J unit
Introduction To J unitOlga Extone
 
Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit Testing
Mike Lively
 
Workshop unit test
Workshop   unit testWorkshop   unit test
Workshop unit test
Francesco Garavaglia
 
JUnit Presentation
JUnit PresentationJUnit Presentation
JUnit Presentation
priya_trivedi
 
J Unit
J UnitJ Unit
JUnit 5 - The Next Generation of JUnit - Ted's Tool Time
JUnit 5 - The Next Generation of JUnit - Ted's Tool TimeJUnit 5 - The Next Generation of JUnit - Ted's Tool Time
JUnit 5 - The Next Generation of JUnit - Ted's Tool Time
Ted Vinke
 
Unit testing
Unit testingUnit testing
Unit testing
Panos Pnevmatikatos
 
Unit Testing in Java
Unit Testing in JavaUnit Testing in Java
Unit Testing in Java
Ahmed M. Gomaa
 

What's hot (20)

Testing with Junit4
Testing with Junit4Testing with Junit4
Testing with Junit4
 
Unit Testing with JUnit4 by Ravikiran Janardhana
Unit Testing with JUnit4 by Ravikiran JanardhanaUnit Testing with JUnit4 by Ravikiran Janardhana
Unit Testing with JUnit4 by Ravikiran Janardhana
 
Java Unit Testing
Java Unit TestingJava Unit Testing
Java Unit Testing
 
Unit testing best practices with JUnit
Unit testing best practices with JUnitUnit testing best practices with JUnit
Unit testing best practices with JUnit
 
JUNit Presentation
JUNit PresentationJUNit Presentation
JUNit Presentation
 
Junit
JunitJunit
Junit
 
Simple Unit Testing With Netbeans 6.1
Simple Unit Testing With Netbeans 6.1Simple Unit Testing With Netbeans 6.1
Simple Unit Testing With Netbeans 6.1
 
Unit testing with JUnit
Unit testing with JUnitUnit testing with JUnit
Unit testing with JUnit
 
Unit testing
Unit testingUnit testing
Unit testing
 
JUnit 4
JUnit 4JUnit 4
JUnit 4
 
JUnit 5
JUnit 5JUnit 5
JUnit 5
 
Introduction To J unit
Introduction To J unitIntroduction To J unit
Introduction To J unit
 
Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit Testing
 
Workshop unit test
Workshop   unit testWorkshop   unit test
Workshop unit test
 
JUnit Presentation
JUnit PresentationJUnit Presentation
JUnit Presentation
 
J Unit
J UnitJ Unit
J Unit
 
JUnit 5 - The Next Generation of JUnit - Ted's Tool Time
JUnit 5 - The Next Generation of JUnit - Ted's Tool TimeJUnit 5 - The Next Generation of JUnit - Ted's Tool Time
JUnit 5 - The Next Generation of JUnit - Ted's Tool Time
 
Unit testing
Unit testingUnit testing
Unit testing
 
Junit
JunitJunit
Junit
 
Unit Testing in Java
Unit Testing in JavaUnit Testing in Java
Unit Testing in Java
 

Viewers also liked

Test Driven Development and JUnit
Test Driven Development and JUnitTest Driven Development and JUnit
Test Driven Development and JUnit
Somenath Mukhopadhyay
 
Tech talks #1- Unit testing and TDD
Tech talks #1- Unit testing and TDDTech talks #1- Unit testing and TDD
Tech talks #1- Unit testing and TDD
DUONG Trong Tan
 
Unit testing and scaffolding
Unit testing and scaffoldingUnit testing and scaffolding
Unit testing and scaffolding
Valerio Maggio
 
Improving Software Maintenance using Unsupervised Machine Learning techniques
Improving Software Maintenance using Unsupervised Machine Learning techniquesImproving Software Maintenance using Unsupervised Machine Learning techniques
Improving Software Maintenance using Unsupervised Machine Learning techniques
Valerio Maggio
 
Embrace Unit Testing
Embrace Unit TestingEmbrace Unit Testing
Embrace Unit Testing
alessiopace
 
JUnit 5 - from Lambda to Alpha and beyond
JUnit 5 - from Lambda to Alpha and beyondJUnit 5 - from Lambda to Alpha and beyond
JUnit 5 - from Lambda to Alpha and beyond
Sam Brannen
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first steps
Renato Primavera
 
JUnit Kung Fu: Getting More Out of Your Unit Tests
JUnit Kung Fu: Getting More Out of Your Unit TestsJUnit Kung Fu: Getting More Out of Your Unit Tests
JUnit Kung Fu: Getting More Out of Your Unit Tests
John Ferguson Smart Limited
 
Web frameworks
Web frameworksWeb frameworks
Web frameworks
Valerio Maggio
 
Unsupervised Machine Learning for clone detection
Unsupervised Machine Learning for clone detectionUnsupervised Machine Learning for clone detection
Unsupervised Machine Learning for clone detection
Valerio Maggio
 
快钱移动网关支付接口文档 V3 0 3
快钱移动网关支付接口文档 V3 0 3快钱移动网关支付接口文档 V3 0 3
快钱移动网关支付接口文档 V3 0 3
Sheng Pang
 
Apresentação1
Apresentação1Apresentação1
Apresentação1gabimenon
 
Is this how you hate unit testing?
Is this how you hate unit testing?Is this how you hate unit testing?
Is this how you hate unit testing?Steven Mak
 
JUnit Sample
JUnit SampleJUnit Sample
JUnit PowerUp
JUnit PowerUpJUnit PowerUp
JUnit PowerUp
James McGivern
 

Viewers also liked (16)

Test Driven Development and JUnit
Test Driven Development and JUnitTest Driven Development and JUnit
Test Driven Development and JUnit
 
Tech talks #1- Unit testing and TDD
Tech talks #1- Unit testing and TDDTech talks #1- Unit testing and TDD
Tech talks #1- Unit testing and TDD
 
Unit testing and scaffolding
Unit testing and scaffoldingUnit testing and scaffolding
Unit testing and scaffolding
 
Improving Software Maintenance using Unsupervised Machine Learning techniques
Improving Software Maintenance using Unsupervised Machine Learning techniquesImproving Software Maintenance using Unsupervised Machine Learning techniques
Improving Software Maintenance using Unsupervised Machine Learning techniques
 
Embrace Unit Testing
Embrace Unit TestingEmbrace Unit Testing
Embrace Unit Testing
 
JUnit 5 - from Lambda to Alpha and beyond
JUnit 5 - from Lambda to Alpha and beyondJUnit 5 - from Lambda to Alpha and beyond
JUnit 5 - from Lambda to Alpha and beyond
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first steps
 
JUnit Kung Fu: Getting More Out of Your Unit Tests
JUnit Kung Fu: Getting More Out of Your Unit TestsJUnit Kung Fu: Getting More Out of Your Unit Tests
JUnit Kung Fu: Getting More Out of Your Unit Tests
 
Web frameworks
Web frameworksWeb frameworks
Web frameworks
 
Unsupervised Machine Learning for clone detection
Unsupervised Machine Learning for clone detectionUnsupervised Machine Learning for clone detection
Unsupervised Machine Learning for clone detection
 
快钱移动网关支付接口文档 V3 0 3
快钱移动网关支付接口文档 V3 0 3快钱移动网关支付接口文档 V3 0 3
快钱移动网关支付接口文档 V3 0 3
 
Apresentação1
Apresentação1Apresentação1
Apresentação1
 
Junit tutorial
Junit tutorialJunit tutorial
Junit tutorial
 
Is this how you hate unit testing?
Is this how you hate unit testing?Is this how you hate unit testing?
Is this how you hate unit testing?
 
JUnit Sample
JUnit SampleJUnit Sample
JUnit Sample
 
JUnit PowerUp
JUnit PowerUpJUnit PowerUp
JUnit PowerUp
 

Similar to Unit testing with Junit

TestNG introduction
TestNG introductionTestNG introduction
TestNG introductionDenis Bazhin
 
Junit Interview Questions-ppt
Junit Interview Questions-pptJunit Interview Questions-ppt
Junit Interview Questions-ppt
Mayank Kumar
 
Test ng tutorial
Test ng tutorialTest ng tutorial
Test ng tutorial
Srikrishna k
 
Top 20 Junit interview questions for sdet
Top 20 Junit interview questions for sdetTop 20 Junit interview questions for sdet
Top 20 Junit interview questions for sdet
DevLabs Alliance
 
Introduction To UnitTesting & JUnit
Introduction To UnitTesting & JUnitIntroduction To UnitTesting & JUnit
Introduction To UnitTesting & JUnit
Mindfire Solutions
 
Introduction to unit testing in python
Introduction to unit testing in pythonIntroduction to unit testing in python
Introduction to unit testing in python
Anirudh
 
Presentation android JUnit
Presentation android JUnitPresentation android JUnit
Presentation android JUnit
Enrique López Mañas
 
unit 1 (1).pptx
unit 1 (1).pptxunit 1 (1).pptx
unit 1 (1).pptx
SumitKumar918321
 
testng
testngtestng
TestNG vs. JUnit4
TestNG vs. JUnit4TestNG vs. JUnit4
TestNG vs. JUnit4
Andrey Oleynik
 
Assessing Unit Test Quality
Assessing Unit Test QualityAssessing Unit Test Quality
Assessing Unit Test Qualityguest268ee8
 
TestNG Session presented in PB
TestNG Session presented in PBTestNG Session presented in PB
TestNG Session presented in PB
Abhishek Yadav
 
Unit Testing & TDD Training for Mobile Apps
Unit Testing & TDD Training for Mobile AppsUnit Testing & TDD Training for Mobile Apps
Unit Testing & TDD Training for Mobile Apps
Marcelo Busico
 
J unit presentation
J unit presentationJ unit presentation
J unit presentationPriya Sharma
 
8-testing.pptx
8-testing.pptx8-testing.pptx
8-testing.pptx
ssuserd0fdaa
 
How To Use Ignore Annotation In JUnit
How To Use Ignore Annotation In JUnitHow To Use Ignore Annotation In JUnit
How To Use Ignore Annotation In JUnit
BugRaptors
 
Break through e2e-testing
Break through e2e-testingBreak through e2e-testing
Break through e2e-testingtameemahmed5
 

Similar to Unit testing with Junit (20)

TestNG introduction
TestNG introductionTestNG introduction
TestNG introduction
 
Test ng
Test ngTest ng
Test ng
 
Junit Interview Questions-ppt
Junit Interview Questions-pptJunit Interview Questions-ppt
Junit Interview Questions-ppt
 
Test ng tutorial
Test ng tutorialTest ng tutorial
Test ng tutorial
 
Top 20 Junit interview questions for sdet
Top 20 Junit interview questions for sdetTop 20 Junit interview questions for sdet
Top 20 Junit interview questions for sdet
 
Introduction To UnitTesting & JUnit
Introduction To UnitTesting & JUnitIntroduction To UnitTesting & JUnit
Introduction To UnitTesting & JUnit
 
Introduction to unit testing in python
Introduction to unit testing in pythonIntroduction to unit testing in python
Introduction to unit testing in python
 
Junit
JunitJunit
Junit
 
Presentation android JUnit
Presentation android JUnitPresentation android JUnit
Presentation android JUnit
 
unit 1 (1).pptx
unit 1 (1).pptxunit 1 (1).pptx
unit 1 (1).pptx
 
testng
testngtestng
testng
 
TestNG vs. JUnit4
TestNG vs. JUnit4TestNG vs. JUnit4
TestNG vs. JUnit4
 
Assessing Unit Test Quality
Assessing Unit Test QualityAssessing Unit Test Quality
Assessing Unit Test Quality
 
TestNG Session presented in PB
TestNG Session presented in PBTestNG Session presented in PB
TestNG Session presented in PB
 
Unit Testing & TDD Training for Mobile Apps
Unit Testing & TDD Training for Mobile AppsUnit Testing & TDD Training for Mobile Apps
Unit Testing & TDD Training for Mobile Apps
 
J unit presentation
J unit presentationJ unit presentation
J unit presentation
 
Unit testing basic
Unit testing basicUnit testing basic
Unit testing basic
 
8-testing.pptx
8-testing.pptx8-testing.pptx
8-testing.pptx
 
How To Use Ignore Annotation In JUnit
How To Use Ignore Annotation In JUnitHow To Use Ignore Annotation In JUnit
How To Use Ignore Annotation In JUnit
 
Break through e2e-testing
Break through e2e-testingBreak through e2e-testing
Break through e2e-testing
 

More from Valerio Maggio

Number Crunching in Python
Number Crunching in PythonNumber Crunching in Python
Number Crunching in Python
Valerio Maggio
 
Clone detection in Python
Clone detection in PythonClone detection in Python
Clone detection in Python
Valerio Maggio
 
Machine Learning for Software Maintainability
Machine Learning for Software MaintainabilityMachine Learning for Software Maintainability
Machine Learning for Software Maintainability
Valerio Maggio
 
LINSEN an efficient approach to split identifiers and expand abbreviations
LINSEN an efficient approach to split identifiers and expand abbreviationsLINSEN an efficient approach to split identifiers and expand abbreviations
LINSEN an efficient approach to split identifiers and expand abbreviations
Valerio Maggio
 
A Tree Kernel based approach for clone detection
A Tree Kernel based approach for clone detectionA Tree Kernel based approach for clone detection
A Tree Kernel based approach for clone detection
Valerio Maggio
 
Refactoring: Improve the design of existing code
Refactoring: Improve the design of existing codeRefactoring: Improve the design of existing code
Refactoring: Improve the design of existing code
Valerio Maggio
 
Scaffolding with JMock
Scaffolding with JMockScaffolding with JMock
Scaffolding with JMock
Valerio Maggio
 
Junit in action
Junit in actionJunit in action
Junit in action
Valerio Maggio
 
Design patterns and Refactoring
Design patterns and RefactoringDesign patterns and Refactoring
Design patterns and Refactoring
Valerio Maggio
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Valerio Maggio
 

More from Valerio Maggio (10)

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

Recently uploaded

Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 

Recently uploaded (20)

Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 

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