SlideShare ist ein Scribd-Unternehmen logo
1 von 23
Company
LOGO
JUnit
Week 8, Session 3
Lecturer : ACB
- STQA IE 31315 -
Institut Teknologi Del
Jl. Sisingamangaraja
Sitoluama, Laguboti 22381
Toba โ€“ SUMUT
http://www.del.ac.id
Topics
๏‚ง What is JUnit?
๏‚ง Why JUnit framework?
๏‚ง JUnit mechanics
๏‚ง Fixtures
๏‚ง Test suites
๏‚ง Test runners
๏‚ง JUnit classes
5/20/2014 STQA/Week09/JUnit 2
What is JUnit?
๏‚ง An unit-testing tool for Java programs
๏‚ง Supports Test-Driven-Development
๏‚ง Agile Method
๏‚ง Goal: Accelerate programming and
increase the quality of code.
๏‚ง Not for testing GUI
5/20/2014 STQA/Week09/JUnit 3
JUnit (contโ€™d)
๏‚ง Each method under test is treated
as a unit
๏‚ง The automated testing scripts are
in the form of methods in another
Java source file
๏‚› The job of the software
developers/testers is then to
write the automated testing
scripts
5/20/2014 STQA/Week09/JUnit 4
Why Junit?
๏‚ง Without JUnit, you will have to use
println() to print out some result:
5/20/2014 STQA/Week09/JUnit 5
Why JUnit (contโ€™d)
Without JUnit:
๏‚› No explicit concept of test passing or
failure
๏‚› No mechanism to collect results in
structured fashion
๏‚› No replicability
5/20/2014 STQA/Week09/JUnit 6
Key Goals of JUnit
๏‚ง Easy to use to create tests
๏‚ง Create tests that retain their value over
time
๏‚ง Leverage existing tests to write new
ones (reusable)
5/20/2014 STQA/Week09/JUnit 7
What does JUnit Provide?
๏‚ง API for easily creating Java test cases
5/20/2014 STQA/Week09/JUnit 8
What does JUnit Provide?
๏‚ง Comprehensive assertion facilities
๏‚› verify expected versus actual results
5/20/2014 STQA/Week09/JUnit 9
What does JUnit Provide?
๏‚ง Test runners for running tests
5/20/2014 STQA/Week09/JUnit 10
What does JUnit Provide?
๏‚ง Reporting
5/20/2014 STQA/Week09/JUnit 11
What does JUnit Provide?
Aggregation facility (test suites)
๏‚ง Used to collect all the test cases
๏‚ง Suites can contain testCases and
testSuites
โ€“ TestSuite(java.lang.Class theClass,
<java.lang.String name>)
โ€“ addTest(Test test) or
addTestSuite(java.lang.Class testClass)
๏‚ง Suites can have hierarchy
5/20/2014 STQA/Week09/JUnit 12
How to write JUnit-based
testing code (Minimum)
๏‚ง Include JUnit.jar in the classpath
๏‚ง Define a subclass of TestCase class
๏‚ง Define one or more public testXXX()
methods in the subclass
๏‚ง Write assert methods inside the testXXX
methods()
๏‚ง Optionally define main() to run the
TestCase in standalone mode
5/20/2014 STQA/Week09/JUnit 13
Test methods
๏‚ง Test methods has name pattern: testXXX()
๏‚› XXX reflects the method of the target class
๏‚ง Test methods must have no arguments
๏‚ง Test methods are type of void
5/20/2014 STQA/Week09/JUnit 14
Very Simple Test
import junit.framework.TestCase;
public class SimpleTest extends TestCase {
public SimpleTest(String name) {
super(name);
}
// Test code
public void testSomething() {
System.out.println("About to call assertTrue() method...");
assertTrue(4 == (2 * 2));
}
// You don't have to have main() method, use Test runner
public static void main(String[] args){
junit.textui.TestRunner.run(SimpleTest.class);
}
}
5/20/2014 STQA/Week09/JUnit 15
Assert Statements
๏‚ง JUnit Assertions are methods starting with
assert
๏‚ง Determines the success or failure of a test
๏‚ง An assert is simply a comparison between
๏‚ง an expected value and an actual value
๏‚ง Two variants
๏‚› assertXXX(...)
๏‚› assertXXX(String message, ...)
the message is displayed when the
assertXXX() fails
5/20/2014 STQA/Week09/JUnit 16
Assert Statements
(contโ€™d)
๏‚ง Asserts that a condition is true
โ€“ assertTrue(boolean condition)
โ€“ assertTrue(String message, boolean
condition)
๏‚ง Asserts that a condition is false
โ€“ assertFalse(boolean condition)
โ€“ assertFalse(String message, boolean
condition)
5/20/2014 STQA/Week09/JUnit 17
Assert Statements
(contโ€™d)
๏‚ง Asserts expected.equals(actual) behavior
โ€“ assertEquals(expected, actual)
โ€“ assertEquals(String message, expected,
actual)
๏‚ง Asserts expected == actual behavior
โ€“ assertSame(Object expected, Object
actual)
โ€“ assertSame(String message, Object
expected, Object actual)
5/20/2014 STQA/Week09/JUnit 18
Assert Statements
(contโ€™d)
๏‚ง Asserts object reference is null
โ€“ assertNull(Object obj)
โ€“ assertNull(String message, Object obj)
๏‚ง Asserts object reference is not null
โ€“ assertNotNull(Object obj)
โ€“ assertNotNull(String message, Object obj)
๏‚ง Forces a failure
โ€“ fail()
โ€“ fail(String message)
5/20/2014 STQA/Week09/JUnit 19
Fixtures
๏‚ง setUp() and tearDown() used to initialize
and release common test data
๏‚ง setUp() is run before every test invocation
๏‚ง tearDown() is run after every test method
5/20/2014 STQA/Week09/JUnit 20
JUnit Classes
5/20/2014 STQA/Week09/JUnit 21
References
๏‚ง www.junit.org
๏‚ง http://www.javapassion.com/javase/javaju
nit.pdf
5/20/2014 STQA/Week09/JUnit 22
THANK YOU
5/20/2014 STQA/Week09/JUnit 23

