SlideShare a Scribd company logo
1 of 28
Download to read offline
1
‫ر‬َ‫ـد‬ْ‫ق‬‫ِـ‬‫ن‬،،،‫لما‬‫اننا‬ ‫نصدق‬ْْ‫ق‬ِ‫ن‬‫ر‬َ‫د‬
2
 In traditional testing,
 Code
 Compile
 Test!
Code Compile Test!
3
 Test-Driven Development (TDD) is a software
development process that relies on the repetition of a
very short development cycle:
 first the developer writes an (initially failing test) automated
test case that defines a desired improvement or new function,
 then produces the minimum amount of code to pass that test,
and
 finally refactors the new code to acceptable standards.
 A test is not something you “do”, it is something you
“write” and
 run once, twice, three times, etc.
 It is a piece of code
 Testing is therefore “automated”
 Repeatedly executed, even after small changes
4
1. Write a single test.
2. Compile it. It should not compile
because you have not written the
implementation code
3. Implement just enough code to get
the test to compile
4. Run the test and see it fail
5. Implement just enough code to get
the test to pass
6. Run the test and see it pass
7. Refactor for clarity and “once and
only once”
8. Repeat
5
 Context of Testing:
 Valid inputs
 Invalid inputs
 Errors, exceptions, and events
 Boundary conditions
 Everything that might break
 Testing Framework (Xunit)
 A testing framework is used so that automated testing can
be done after every small change to the code
• This may be as often as every 5 or 10 minutes
6
 Programmers dislike testing
 They will test reasonably thoroughly the first time
 The second time however, testing is usually less thorough
 The third time, well..
 Testing is considered a “boring” task
 Testing might be the job of another department/person
 TDD encourages programmers to maintain an exhaustive
set of repeatable tests
 Tests live alongside the Class/Code Under Test (CUT)
 With tool support, tests can be run selectively
 The tests can be run after every single change
7
 Writing clear requirements.
 Code proven to meet requirements
 Development in small steps (Shorter development cycles).
 The debugging will easier since we will have small code chunks to debug.
 Minimalistic code and enforce the YAGNI “You Aren’t Gonna Need It”
principle
 It is a principle of extreme programming (XP) that states
a programmer should not add functionality until deemed necessary.
 Catch bugs before they are shipped to your customer
 No code without tests
 Tests determine the code
 TDD negates fear
 Fear makes developers communicate less
 Fear makes developers avoid repeatedly testing code
• Afraid of negative feedback
 TDD allows us to refactor, or change the implementation of a class,
without the fear of breaking it
 TDD and refactoring go hand-in-hand
8
 JUnit is a unit testing framework for Java programming
language. It plays a crucial role test-driven
development.
 JUnit is a framework for writing tests
 Written by Erich Gamma (of Design Patterns fame) and
Kent Beck (creator of XP methodology)
 Uses Java features such as annotations and static imports
 JUnit helps the programmer:
• define and execute tests and test suites
• formalize requirements
• write and debug code
• integrate code and always be ready to release a working
version
9
 A test fixture sets up the data (both objects and
primitives) that are needed for every test
 Example: If you are testing code that updates an employee
record, you need an employee record to test it on
 A unit test is a test of a single class
 A test case tests the response of a single method to a
particular set of inputs
 A test suite is a collection of test cases
 A test runner is software that runs tests and reports
results
10
 To test a class named Fraction
 Create a test class FractionTest
11
 Methods annotated with @Before will execute before
every test case
 Methods annotated with @After will execute after every
test case
12
 Methods annotated with @BeforeClass will execute once
before all test cases
 Methods annotated with @AfterClass will execute once
after all test cases
 These are useful if you need to allocate and release
expensive resources once
13
 Methods annotated with @Test are considered to be test
cases
14
 For each test case t:
 JUnit executes all @Before methods
• Their order of execution is not specified
 JUnit executes t
• Any exceptions during its execution are logged
 JUnit executes all @After methods
• Their order of execution is not specified
 A report for all test cases is presented
15
 Call the methods of the class being tested
 Assert what the correct result should be with one of the
provided assert methods
 These steps can be repeated as many times as necessary
 An assert method is a JUnit method that performs a
test, and throws an AssertionError if the test fails
 JUnit catches these exceptions and shows you the results
