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?

05 junit
05 junit05 junit
05 junitmha4
 
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.1Kiki Ahmadi
 
JUnit- A Unit Testing Framework
JUnit- A Unit Testing FrameworkJUnit- A Unit Testing Framework
JUnit- A Unit Testing FrameworkOnkar Deshpande
 
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 TimeTed Vinke
 
What is JUnit? | Edureka
What is JUnit? | EdurekaWhat is JUnit? | Edureka
What is JUnit? | EdurekaEdureka!
 
Unit testing with Junit
Unit testing with JunitUnit testing with Junit
Unit testing with JunitValerio Maggio
 
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 practicesNarendra Pathai
 
Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit TestingMike Lively
 

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 JUnit Testing Framework Explained

J unit presentation
J unit presentationJ unit presentation
J unit presentationPriya Sharma
 
Junit4&testng presentation
Junit4&testng presentationJunit4&testng presentation
Junit4&testng presentationSanjib Dhar
 
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 testOmer Karpas
 
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 2013Svetlin Nakov
 
Unit testing by Svetlin Nakov
Unit testing by Svetlin NakovUnit testing by Svetlin Nakov
Unit testing by Svetlin Nakovit-tour
 
Junit and cactus
Junit and cactusJunit and cactus
Junit and cactusHimanshu
 
TestNG Session presented in PB
TestNG Session presented in PBTestNG Session presented in PB
TestNG Session presented in PBAbhishek Yadav
 
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 vsAbhimanyu Singhal
 
GeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsGeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsTomek Kaczanowski
 
Confitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good TestsConfitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good TestsTomek Kaczanowski
 
TestNG introduction
TestNG introductionTestNG introduction
TestNG introductionDenis Bazhin
 
Introduction to JUnit testing in OpenDaylight
Introduction to JUnit testing in OpenDaylightIntroduction to JUnit testing in OpenDaylight
Introduction to JUnit testing in OpenDaylightOpenDaylight
 