Weitere รคhnliche Inhalte

Was ist angesagt?

JUNit Presentation
JUNit PresentationJUNit Presentation
JUNit Presentation
Animesh Kumar
ย 
3 j unit
3 j unit3 j unit
3 j unit
kishoregali
ย 

Was ist angesagt? (20)

05 junit
05 junit05 junit
05 junit
ย 
Junit
JunitJunit
Junit
ย 
Junit 4.0
Junit 4.0Junit 4.0
Junit 4.0
ย 
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
ย 
JUNit Presentation
JUNit PresentationJUNit Presentation
JUNit Presentation
ย 
JUnit 5
JUnit 5JUnit 5
JUnit 5
ย 
Junit
JunitJunit
Junit
ย 
JUnit- A Unit Testing Framework
JUnit- A Unit Testing FrameworkJUnit- A Unit Testing Framework
JUnit- A Unit Testing Framework
ย 
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
ย 
What is JUnit? | Edureka
What is JUnit? | EdurekaWhat is JUnit? | Edureka
What is JUnit? | Edureka
ย 
Junit tutorial
Junit tutorialJunit tutorial
Junit tutorial
ย 
Unit testing with Junit
Unit testing with JunitUnit testing with Junit
Unit testing with Junit
ย 
Unit Testing in Java
Unit Testing in JavaUnit Testing in Java
Unit Testing in Java
ย 
Unit testing with JUnit
Unit testing with JUnitUnit testing with JUnit
Unit testing with JUnit
ย 
Introduction to JUnit
Introduction to JUnitIntroduction to JUnit
Introduction to JUnit
ย 
Test driven development - JUnit basics and best practices
Test driven development - JUnit basics and best practicesTest driven development - JUnit basics and best practices
Test driven development - JUnit basics and best practices
ย 
3 j unit
3 j unit3 j unit
3 j unit
ย 
Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit Testing
ย 
Testing In Java
Testing In JavaTesting In Java
Testing In Java
ย 