16
 assertTrue(boolean b)
assertTrue(String s, boolean b)
 Throws an AssertionError if b is False
 The optional message s is included in the Error
 assertFalse(boolean b)
assertFalse(String s, boolean b)
 Throws an AssertionError if b is True
 All assert methods have an optional message
17
 assertEquals(Object expected, Object actual)
 Uses the equals method to compare the two objects
 Primitives can be passed as arguments.
 Casting may be required for primitives.
 There is also a version to compare arrays.
18
 assertSame(Object expected, Object actual)
 Asserts that two references are attached to the same
object (using ==)
 assertNotSame(Object expected, Object actual)
 Asserts that two references are not attached to the same
object
19
 assertNull(Object object)
 Asserts that a reference is null
 assertNotNull(Object object)
 Asserts that a reference is not null
 fail()
 Causes the test to fail and throw an AssertionError
• Useful as a result of a complex test, or when testing for
exceptions
20
 If a test case is expected to raise an exception, it can be
noted as follows
21
 A statement such as
assert boolean_condition;
 will also throw an AssertionError if the boolean_condition
is false
 Can be used instead of the Junit assertTrue method
22
 Test cases that are not finished yet can be annotated
with @Ignore
 JUnit will not execute the test case but will report how
many test cases are being ignored
23
 To demonstrate the power of JUnit let us create the
simple Java class and write several test cases for it.
 The class named MathFunc has two methods: factorial
of non-negative number and sum of two integer
numbers. Also, there is a counter of method calls just to
make the class more complex.
24
MathFunc
 The source code of the class is shown below.
25
MathFuncTest
 In order to create Unit test cases we need to create the class with
some methods. Of course the class could have auxiliary methods.
26
MathFuncTest (Cont.)
27
JUnitCore
 The most modern method to programmatically run tests
is JUnitCore. Just add the following method to the
MathFuncTest class:
 And the execution result is:
28

More Related Content

What's hot

Control Flow Testing
Control Flow TestingControl Flow Testing
Control Flow TestingHirra Sultan
 
Unit 3 Control Flow Testing
Unit 3   Control Flow TestingUnit 3   Control Flow Testing
Unit 3 Control Flow Testingravikhimani
 
WHITE BOX & BLACK BOX TESTING IN DATABASE
WHITE BOX & BLACK BOXTESTING IN DATABASEWHITE BOX & BLACK BOXTESTING IN DATABASE
WHITE BOX & BLACK BOX TESTING IN DATABASESalman Memon
 
White Box Testing
White Box TestingWhite Box Testing
White Box TestingAlisha Roy
 
Black Box Testing
Black Box TestingBlack Box Testing
Black Box TestingTestbytes
 
White boxvsblackbox
White boxvsblackboxWhite boxvsblackbox
White boxvsblackboxsanerjjd
 
Object oriented testing
Object oriented testingObject oriented testing
Object oriented testingHaris Jamil
 
Se (techniques for black box testing ppt)
Se (techniques for black box testing ppt)Se (techniques for black box testing ppt)
Se (techniques for black box testing ppt)Mani Kanth
 
Structural and functional testing
Structural and functional testingStructural and functional testing
Structural and functional testingHimanshu
 
White box & Black box testing
White box & Black box testingWhite box & Black box testing
White box & Black box testingNitishMhaske1
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesDerek Smith
 
Equivalence class testing
Equivalence  class testingEquivalence  class testing
Equivalence class testingMani Kanth
 

What's hot (20)

Control Flow Testing
Control Flow TestingControl Flow Testing
Control Flow Testing
 
Unit 3 Control Flow Testing
Unit 3   Control Flow TestingUnit 3   Control Flow Testing
Unit 3 Control Flow Testing
 
Unit testing
Unit testingUnit testing
Unit testing
 
WHITE BOX & BLACK BOX TESTING IN DATABASE
WHITE BOX & BLACK BOXTESTING IN DATABASEWHITE BOX & BLACK BOXTESTING IN DATABASE
WHITE BOX & BLACK BOX TESTING IN DATABASE
 
White box testing
White box testingWhite box testing
White box testing
 
Black Box Testing
Black Box TestingBlack Box Testing
Black Box Testing
 