Ähnlich wie JUnit Testing Framework Explained (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

INSIDER THREAT PREVENTION IN THE US BANKING SYSTEM
INSIDER THREAT PREVENTION IN THE US BANKING SYSTEMINSIDER THREAT PREVENTION IN THE US BANKING SYSTEM
INSIDER THREAT PREVENTION IN THE US BANKING SYSTEMijsc
 
原版定制copy澳洲詹姆斯库克大学毕业证JCU毕业证成绩单留信学历认证保障质量
原版定制copy澳洲詹姆斯库克大学毕业证JCU毕业证成绩单留信学历认证保障质量原版定制copy澳洲詹姆斯库克大学毕业证JCU毕业证成绩单留信学历认证保障质量
原版定制copy澳洲詹姆斯库克大学毕业证JCU毕业证成绩单留信学历认证保障质量sehgh15heh
 
办理英属哥伦比亚大学毕业证成绩单|购买加拿大UBC文凭证书
办理英属哥伦比亚大学毕业证成绩单|购买加拿大UBC文凭证书办理英属哥伦比亚大学毕业证成绩单|购买加拿大UBC文凭证书
办理英属哥伦比亚大学毕业证成绩单|购买加拿大UBC文凭证书zdzoqco
 
Call In girls Connaught Place (DELHI)⇛9711147426🔝Delhi NCR
Call In girls Connaught Place (DELHI)⇛9711147426🔝Delhi NCRCall In girls Connaught Place (DELHI)⇛9711147426🔝Delhi NCR
Call In girls Connaught Place (DELHI)⇛9711147426🔝Delhi NCRjennyeacort
 
Dwarka Call Girls 9643097474 Phone Number 24x7 Best Services
Dwarka Call Girls 9643097474 Phone Number 24x7 Best ServicesDwarka Call Girls 9643097474 Phone Number 24x7 Best Services
Dwarka Call Girls 9643097474 Phone Number 24x7 Best Servicesnajka9823
 
Call Girls Ahmedabad 7397865700 Ridhima Hire Me Full Night
Call Girls Ahmedabad 7397865700 Ridhima Hire Me Full NightCall Girls Ahmedabad 7397865700 Ridhima Hire Me Full Night
Call Girls Ahmedabad 7397865700 Ridhima Hire Me Full Nightssuser7cb4ff
 
Hi FI Call Girl Ahmedabad 7397865700 Independent Call Girls
Hi FI Call Girl Ahmedabad 7397865700 Independent Call GirlsHi FI Call Girl Ahmedabad 7397865700 Independent Call Girls
Hi FI Call Girl Ahmedabad 7397865700 Independent Call Girlsssuser7cb4ff
 
Title-Role of forestry in restoration of degraded lands.pptx
Title-Role of forestry in restoration of degraded lands.pptxTitle-Role of forestry in restoration of degraded lands.pptx
Title-Role of forestry in restoration of degraded lands.pptxSagar Chaudhary
 
global trend Chapter 1.presentation power point
global trend Chapter 1.presentation power pointglobal trend Chapter 1.presentation power point
global trend Chapter 1.presentation power pointyohannisyohannis54
 
办理德州理工大学毕业证书TTU文凭学位证书
办理德州理工大学毕业证书TTU文凭学位证书办理德州理工大学毕业证书TTU文凭学位证书
办理德州理工大学毕业证书TTU文凭学位证书zdzoqco
 
See How do animals kill their prey for food
See How do animals kill their prey for foodSee How do animals kill their prey for food
See How do animals kill their prey for fooddrsk203
 
Call Girls Sarovar Portico Naraina Hotel, New Delhi 9873777170
Call Girls Sarovar Portico Naraina Hotel, New Delhi 9873777170Call Girls Sarovar Portico Naraina Hotel, New Delhi 9873777170
Call Girls Sarovar Portico Naraina Hotel, New Delhi 9873777170simranguptaxx69
 
NO1 Certified Rohani Amil In Islamabad Amil Baba in Rawalpindi Kala Jadu Amil...
NO1 Certified Rohani Amil In Islamabad Amil Baba in Rawalpindi Kala Jadu Amil...NO1 Certified Rohani Amil In Islamabad Amil Baba in Rawalpindi Kala Jadu Amil...
NO1 Certified Rohani Amil In Islamabad Amil Baba in Rawalpindi Kala Jadu Amil...Amil baba
 
EMP (Environment Management Plan . .pptx
EMP (Environment Management Plan . .pptxEMP (Environment Management Plan . .pptx
EMP (Environment Management Plan . .pptxSarmad Naeem
 
Al Jaddaf Housewife Call Girls +971509530047 Al Jaddaf Call Girls
Al Jaddaf Housewife Call Girls +971509530047 Al Jaddaf Call GirlsAl Jaddaf Housewife Call Girls +971509530047 Al Jaddaf Call Girls
Al Jaddaf Housewife Call Girls +971509530047 Al Jaddaf Call Girlstiril72860
 
Along the Lakefront, "Menacing Unknown"s
Along the Lakefront, "Menacing Unknown"sAlong the Lakefront, "Menacing Unknown"s
Along the Lakefront, "Menacing Unknown"syalehistoricalreview
 
UNIT ONE ppt history of Ethiopia and horn.pptx
UNIT ONE ppt history of Ethiopia and horn.pptxUNIT ONE ppt history of Ethiopia and horn.pptx
UNIT ONE ppt history of Ethiopia and horn.pptxzeyohannesamare
 

Kürzlich hochgeladen (20)

INSIDER THREAT PREVENTION IN THE US BANKING SYSTEM
INSIDER THREAT PREVENTION IN THE US BANKING SYSTEMINSIDER THREAT PREVENTION IN THE US BANKING SYSTEM
INSIDER THREAT PREVENTION IN THE US BANKING SYSTEM
 
PLANTILLAS DE MEMORAMA CIENCIAS NATURALES
PLANTILLAS DE MEMORAMA CIENCIAS NATURALESPLANTILLAS DE MEMORAMA CIENCIAS NATURALES
PLANTILLAS DE MEMORAMA CIENCIAS NATURALES
 
原版定制copy澳洲詹姆斯库克大学毕业证JCU毕业证成绩单留信学历认证保障质量
原版定制copy澳洲詹姆斯库克大学毕业证JCU毕业证成绩单留信学历认证保障质量原版定制copy澳洲詹姆斯库克大学毕业证JCU毕业证成绩单留信学历认证保障质量
原版定制copy澳洲詹姆斯库克大学毕业证JCU毕业证成绩单留信学历认证保障质量
 
办理英属哥伦比亚大学毕业证成绩单|购买加拿大UBC文凭证书
办理英属哥伦比亚大学毕业证成绩单|购买加拿大UBC文凭证书办理英属哥伦比亚大学毕业证成绩单|购买加拿大UBC文凭证书
办理英属哥伦比亚大学毕业证成绩单|购买加拿大UBC文凭证书
 
Call In girls Connaught Place (DELHI)⇛9711147426🔝Delhi NCR
Call In girls Connaught Place (DELHI)⇛9711147426🔝Delhi NCRCall In girls Connaught Place (DELHI)⇛9711147426🔝Delhi NCR
Call In girls Connaught Place (DELHI)⇛9711147426🔝Delhi NCR
 
Dwarka Call Girls 9643097474 Phone Number 24x7 Best Services
Dwarka Call Girls 9643097474 Phone Number 24x7 Best ServicesDwarka Call Girls 9643097474 Phone Number 24x7 Best Services
Dwarka Call Girls 9643097474 Phone Number 24x7 Best Services
 
Call Girls Ahmedabad 7397865700 Ridhima Hire Me Full Night
Call Girls Ahmedabad 7397865700 Ridhima Hire Me Full NightCall Girls Ahmedabad 7397865700 Ridhima Hire Me Full Night
Call Girls Ahmedabad 7397865700 Ridhima Hire Me Full Night
 
Hi FI Call Girl Ahmedabad 7397865700 Independent Call Girls
Hi FI Call Girl Ahmedabad 7397865700 Independent Call GirlsHi FI Call Girl Ahmedabad 7397865700 Independent Call Girls
Hi FI Call Girl Ahmedabad 7397865700 Independent Call Girls
 
FULL ENJOY Call Girls In kashmiri gate (Delhi) Call Us 9953056974
FULL ENJOY Call Girls In  kashmiri gate (Delhi) Call Us 9953056974FULL ENJOY Call Girls In  kashmiri gate (Delhi) Call Us 9953056974
FULL ENJOY Call Girls In kashmiri gate (Delhi) Call Us 9953056974
 
Title-Role of forestry in restoration of degraded lands.pptx
Title-Role of forestry in restoration of degraded lands.pptxTitle-Role of forestry in restoration of degraded lands.pptx
Title-Role of forestry in restoration of degraded lands.pptx
 
young call girls in Janakpuri🔝 9953056974 🔝 escort Service
young call girls in Janakpuri🔝 9953056974 🔝 escort Serviceyoung call girls in Janakpuri🔝 9953056974 🔝 escort Service
young call girls in Janakpuri🔝 9953056974 🔝 escort Service
 
global trend Chapter 1.presentation power point
global trend Chapter 1.presentation power pointglobal trend Chapter 1.presentation power point
global trend Chapter 1.presentation power point
 
办理德州理工大学毕业证书TTU文凭学位证书
办理德州理工大学毕业证书TTU文凭学位证书办理德州理工大学毕业证书TTU文凭学位证书
办理德州理工大学毕业证书TTU文凭学位证书
 
See How do animals kill their prey for food
See How do animals kill their prey for foodSee How do animals kill their prey for food
See How do animals kill their prey for food
 
Call Girls Sarovar Portico Naraina Hotel, New Delhi 9873777170
Call Girls Sarovar Portico Naraina Hotel, New Delhi 9873777170Call Girls Sarovar Portico Naraina Hotel, New Delhi 9873777170
Call Girls Sarovar Portico Naraina Hotel, New Delhi 9873777170
 
NO1 Certified Rohani Amil In Islamabad Amil Baba in Rawalpindi Kala Jadu Amil...
NO1 Certified Rohani Amil In Islamabad Amil Baba in Rawalpindi Kala Jadu Amil...NO1 Certified Rohani Amil In Islamabad Amil Baba in Rawalpindi Kala Jadu Amil...
NO1 Certified Rohani Amil In Islamabad Amil Baba in Rawalpindi Kala Jadu Amil...
 
EMP (Environment Management Plan . .pptx
EMP (Environment Management Plan . .pptxEMP (Environment Management Plan . .pptx
EMP (Environment Management Plan . .pptx
 
Al Jaddaf Housewife Call Girls +971509530047 Al Jaddaf Call Girls
Al Jaddaf Housewife Call Girls +971509530047 Al Jaddaf Call GirlsAl Jaddaf Housewife Call Girls +971509530047 Al Jaddaf Call Girls
Al Jaddaf Housewife Call Girls +971509530047 Al Jaddaf Call Girls
 
Along the Lakefront, "Menacing Unknown"s
Along the Lakefront, "Menacing Unknown"sAlong the Lakefront, "Menacing Unknown"s
Along the Lakefront, "Menacing Unknown"s
 
UNIT ONE ppt history of Ethiopia and horn.pptx
UNIT ONE ppt history of Ethiopia and horn.pptxUNIT ONE ppt history of Ethiopia and horn.pptx
UNIT ONE ppt history of Ethiopia and horn.pptx
 

JUnit Testing Framework Explained

  • 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