ร„hnlich wie Introduction To J unit

J unit presentation
J unit presentationJ unit presentation
J unit presentation
Priya Sharma
ย 
Junit4.0
Junit4.0Junit4.0
Junit4.0
gouthamrv
ย 
N Unit Presentation
N Unit PresentationN Unit Presentation
N Unit Presentation
priya_trivedi
ย 
Unit testing and test driven development using vs
Unit testing and test driven development using vsUnit testing and test driven development using vs
Unit testing and test driven development using vs
Abhimanyu Singhal
ย 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Anand Kumar Rajana
ย 
TestNG introduction
TestNG introductionTestNG introduction
TestNG introduction
Denis Bazhin
ย 

ร„hnlich wie Introduction To J unit (20)

L08 Unit Testing
L08 Unit TestingL08 Unit Testing
L08 Unit Testing
ย 
J unit presentation
J unit presentationJ unit presentation
J unit presentation
ย 
Junit4&testng presentation
Junit4&testng presentationJunit4&testng presentation
Junit4&testng presentation
ย 
8-testing.pptx
8-testing.pptx8-testing.pptx
8-testing.pptx
ย 
What is new in JUnit5
What is new in JUnit5What is new in JUnit5
What is new in JUnit5
ย 
Coded ui - lesson 4 - coded ui test
Coded ui - lesson 4 - coded ui testCoded ui - lesson 4 - coded ui test
Coded ui - lesson 4 - coded ui test
ย 
Junit4.0
Junit4.0Junit4.0
Junit4.0
ย 
Unit Testing - Nakov's Talk @ VarnaConf 2013
Unit Testing - Nakov's Talk @ VarnaConf 2013Unit Testing - Nakov's Talk @ VarnaConf 2013
Unit Testing - Nakov's Talk @ VarnaConf 2013
ย 
Unit testing by Svetlin Nakov
Unit testing by Svetlin NakovUnit testing by Svetlin Nakov
Unit testing by Svetlin Nakov
ย 
Junit and cactus
Junit and cactusJunit and cactus
Junit and cactus
ย 
TestNG Session presented in PB
TestNG Session presented in PBTestNG Session presented in PB
TestNG Session presented in PB
ย 
N Unit Presentation
N Unit PresentationN Unit Presentation
N Unit Presentation
ย 
Unit testing and test driven development using vs
Unit testing and test driven development using vsUnit testing and test driven development using vs
Unit testing and test driven development using vs
ย 
GeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsGeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good Tests
ย 
Confitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good TestsConfitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good Tests
ย 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
ย 
Junit
JunitJunit
Junit
ย 
TestNG introduction
TestNG introductionTestNG introduction
TestNG introduction
ย 
Qunit testing slider
Qunit testing sliderQunit testing slider
Qunit testing slider
ย 
Introduction to JUnit testing in OpenDaylight
Introduction to JUnit testing in OpenDaylightIntroduction to JUnit testing in OpenDaylight
Introduction to JUnit testing in OpenDaylight
ย 

Kรผrzlich hochgeladen