unit testing and debugging
unit testing and debuggingunit testing and debugging
unit testing and debugging
 
Black & White Box testing
Black & White Box testingBlack & White Box testing
Black & White Box testing
 
White Box Testing
White Box TestingWhite Box Testing
White Box Testing
 
Black Box Testing
Black Box TestingBlack Box Testing
Black Box Testing
 
White boxvsblackbox
White boxvsblackboxWhite boxvsblackbox
White boxvsblackbox
 
Object oriented testing
Object oriented testingObject oriented testing
Object oriented testing
 
Black box software testing
Black box software testingBlack box software testing
Black box software testing
 
Introduction to White box testing
Introduction to White box testingIntroduction to White box testing
Introduction to White box testing
 
Se (techniques for black box testing ppt)
Se (techniques for black box testing ppt)Se (techniques for black box testing ppt)
Se (techniques for black box testing ppt)
 
Structural and functional testing
Structural and functional testingStructural and functional testing
Structural and functional testing
 
debugging and testing
debugging and testingdebugging and testing
debugging and testing
 
White box & Black box testing
White box & Black box testingWhite box & Black box testing
White box & Black box testing
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best Practices
 
Equivalence class testing
Equivalence  class testingEquivalence  class testing
Equivalence class testing
 

Similar to SE2_Lec 21_ TDD and Junit

SE2018_Lec 20_ Test-Driven Development (TDD)
SE2018_Lec 20_ Test-Driven Development (TDD)SE2018_Lec 20_ Test-Driven Development (TDD)
SE2018_Lec 20_ Test-Driven Development (TDD)Amr E. Mohamed
 
J unit presentation
J unit presentationJ unit presentation
J unit presentationPriya Sharma
 
Assessing Unit Test Quality
Assessing Unit Test QualityAssessing Unit Test Quality
Assessing Unit Test Qualityguest268ee8
 
05 junit
05 junit05 junit
05 junitmha4
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Developmentguestc8093a6
 
Getting started with Test Driven Development
Getting started with Test Driven DevelopmentGetting started with Test Driven Development
Getting started with Test Driven DevelopmentFerdous Mahmud Shaon
 
Quality Software With Unit Test
Quality Software With Unit TestQuality Software With Unit Test
Quality Software With Unit Testalice yang
 
Test driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + EclipseTest driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + EclipseUTC Fire & Security
 
Getting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud ShaonGetting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud ShaonCefalo
 

Similar to SE2_Lec 21_ TDD and Junit (20)

SE2018_Lec 20_ Test-Driven Development (TDD)
SE2018_Lec 20_ Test-Driven Development (TDD)SE2018_Lec 20_ Test-Driven Development (TDD)
SE2018_Lec 20_ Test-Driven Development (TDD)
 
unit 1 (1).pptx
unit 1 (1).pptxunit 1 (1).pptx
unit 1 (1).pptx
 
Workshop unit test
Workshop   unit testWorkshop   unit test
Workshop unit test
 
J unit presentation
J unit presentationJ unit presentation
J unit presentation
 
JUnit Presentation
JUnit PresentationJUnit Presentation
JUnit Presentation
 
Assessing Unit Test Quality
Assessing Unit Test QualityAssessing Unit Test Quality
Assessing Unit Test Quality
 
05 junit
05 junit05 junit
05 junit
 
Why Unit Testingl
Why Unit TestinglWhy Unit Testingl
Why Unit Testingl
 
Why unit testingl
Why unit testinglWhy unit testingl
Why unit testingl
 
Why Unit Testingl
Why Unit TestinglWhy Unit Testingl
Why Unit Testingl
 
Unit testing
Unit testingUnit testing
Unit testing
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Unit Testing in Java
Unit Testing in JavaUnit Testing in Java
Unit Testing in Java
 
Getting started with Test Driven Development
Getting started with Test Driven DevelopmentGetting started with Test Driven Development
Getting started with Test Driven Development
 
Quality Software With Unit Test
Quality Software With Unit TestQuality Software With Unit Test
Quality Software With Unit Test
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
TDD Best Practices
TDD Best PracticesTDD Best Practices
TDD Best Practices
 
Test driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + EclipseTest driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + Eclipse
 
Getting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud ShaonGetting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud Shaon
 
8-testing.pptx
8-testing.pptx8-testing.pptx
8-testing.pptx
 

More from Amr E. Mohamed

Dsp 2018 foehu - lec 10 - multi-rate digital signal processing
Dsp 2018 foehu - lec 10 - multi-rate digital signal processingDsp 2018 foehu - lec 10 - multi-rate digital signal processing
Dsp 2018 foehu - lec 10 - multi-rate digital signal processingAmr E. Mohamed
 
Dcs lec03 - z-analysis of discrete time control systems
Dcs   lec03 - z-analysis of discrete time control systemsDcs   lec03 - z-analysis of discrete time control systems
Dcs lec03 - z-analysis of discrete time control systemsAmr E. Mohamed
 
Dcs lec02 - z-transform
Dcs   lec02 - z-transformDcs   lec02 - z-transform
Dcs lec02 - z-transformAmr E. Mohamed
 
Dcs lec01 - introduction to discrete-time control systems
Dcs   lec01 - introduction to discrete-time control systemsDcs   lec01 - introduction to discrete-time control systems
Dcs lec01 - introduction to discrete-time control systemsAmr E. Mohamed
 
DDSP_2018_FOEHU - Lec 10 - Digital Signal Processing Applications
DDSP_2018_FOEHU - Lec 10 - Digital Signal Processing ApplicationsDDSP_2018_FOEHU - Lec 10 - Digital Signal Processing Applications
DDSP_2018_FOEHU - Lec 10 - Digital Signal Processing ApplicationsAmr E. Mohamed
 
DSP_2018_FOEHU - Lec 07 - IIR Filter Design
DSP_2018_FOEHU - Lec 07 - IIR Filter DesignDSP_2018_FOEHU - Lec 07 - IIR Filter Design
DSP_2018_FOEHU - Lec 07 - IIR Filter DesignAmr E. Mohamed
 
DSP_2018_FOEHU - Lec 06 - FIR Filter Design
DSP_2018_FOEHU - Lec 06 - FIR Filter DesignDSP_2018_FOEHU - Lec 06 - FIR Filter Design
DSP_2018_FOEHU - Lec 06 - FIR Filter DesignAmr E. Mohamed
 
SE2018_Lec-22_-Continuous-Integration-Tools
SE2018_Lec-22_-Continuous-Integration-ToolsSE2018_Lec-22_-Continuous-Integration-Tools
SE2018_Lec-22_-Continuous-Integration-ToolsAmr E. Mohamed
 
SE2018_Lec 21_ Software Configuration Management (SCM)
SE2018_Lec 21_ Software Configuration Management (SCM)SE2018_Lec 21_ Software Configuration Management (SCM)
SE2018_Lec 21_ Software Configuration Management (SCM)Amr E. Mohamed
 
SE2018_Lec 18_ Design Principles and Design Patterns
SE2018_Lec 18_ Design Principles and Design PatternsSE2018_Lec 18_ Design Principles and Design Patterns
SE2018_Lec 18_ Design Principles and Design PatternsAmr E. Mohamed
 
Selenium - Introduction
Selenium - IntroductionSelenium - Introduction
Selenium - IntroductionAmr E. Mohamed
 
SE2018_Lec 19_ Software Testing
SE2018_Lec 19_ Software TestingSE2018_Lec 19_ Software Testing
SE2018_Lec 19_ Software TestingAmr E. Mohamed
 
DSP_2018_FOEHU - Lec 08 - The Discrete Fourier Transform
DSP_2018_FOEHU - Lec 08 - The Discrete Fourier TransformDSP_2018_FOEHU - Lec 08 - The Discrete Fourier Transform
DSP_2018_FOEHU - Lec 08 - The Discrete Fourier TransformAmr E. Mohamed
 
DSP_2018_FOEHU - Lec 05 - Digital Filters
DSP_2018_FOEHU - Lec 05 - Digital FiltersDSP_2018_FOEHU - Lec 05 - Digital Filters
DSP_2018_FOEHU - Lec 05 - Digital FiltersAmr E. Mohamed
 