Call Girls Service Pune โ‚น7.5k Pick Up & Drop With Cash Payment 8005736733 Cal...
Call Girls Service Pune โ‚น7.5k Pick Up & Drop With Cash Payment 8005736733 Cal...Call Girls Service Pune โ‚น7.5k Pick Up & Drop With Cash Payment 8005736733 Cal...
Call Girls Service Pune โ‚น7.5k Pick Up & Drop With Cash Payment 8005736733 Cal...
SUHANI PANDEY
ย 
VIP Model Call Girls Charholi Budruk ( Pune ) Call ON 8005736733 Starting Fro...
VIP Model Call Girls Charholi Budruk ( Pune ) Call ON 8005736733 Starting Fro...VIP Model Call Girls Charholi Budruk ( Pune ) Call ON 8005736733 Starting Fro...
VIP Model Call Girls Charholi Budruk ( Pune ) Call ON 8005736733 Starting Fro...
SUHANI PANDEY
ย 
VIP Model Call Girls Hadapsar ( Pune ) Call ON 8005736733 Starting From 5K to...
VIP Model Call Girls Hadapsar ( Pune ) Call ON 8005736733 Starting From 5K to...VIP Model Call Girls Hadapsar ( Pune ) Call ON 8005736733 Starting From 5K to...
VIP Model Call Girls Hadapsar ( Pune ) Call ON 8005736733 Starting From 5K to...
SUHANI PANDEY
ย 
RA 7942:vThe Philippine Mining Act of 1995
RA 7942:vThe Philippine Mining Act of 1995RA 7942:vThe Philippine Mining Act of 1995
RA 7942:vThe Philippine Mining Act of 1995
garthraymundo123
ย 
Verified Trusted Kalyani Nagar Call Girls 8005736733 ๐ˆ๐๐ƒ๐„๐๐„๐๐ƒ๐„๐๐“ Call ๐†๐ˆ๐‘๐‹ ๐•...
Verified Trusted Kalyani Nagar Call Girls  8005736733 ๐ˆ๐๐ƒ๐„๐๐„๐๐ƒ๐„๐๐“ Call ๐†๐ˆ๐‘๐‹ ๐•...Verified Trusted Kalyani Nagar Call Girls  8005736733 ๐ˆ๐๐ƒ๐„๐๐„๐๐ƒ๐„๐๐“ Call ๐†๐ˆ๐‘๐‹ ๐•...
Verified Trusted Kalyani Nagar Call Girls 8005736733 ๐ˆ๐๐ƒ๐„๐๐„๐๐ƒ๐„๐๐“ Call ๐†๐ˆ๐‘๐‹ ๐•...
tanu pandey
ย 
VIP Model Call Girls Chakan ( Pune ) Call ON 8005736733 Starting From 5K to 2...
VIP Model Call Girls Chakan ( Pune ) Call ON 8005736733 Starting From 5K to 2...VIP Model Call Girls Chakan ( Pune ) Call ON 8005736733 Starting From 5K to 2...
VIP Model Call Girls Chakan ( Pune ) Call ON 8005736733 Starting From 5K to 2...
SUHANI PANDEY
ย 
Environmental Science - Nuclear Hazards and Us.pptx
Environmental Science - Nuclear Hazards and Us.pptxEnvironmental Science - Nuclear Hazards and Us.pptx
Environmental Science - Nuclear Hazards and Us.pptx
hossanmdjobayer103
ย 
VVIP Pune Call Girls Vishal Nagar WhatSapp Number 8005736733 With Elite Staff...
VVIP Pune Call Girls Vishal Nagar WhatSapp Number 8005736733 With Elite Staff...VVIP Pune Call Girls Vishal Nagar WhatSapp Number 8005736733 With Elite Staff...
VVIP Pune Call Girls Vishal Nagar WhatSapp Number 8005736733 With Elite Staff...
SUHANI PANDEY
ย 

Kรผrzlich hochgeladen (20)