DSP_2018_FOEHU - Lec 04 - The z-Transform
DSP_2018_FOEHU - Lec 04 - The z-TransformDSP_2018_FOEHU - Lec 04 - The z-Transform
DSP_2018_FOEHU - Lec 04 - The z-TransformAmr E. Mohamed
 
DSP_2018_FOEHU - Lec 03 - Discrete-Time Signals and Systems
DSP_2018_FOEHU - Lec 03 - Discrete-Time Signals and SystemsDSP_2018_FOEHU - Lec 03 - Discrete-Time Signals and Systems
DSP_2018_FOEHU - Lec 03 - Discrete-Time Signals and SystemsAmr E. Mohamed
 
DSP_2018_FOEHU - Lec 02 - Sampling of Continuous Time Signals
DSP_2018_FOEHU - Lec 02 - Sampling of Continuous Time SignalsDSP_2018_FOEHU - Lec 02 - Sampling of Continuous Time Signals
DSP_2018_FOEHU - Lec 02 - Sampling of Continuous Time SignalsAmr E. Mohamed
 
SE2018_Lec 15_ Software Design
SE2018_Lec 15_ Software DesignSE2018_Lec 15_ Software Design
SE2018_Lec 15_ Software DesignAmr E. Mohamed
 
DSP_2018_FOEHU - Lec 1 - Introduction to Digital Signal Processing
DSP_2018_FOEHU - Lec 1 - Introduction to Digital Signal ProcessingDSP_2018_FOEHU - Lec 1 - Introduction to Digital Signal Processing
DSP_2018_FOEHU - Lec 1 - Introduction to Digital Signal ProcessingAmr E. Mohamed
 

More from Amr E. Mohamed (20)

Dsp 2018 foehu - lec 10 - multi-rate digital signal processing
Dsp 2018 foehu - lec 10 - multi-rate digital signal processingDsp 2018 foehu - lec 10 - multi-rate digital signal processing
Dsp 2018 foehu - lec 10 - multi-rate digital signal processing
 
Dcs lec03 - z-analysis of discrete time control systems
Dcs   lec03 - z-analysis of discrete time control systemsDcs   lec03 - z-analysis of discrete time control systems
Dcs lec03 - z-analysis of discrete time control systems
 
Dcs lec02 - z-transform
Dcs   lec02 - z-transformDcs   lec02 - z-transform
Dcs lec02 - z-transform
 
Dcs lec01 - introduction to discrete-time control systems
Dcs   lec01 - introduction to discrete-time control systemsDcs   lec01 - introduction to discrete-time control systems
Dcs lec01 - introduction to discrete-time control systems
 
DDSP_2018_FOEHU - Lec 10 - Digital Signal Processing Applications
DDSP_2018_FOEHU - Lec 10 - Digital Signal Processing ApplicationsDDSP_2018_FOEHU - Lec 10 - Digital Signal Processing Applications
DDSP_2018_FOEHU - Lec 10 - Digital Signal Processing Applications
 
DSP_2018_FOEHU - Lec 07 - IIR Filter Design
DSP_2018_FOEHU - Lec 07 - IIR Filter DesignDSP_2018_FOEHU - Lec 07 - IIR Filter Design
DSP_2018_FOEHU - Lec 07 - IIR Filter Design
 
DSP_2018_FOEHU - Lec 06 - FIR Filter Design
DSP_2018_FOEHU - Lec 06 - FIR Filter DesignDSP_2018_FOEHU - Lec 06 - FIR Filter Design
DSP_2018_FOEHU - Lec 06 - FIR Filter Design
 
SE2018_Lec 17_ Coding
SE2018_Lec 17_ CodingSE2018_Lec 17_ Coding
SE2018_Lec 17_ Coding
 
SE2018_Lec-22_-Continuous-Integration-Tools
SE2018_Lec-22_-Continuous-Integration-ToolsSE2018_Lec-22_-Continuous-Integration-Tools
SE2018_Lec-22_-Continuous-Integration-Tools
 
SE2018_Lec 21_ Software Configuration Management (SCM)
SE2018_Lec 21_ Software Configuration Management (SCM)SE2018_Lec 21_ Software Configuration Management (SCM)
SE2018_Lec 21_ Software Configuration Management (SCM)
 
SE2018_Lec 18_ Design Principles and Design Patterns
SE2018_Lec 18_ Design Principles and Design PatternsSE2018_Lec 18_ Design Principles and Design Patterns
SE2018_Lec 18_ Design Principles and Design Patterns
 
Selenium - Introduction
Selenium - IntroductionSelenium - Introduction
Selenium - Introduction
 
SE2018_Lec 19_ Software Testing
SE2018_Lec 19_ Software TestingSE2018_Lec 19_ Software Testing
SE2018_Lec 19_ Software Testing
 
DSP_2018_FOEHU - Lec 08 - The Discrete Fourier Transform
DSP_2018_FOEHU - Lec 08 - The Discrete Fourier TransformDSP_2018_FOEHU - Lec 08 - The Discrete Fourier Transform
DSP_2018_FOEHU - Lec 08 - The Discrete Fourier Transform
 
DSP_2018_FOEHU - Lec 05 - Digital Filters
DSP_2018_FOEHU - Lec 05 - Digital FiltersDSP_2018_FOEHU - Lec 05 - Digital Filters
DSP_2018_FOEHU - Lec 05 - Digital Filters
 
DSP_2018_FOEHU - Lec 04 - The z-Transform
DSP_2018_FOEHU - Lec 04 - The z-TransformDSP_2018_FOEHU - Lec 04 - The z-Transform
DSP_2018_FOEHU - Lec 04 - The z-Transform
 
DSP_2018_FOEHU - Lec 03 - Discrete-Time Signals and Systems
DSP_2018_FOEHU - Lec 03 - Discrete-Time Signals and SystemsDSP_2018_FOEHU - Lec 03 - Discrete-Time Signals and Systems
DSP_2018_FOEHU - Lec 03 - Discrete-Time Signals and Systems
 
DSP_2018_FOEHU - Lec 02 - Sampling of Continuous Time Signals
DSP_2018_FOEHU - Lec 02 - Sampling of Continuous Time SignalsDSP_2018_FOEHU - Lec 02 - Sampling of Continuous Time Signals
DSP_2018_FOEHU - Lec 02 - Sampling of Continuous Time Signals
 
SE2018_Lec 15_ Software Design
SE2018_Lec 15_ Software DesignSE2018_Lec 15_ Software Design
SE2018_Lec 15_ Software Design
 
DSP_2018_FOEHU - Lec 1 - Introduction to Digital Signal Processing
DSP_2018_FOEHU - Lec 1 - Introduction to Digital Signal ProcessingDSP_2018_FOEHU - Lec 1 - Introduction to Digital Signal Processing
DSP_2018_FOEHU - Lec 1 - Introduction to Digital Signal Processing
 

Recently uploaded

call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 

Recently uploaded (20)

call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 