Enhancing forest data transparency for climate action
Enhancing forest data transparency for climate actionEnhancing forest data transparency for climate action
Enhancing forest data transparency for climate action
ย 
Call Girls Service Pune โ‚น7.5k Pick Up & Drop With Cash Payment 8005736733 Cal...
Call Girls Service Pune โ‚น7.5k Pick Up & Drop With Cash Payment 8005736733 Cal...Call Girls Service Pune โ‚น7.5k Pick Up & Drop With Cash Payment 8005736733 Cal...
Call Girls Service Pune โ‚น7.5k Pick Up & Drop With Cash Payment 8005736733 Cal...
ย 
Deforestation
DeforestationDeforestation
Deforestation
ย 
Green Marketing
Green MarketingGreen Marketing
Green Marketing
ย 
Presentation: Farmer-led climate adaptation - Project launch and overview by ...
Presentation: Farmer-led climate adaptation - Project launch and overview by ...Presentation: Farmer-led climate adaptation - Project launch and overview by ...
Presentation: Farmer-led climate adaptation - Project launch and overview by ...
ย 
VIP Model Call Girls Charholi Budruk ( Pune ) Call ON 8005736733 Starting Fro...
VIP Model Call Girls Charholi Budruk ( Pune ) Call ON 8005736733 Starting Fro...VIP Model Call Girls Charholi Budruk ( Pune ) Call ON 8005736733 Starting Fro...
VIP Model Call Girls Charholi Budruk ( Pune ) Call ON 8005736733 Starting Fro...
ย 
VIP Model Call Girls Hadapsar ( Pune ) Call ON 8005736733 Starting From 5K to...
VIP Model Call Girls Hadapsar ( Pune ) Call ON 8005736733 Starting From 5K to...VIP Model Call Girls Hadapsar ( Pune ) Call ON 8005736733 Starting From 5K to...
VIP Model Call Girls Hadapsar ( Pune ) Call ON 8005736733 Starting From 5K to...
ย 
Call Girls Talegaon Dabhade Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Talegaon Dabhade Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Talegaon Dabhade Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Talegaon Dabhade Call Me 7737669865 Budget Friendly No Advance Boo...
ย 
Call Girls Ramtek Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Ramtek Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Ramtek Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Ramtek Call Me 7737669865 Budget Friendly No Advance Booking
ย 
Book Sex Workers Available Pune Call Girls Kondhwa 6297143586 Call Hot India...
Book Sex Workers Available Pune Call Girls Kondhwa  6297143586 Call Hot India...Book Sex Workers Available Pune Call Girls Kondhwa  6297143586 Call Hot India...
Book Sex Workers Available Pune Call Girls Kondhwa 6297143586 Call Hot India...
ย 
DENR EPR Law Compliance Updates April 2024
DENR EPR Law Compliance Updates April 2024DENR EPR Law Compliance Updates April 2024
DENR EPR Law Compliance Updates April 2024
ย 
RATING SYSTEMS- IGBC, GRIHA, LEED--.pptx
RATING  SYSTEMS- IGBC, GRIHA, LEED--.pptxRATING  SYSTEMS- IGBC, GRIHA, LEED--.pptx
RATING SYSTEMS- IGBC, GRIHA, LEED--.pptx
ย 
Call Girls Budhwar Peth Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Budhwar Peth Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Budhwar Peth Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Budhwar Peth Call Me 7737669865 Budget Friendly No Advance Booking
ย 
(INDIRA) Call Girl Katra Call Now 8617697112 Katra Escorts 24x7
(INDIRA) Call Girl Katra Call Now 8617697112 Katra Escorts 24x7(INDIRA) Call Girl Katra Call Now 8617697112 Katra Escorts 24x7
(INDIRA) Call Girl Katra Call Now 8617697112 Katra Escorts 24x7
ย 
CSR_Module5_Green Earth Initiative, Tree Planting Day
CSR_Module5_Green Earth Initiative, Tree Planting DayCSR_Module5_Green Earth Initiative, Tree Planting Day
CSR_Module5_Green Earth Initiative, Tree Planting Day
ย 
RA 7942:vThe Philippine Mining Act of 1995
RA 7942:vThe Philippine Mining Act of 1995RA 7942:vThe Philippine Mining Act of 1995
RA 7942:vThe Philippine Mining Act of 1995
ย 
Verified Trusted Kalyani Nagar Call Girls 8005736733 ๐ˆ๐๐ƒ๐„๐๐„๐๐ƒ๐„๐๐“ Call ๐†๐ˆ๐‘๐‹ ๐•...
Verified Trusted Kalyani Nagar Call Girls  8005736733 ๐ˆ๐๐ƒ๐„๐๐„๐๐ƒ๐„๐๐“ Call ๐†๐ˆ๐‘๐‹ ๐•...Verified Trusted Kalyani Nagar Call Girls  8005736733 ๐ˆ๐๐ƒ๐„๐๐„๐๐ƒ๐„๐๐“ Call ๐†๐ˆ๐‘๐‹ ๐•...
Verified Trusted Kalyani Nagar Call Girls 8005736733 ๐ˆ๐๐ƒ๐„๐๐„๐๐ƒ๐„๐๐“ Call ๐†๐ˆ๐‘๐‹ ๐•...
ย 
VIP Model Call Girls Chakan ( Pune ) Call ON 8005736733 Starting From 5K to 2...
VIP Model Call Girls Chakan ( Pune ) Call ON 8005736733 Starting From 5K to 2...VIP Model Call Girls Chakan ( Pune ) Call ON 8005736733 Starting From 5K to 2...
VIP Model Call Girls Chakan ( Pune ) Call ON 8005736733 Starting From 5K to 2...
ย 
Environmental Science - Nuclear Hazards and Us.pptx
Environmental Science - Nuclear Hazards and Us.pptxEnvironmental Science - Nuclear Hazards and Us.pptx
Environmental Science - Nuclear Hazards and Us.pptx
ย 
VVIP Pune Call Girls Vishal Nagar WhatSapp Number 8005736733 With Elite Staff...
VVIP Pune Call Girls Vishal Nagar WhatSapp Number 8005736733 With Elite Staff...VVIP Pune Call Girls Vishal Nagar WhatSapp Number 8005736733 With Elite Staff...
VVIP Pune Call Girls Vishal Nagar WhatSapp Number 8005736733 With Elite Staff...
ย 