SE2_Lec 21_ TDD and Junit

  • 2. 2  In traditional testing,  Code  Compile  Test! Code Compile Test!
  • 3. 3  Test-Driven Development (TDD) is a software development process that relies on the repetition of a very short development cycle:  first the developer writes an (initially failing test) automated test case that defines a desired improvement or new function,  then produces the minimum amount of code to pass that test, and  finally refactors the new code to acceptable standards.  A test is not something you “do”, it is something you “write” and  run once, twice, three times, etc.  It is a piece of code  Testing is therefore “automated”  Repeatedly executed, even after small changes
  • 4. 4 1. Write a single test. 2. Compile it. It should not compile because you have not written the implementation code 3. Implement just enough code to get the test to compile 4. Run the test and see it fail 5. Implement just enough code to get the test to pass 6. Run the test and see it pass 7. Refactor for clarity and “once and only once” 8. Repeat
  • 5. 5  Context of Testing:  Valid inputs  Invalid inputs  Errors, exceptions, and events  Boundary conditions  Everything that might break  Testing Framework (Xunit)  A testing framework is used so that automated testing can be done after every small change to the code • This may be as often as every 5 or 10 minutes
  • 6. 6  Programmers dislike testing  They will test reasonably thoroughly the first time  The second time however, testing is usually less thorough  The third time, well..  Testing is considered a “boring” task  Testing might be the job of another department/person  TDD encourages programmers to maintain an exhaustive set of repeatable tests  Tests live alongside the Class/Code Under Test (CUT)  With tool support, tests can be run selectively  The tests can be run after every single change
  • 7. 7  Writing clear requirements.  Code proven to meet requirements  Development in small steps (Shorter development cycles).  The debugging will easier since we will have small code chunks to debug.  Minimalistic code and enforce the YAGNI “You Aren’t Gonna Need It” principle  It is a principle of extreme programming (XP) that states a programmer should not add functionality until deemed necessary.  Catch bugs before they are shipped to your customer  No code without tests  Tests determine the code  TDD negates fear  Fear makes developers communicate less  Fear makes developers avoid repeatedly testing code • Afraid of negative feedback  TDD allows us to refactor, or change the implementation of a class, without the fear of breaking it  TDD and refactoring go hand-in-hand
  • 8. 8  JUnit is a unit testing framework for Java programming language. It plays a crucial role test-driven development.  JUnit is a framework for writing tests  Written by Erich Gamma (of Design Patterns fame) and Kent Beck (creator of XP methodology)  Uses Java features such as annotations and static imports  JUnit helps the programmer: • define and execute tests and test suites • formalize requirements • write and debug code • integrate code and always be ready to release a working version
  • 9. 9  A test fixture sets up the data (both objects and primitives) that are needed for every test  Example: If you are testing code that updates an employee record, you need an employee record to test it on  A unit test is a test of a single class  A test case tests the response of a single method to a particular set of inputs  A test suite is a collection of test cases  A test runner is software that runs tests and reports results
  • 10. 10  To test a class named Fraction  Create a test class FractionTest
  • 11. 11  Methods annotated with @Before will execute before every test case  Methods annotated with @After will execute after every test case
  • 12. 12  Methods annotated with @BeforeClass will execute once before all test cases  Methods annotated with @AfterClass will execute once after all test cases  These are useful if you need to allocate and release expensive resources once
  • 13. 13  Methods annotated with @Test are considered to be test cases
  • 14. 14  For each test case t:  JUnit executes all @Before methods • Their order of execution is not specified  JUnit executes t • Any exceptions during its execution are logged  JUnit executes all @After methods • Their order of execution is not specified  A report for all test cases is presented
  • 15. 15  Call the methods of the class being tested  Assert what the correct result should be with one of the provided assert methods  These steps can be repeated as many times as necessary  An assert method is a JUnit method that performs a test, and throws an AssertionError if the test fails  JUnit catches these exceptions and shows you the results
  • 16. 16  assertTrue(boolean b) assertTrue(String s, boolean b)  Throws an AssertionError if b is False  The optional message s is included in the Error  assertFalse(boolean b) assertFalse(String s, boolean b)  Throws an AssertionError if b is True  All assert methods have an optional message
  • 17. 17  assertEquals(Object expected, Object actual)  Uses the equals method to compare the two objects  Primitives can be passed as arguments.  Casting may be required for primitives.  There is also a version to compare arrays.
  • 18. 18  assertSame(Object expected, Object actual)  Asserts that two references are attached to the same object (using ==)  assertNotSame(Object expected, Object actual)  Asserts that two references are not attached to the same object
  • 19. 19  assertNull(Object object)  Asserts that a reference is null  assertNotNull(Object object)  Asserts that a reference is not null  fail()  Causes the test to fail and throw an AssertionError • Useful as a result of a complex test, or when testing for exceptions
  • 20. 20  If a test case is expected to raise an exception, it can be noted as follows
  • 21. 21  A statement such as assert boolean_condition;  will also throw an AssertionError if the boolean_condition is false  Can be used instead of the Junit assertTrue method
  • 22. 22  Test cases that are not finished yet can be annotated with @Ignore  JUnit will not execute the test case but will report how many test cases are being ignored
  • 23. 23  To demonstrate the power of JUnit let us create the simple Java class and write several test cases for it.  The class named MathFunc has two methods: factorial of non-negative number and sum of two integer numbers. Also, there is a counter of method calls just to make the class more complex.
  • 24. 24 MathFunc  The source code of the class is shown below.
  • 25. 25 MathFuncTest  In order to create Unit test cases we need to create the class with some methods. Of course the class could have auxiliary methods.
  • 27. 27 JUnitCore  The most modern method to programmatically run tests is JUnitCore. Just add the following method to the MathFuncTest class:  And the execution result is:
  • 28. 28