Introduction To J unit

  • 1. Company LOGO JUnit Week 8, Session 3 Lecturer : ACB - STQA IE 31315 - Institut Teknologi Del Jl. Sisingamangaraja Sitoluama, Laguboti 22381 Toba โ€“ SUMUT http://www.del.ac.id
  • 2. Topics ๏‚ง What is JUnit? ๏‚ง Why JUnit framework? ๏‚ง JUnit mechanics ๏‚ง Fixtures ๏‚ง Test suites ๏‚ง Test runners ๏‚ง JUnit classes 5/20/2014 STQA/Week09/JUnit 2
  • 3. What is JUnit? ๏‚ง An unit-testing tool for Java programs ๏‚ง Supports Test-Driven-Development ๏‚ง Agile Method ๏‚ง Goal: Accelerate programming and increase the quality of code. ๏‚ง Not for testing GUI 5/20/2014 STQA/Week09/JUnit 3
  • 4. JUnit (contโ€™d) ๏‚ง Each method under test is treated as a unit ๏‚ง The automated testing scripts are in the form of methods in another Java source file ๏‚› The job of the software developers/testers is then to write the automated testing scripts 5/20/2014 STQA/Week09/JUnit 4
  • 5. Why Junit? ๏‚ง Without JUnit, you will have to use println() to print out some result: 5/20/2014 STQA/Week09/JUnit 5
  • 6. Why JUnit (contโ€™d) Without JUnit: ๏‚› No explicit concept of test passing or failure ๏‚› No mechanism to collect results in structured fashion ๏‚› No replicability 5/20/2014 STQA/Week09/JUnit 6
  • 7. Key Goals of JUnit ๏‚ง Easy to use to create tests ๏‚ง Create tests that retain their value over time ๏‚ง Leverage existing tests to write new ones (reusable) 5/20/2014 STQA/Week09/JUnit 7
  • 8. What does JUnit Provide? ๏‚ง API for easily creating Java test cases 5/20/2014 STQA/Week09/JUnit 8
  • 9. What does JUnit Provide? ๏‚ง Comprehensive assertion facilities ๏‚› verify expected versus actual results 5/20/2014 STQA/Week09/JUnit 9
  • 10. What does JUnit Provide? ๏‚ง Test runners for running tests 5/20/2014 STQA/Week09/JUnit 10
  • 11. What does JUnit Provide? ๏‚ง Reporting 5/20/2014 STQA/Week09/JUnit 11
  • 12. What does JUnit Provide? Aggregation facility (test suites) ๏‚ง Used to collect all the test cases ๏‚ง Suites can contain testCases and testSuites โ€“ TestSuite(java.lang.Class theClass, <java.lang.String name>) โ€“ addTest(Test test) or addTestSuite(java.lang.Class testClass) ๏‚ง Suites can have hierarchy 5/20/2014 STQA/Week09/JUnit 12
  • 13. How to write JUnit-based testing code (Minimum) ๏‚ง Include JUnit.jar in the classpath ๏‚ง Define a subclass of TestCase class ๏‚ง Define one or more public testXXX() methods in the subclass ๏‚ง Write assert methods inside the testXXX methods() ๏‚ง Optionally define main() to run the TestCase in standalone mode 5/20/2014 STQA/Week09/JUnit 13
  • 14. Test methods ๏‚ง Test methods has name pattern: testXXX() ๏‚› XXX reflects the method of the target class ๏‚ง Test methods must have no arguments ๏‚ง Test methods are type of void 5/20/2014 STQA/Week09/JUnit 14
  • 15. Very Simple Test import junit.framework.TestCase; public class SimpleTest extends TestCase { public SimpleTest(String name) { super(name); } // Test code public void testSomething() { System.out.println("About to call assertTrue() method..."); assertTrue(4 == (2 * 2)); } // You don't have to have main() method, use Test runner public static void main(String[] args){ junit.textui.TestRunner.run(SimpleTest.class); } } 5/20/2014 STQA/Week09/JUnit 15
  • 16. Assert Statements ๏‚ง JUnit Assertions are methods starting with assert ๏‚ง Determines the success or failure of a test ๏‚ง An assert is simply a comparison between ๏‚ง an expected value and an actual value ๏‚ง Two variants ๏‚› assertXXX(...) ๏‚› assertXXX(String message, ...) the message is displayed when the assertXXX() fails 5/20/2014 STQA/Week09/JUnit 16
  • 17. Assert Statements (contโ€™d) ๏‚ง Asserts that a condition is true โ€“ assertTrue(boolean condition) โ€“ assertTrue(String message, boolean condition) ๏‚ง Asserts that a condition is false โ€“ assertFalse(boolean condition) โ€“ assertFalse(String message, boolean condition) 5/20/2014 STQA/Week09/JUnit 17
  • 18. Assert Statements (contโ€™d) ๏‚ง Asserts expected.equals(actual) behavior โ€“ assertEquals(expected, actual) โ€“ assertEquals(String message, expected, actual) ๏‚ง Asserts expected == actual behavior โ€“ assertSame(Object expected, Object actual) โ€“ assertSame(String message, Object expected, Object actual) 5/20/2014 STQA/Week09/JUnit 18
  • 19. Assert Statements (contโ€™d) ๏‚ง Asserts object reference is null โ€“ assertNull(Object obj) โ€“ assertNull(String message, Object obj) ๏‚ง Asserts object reference is not null โ€“ assertNotNull(Object obj) โ€“ assertNotNull(String message, Object obj) ๏‚ง Forces a failure โ€“ fail() โ€“ fail(String message) 5/20/2014 STQA/Week09/JUnit 19
  • 20. Fixtures ๏‚ง setUp() and tearDown() used to initialize and release common test data ๏‚ง setUp() is run before every test invocation ๏‚ง tearDown() is run after every test method 5/20/2014 STQA/Week09/JUnit 20