SlideShare a Scribd company logo
1 of 16
JUnit
A tool for test-driven development
History
 Kent Beck developed the first xUnit automated test tool for
Smalltalk in mid-90’s
 Beck and Gamma (of design patterns Gang of Four)
developed JUnit on a flight from Zurich to Washington, D.C.
 Martin Fowler: “Never in the field of software development
was so much owed by so many to so few lines of code.”
 Junit has become the standard tool for Test-Driven
Development in Java (see Junit.org)
 Junit test generators now part of many Java IDEs (Eclipse,
BlueJ, Jbuilder, DrJava)
 Xunit tools have since been developed for many other
languages (Perl, C++, Python, Visual Basic, C#, …)
Why create a test suite?
 Obviously you have to test your code—right?
 You can do ad hoc testing (running whatever tests occur to you
at the moment), or
 You can build a test suite (a thorough set of tests that can be
run at any time)
 Disadvantages of a test suite
 It’s a lot of extra programming
• True, but use of a good test framework can help quite a bit
 You don’t have time to do all that extra work
• False! Experiments repeatedly show that test suites reduce
debugging time more than the amount spent building the test suite
 Advantages of a test suite
 Reduces total number of bugs in delivered code
 Makes code much more maintainable and refactorable
Architectural overview
 JUnit test framework is a
package of classes that
lets you write tests for
each method, then easily
run those tests
 TestRunner runs tests
and reports TestResults
 You test your class by
extending abstract class
TestCase
 To write test cases, you
need to know and
understand the
Assert class
Writing a TestCase
 To start using JUnit, create a subclass of
TestCase, to which you add test methods
 Here’s a skeletal test class:
import junit.framework.TestCase;
public class TestBowl extends TestCase {
} //Test my class Bowl
 Name of class is important – should be of the
form TestMyClass or MyClassTest
 This naming convention lets TestRunner
automatically find your test classes
Writing methods in TestCase
 Pattern follows programming by contract paradigm:
 Set up preconditions
 Exercise functionality being tested
 Check postconditions
 Example:
public void testEmptyList() {
Bowl emptyBowl = new Bowl();
assertEquals(“Size of an empty list should be zero.”,
0, emptyList.size());
assertTrue(“An empty bowl should report empty.”,
emptyBowl.isEmpty());
}
 Things to notice:
 Specific method signature – public void testWhatever()
• Allows them to be found and collected automatically by JUnit
 Coding follows pattern
 Notice the assert-type calls…
Assert methods
 Each assert method has parameters like these:
message, expected-value, actual-value
 Assert methods dealing with floating point
numbers get an additional argument, a tolerance
 Each assert method has an equivalent version
that does not take a message – however, this
use is not recommended because:
 messages helps documents the tests
 messages provide additional information when
reading failure logs
Assert methods
 assertTrue(String message, Boolean test)
 assertFalse(String message, Boolean test)
 assertNull(String message, Object object)
 assertNotNull(String message, Object object)
 assertEquals(String message, Object expected,
Object actual) (uses equals method)
 assertSame(String message, Object expected,
Object actual) (uses == operator)
 assertNotSame(String message, Object expected,
Object actual)
More stuff in test classes
 Suppose you want to test a class Counter
 public class CounterTest
extends junit.framework.TestCase {
 This is the unit test for the Counter class
 public CounterTest() { } //Default constructor
 protected void setUp()
 Test fixture creates and initializes instance variables, etc.
 protected void tearDown()
 Releases any system resources used by the test fixture
 public void testIncrement(), public void testDecrement()
 These methods contain tests for the Counter methods
increment(), decrement(), etc.
 Note capitalization convention
JUnit tests for Counter
public class CounterTest extends junit.framework.TestCase {
Counter counter1;
public CounterTest() { } // default constructor
protected void setUp() { // creates a (simple) test fixture
counter1 = new Counter();
}
public void testIncrement() {
assertTrue(counter1.increment() == 1);
assertTrue(counter1.increment() == 2);
}
public void testDecrement() {
assertTrue(counter1.decrement() == -1);
}
}
Note that each test begins
with a brand new counter
This means you don’t
have to worry about the
order in which the tests
are run
TestSuites
 TestSuites collect a selection of tests to run them as a unit
 Collections automatically use TestSuites, however to specify the
order in which tests are run, write your own:
public static Test suite() {
suite.addTest(new TestBowl(“testBowl”));
suite.addTest(new TestBowl(“testAdding”));
return suite;
}
 Should seldom have to write your own TestSuites as each method
in your TestCase should be independent of all others
 Can create TestSuites that test a whole package:
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTestSuite(TestBowl.class);
suite.addTestSuite(TestFruit.class);
return suite;
}
JUnit in Eclipse
 To create a test class,
select File→ New→
Other... → Java,
JUnit, TestCase and
enter the name of the
class you will test
Fill this in
This will be filled
in automatically
Running JUnit
First, select a Test classSecond, use this
pulldown menu
Third, Run As → JUnit Test
Results
Your results are here
Unit testing for other languages
 Unit testing tools differentiate between:
 Errors (unanticipated problems caught by exceptions)
 Failures (anticipated problems checked with
assertions)
 Basic unit of testing:
 CPPUNIT_ASSERT(Bool) examines an expression
 CPPUnit has variety of test classes
(e.g. TestFixture)
 Inherit from them and overload methods
More Information
 http://www.junit.org
Download of JUnit
Lots of information on using JUnit

More Related Content

What's hot

What's hot (20)

Junit
JunitJunit
Junit
 
Unit and integration Testing
Unit and integration TestingUnit and integration Testing
Unit and integration Testing
 
Unit testing with JUnit
Unit testing with JUnitUnit testing with JUnit
Unit testing with JUnit
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best Practices
 
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
 
TestNG Framework
TestNG Framework TestNG Framework
TestNG Framework
 
Test NG Framework Complete Walk Through
Test NG Framework Complete Walk ThroughTest NG Framework Complete Walk Through
Test NG Framework Complete Walk Through
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Testing with Spring: An Introduction
Testing with Spring: An IntroductionTesting with Spring: An Introduction
Testing with Spring: An Introduction
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And Mocking
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDD
 
UNIT TESTING PPT
UNIT TESTING PPTUNIT TESTING PPT
UNIT TESTING PPT
 
Introduction to Automation Testing
Introduction to Automation TestingIntroduction to Automation Testing
Introduction to Automation Testing
 
testng
testngtestng
testng
 
JUnit 5
JUnit 5JUnit 5
JUnit 5
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first steps
 
TestNG with selenium
TestNG with seleniumTestNG with selenium
TestNG with selenium
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testing
 

Similar to Junit (20)

Junit
JunitJunit
Junit
 
J unit presentation
J unit presentationJ unit presentation
J unit presentation
 
8-testing.pptx
8-testing.pptx8-testing.pptx
8-testing.pptx
 
Junit 4.0
Junit 4.0Junit 4.0
Junit 4.0
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
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
 
Testing with Junit4
Testing with Junit4Testing with Junit4
Testing with Junit4
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Junit4&testng presentation
Junit4&testng presentationJunit4&testng presentation
Junit4&testng presentation
 
Junit With Eclipse
Junit With EclipseJunit With Eclipse
Junit With Eclipse
 
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 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
 
Junit and cactus
Junit and cactusJunit and cactus
Junit and cactus
 
Unit test
Unit testUnit test
Unit test
 
Unit Testing on Android - Droidcon Berlin 2015
Unit Testing on Android - Droidcon Berlin 2015Unit Testing on Android - Droidcon Berlin 2015
Unit Testing on Android - Droidcon Berlin 2015
 
Cpp unit
Cpp unit Cpp unit
Cpp unit
 
Unit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptxUnit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptx
 
Junit Test Cases
Junit Test CasesJunit Test Cases
Junit Test Cases
 
Introduction to JUnit testing in OpenDaylight
Introduction to JUnit testing in OpenDaylightIntroduction to JUnit testing in OpenDaylight
Introduction to JUnit testing in OpenDaylight
 

More from Manav Prasad (20)

Experience with mulesoft
Experience with mulesoftExperience with mulesoft
Experience with mulesoft
 
Mulesoftconnectors
MulesoftconnectorsMulesoftconnectors
Mulesoftconnectors
 
Mule and web services
Mule and web servicesMule and web services
Mule and web services
 
Mulesoft cloudhub
Mulesoft cloudhubMulesoft cloudhub
Mulesoft cloudhub
 
Perl tutorial
Perl tutorialPerl tutorial
Perl tutorial
 
Hibernate presentation
Hibernate presentationHibernate presentation
Hibernate presentation
 
Jpa
JpaJpa
Jpa
 
Spring introduction
Spring introductionSpring introduction
Spring introduction
 
Json
Json Json
Json
 
The spring framework
The spring frameworkThe spring framework
The spring framework
 
Rest introduction
Rest introductionRest introduction
Rest introduction
 
Exceptions in java
Exceptions in javaExceptions in java
Exceptions in java
 
Xml parsers
Xml parsersXml parsers
Xml parsers
 
Xpath
XpathXpath
Xpath
 
Xslt
XsltXslt
Xslt
 
Xhtml
XhtmlXhtml
Xhtml
 
Css
CssCss
Css
 
Introduction to html5
Introduction to html5Introduction to html5
Introduction to html5
 
Ajax
AjaxAjax
Ajax
 
J query
J queryJ query
J query
 

Recently uploaded

Microsoft BitLocker Bypass Attack Method.pdf
Microsoft BitLocker Bypass Attack Method.pdfMicrosoft BitLocker Bypass Attack Method.pdf
Microsoft BitLocker Bypass Attack Method.pdfOverkill Security
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontologyjohnbeverley2021
 
Top 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTop 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTopCSSGallery
 
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...FIDO Alliance
 
Vector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptxVector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptxjbellis
 
Intro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxIntro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxFIDO Alliance
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsLeah Henrickson
 
Cyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptx
Cyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptxCyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptx
Cyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptxMasterG
 
Introduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptxIntroduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptxFIDO Alliance
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxFIDO Alliance
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAnitaRaj43
 
Design Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptxDesign Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptxFIDO Alliance
 
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)Paige Cruz
 
Navigating the Large Language Model choices_Ravi Daparthi
Navigating the Large Language Model choices_Ravi DaparthiNavigating the Large Language Model choices_Ravi Daparthi
Navigating the Large Language Model choices_Ravi DaparthiRaviKumarDaparthi
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxMarkSteadman7
 
State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!Memoori
 
UiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overviewUiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overviewDianaGray10
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc
 
Microsoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireMicrosoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireExakis Nelite
 
The Ultimate Prompt Engineering Guide for Generative AI: Get the Most Out of ...
The Ultimate Prompt Engineering Guide for Generative AI: Get the Most Out of ...The Ultimate Prompt Engineering Guide for Generative AI: Get the Most Out of ...
The Ultimate Prompt Engineering Guide for Generative AI: Get the Most Out of ...SOFTTECHHUB
 

Recently uploaded (20)

Microsoft BitLocker Bypass Attack Method.pdf
Microsoft BitLocker Bypass Attack Method.pdfMicrosoft BitLocker Bypass Attack Method.pdf
Microsoft BitLocker Bypass Attack Method.pdf
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Top 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTop 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development Companies
 
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
 
Vector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptxVector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptx
 
Intro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxIntro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptx
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
 
Cyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptx
Cyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptxCyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptx
Cyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptx
 
Introduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptxIntroduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptx
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
Design Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptxDesign Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptx
 
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
 
Navigating the Large Language Model choices_Ravi Daparthi
Navigating the Large Language Model choices_Ravi DaparthiNavigating the Large Language Model choices_Ravi Daparthi
Navigating the Large Language Model choices_Ravi Daparthi
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptx
 
State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!
 
UiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overviewUiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overview
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
 
Microsoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireMicrosoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - Questionnaire
 
The Ultimate Prompt Engineering Guide for Generative AI: Get the Most Out of ...
The Ultimate Prompt Engineering Guide for Generative AI: Get the Most Out of ...The Ultimate Prompt Engineering Guide for Generative AI: Get the Most Out of ...
The Ultimate Prompt Engineering Guide for Generative AI: Get the Most Out of ...
 

Junit

  • 1. JUnit A tool for test-driven development
  • 2. History  Kent Beck developed the first xUnit automated test tool for Smalltalk in mid-90’s  Beck and Gamma (of design patterns Gang of Four) developed JUnit on a flight from Zurich to Washington, D.C.  Martin Fowler: “Never in the field of software development was so much owed by so many to so few lines of code.”  Junit has become the standard tool for Test-Driven Development in Java (see Junit.org)  Junit test generators now part of many Java IDEs (Eclipse, BlueJ, Jbuilder, DrJava)  Xunit tools have since been developed for many other languages (Perl, C++, Python, Visual Basic, C#, …)
  • 3. Why create a test suite?  Obviously you have to test your code—right?  You can do ad hoc testing (running whatever tests occur to you at the moment), or  You can build a test suite (a thorough set of tests that can be run at any time)  Disadvantages of a test suite  It’s a lot of extra programming • True, but use of a good test framework can help quite a bit  You don’t have time to do all that extra work • False! Experiments repeatedly show that test suites reduce debugging time more than the amount spent building the test suite  Advantages of a test suite  Reduces total number of bugs in delivered code  Makes code much more maintainable and refactorable
  • 4. Architectural overview  JUnit test framework is a package of classes that lets you write tests for each method, then easily run those tests  TestRunner runs tests and reports TestResults  You test your class by extending abstract class TestCase  To write test cases, you need to know and understand the Assert class
  • 5. Writing a TestCase  To start using JUnit, create a subclass of TestCase, to which you add test methods  Here’s a skeletal test class: import junit.framework.TestCase; public class TestBowl extends TestCase { } //Test my class Bowl  Name of class is important – should be of the form TestMyClass or MyClassTest  This naming convention lets TestRunner automatically find your test classes
  • 6. Writing methods in TestCase  Pattern follows programming by contract paradigm:  Set up preconditions  Exercise functionality being tested  Check postconditions  Example: public void testEmptyList() { Bowl emptyBowl = new Bowl(); assertEquals(“Size of an empty list should be zero.”, 0, emptyList.size()); assertTrue(“An empty bowl should report empty.”, emptyBowl.isEmpty()); }  Things to notice:  Specific method signature – public void testWhatever() • Allows them to be found and collected automatically by JUnit  Coding follows pattern  Notice the assert-type calls…
  • 7. Assert methods  Each assert method has parameters like these: message, expected-value, actual-value  Assert methods dealing with floating point numbers get an additional argument, a tolerance  Each assert method has an equivalent version that does not take a message – however, this use is not recommended because:  messages helps documents the tests  messages provide additional information when reading failure logs
  • 8. Assert methods  assertTrue(String message, Boolean test)  assertFalse(String message, Boolean test)  assertNull(String message, Object object)  assertNotNull(String message, Object object)  assertEquals(String message, Object expected, Object actual) (uses equals method)  assertSame(String message, Object expected, Object actual) (uses == operator)  assertNotSame(String message, Object expected, Object actual)
  • 9. More stuff in test classes  Suppose you want to test a class Counter  public class CounterTest extends junit.framework.TestCase {  This is the unit test for the Counter class  public CounterTest() { } //Default constructor  protected void setUp()  Test fixture creates and initializes instance variables, etc.  protected void tearDown()  Releases any system resources used by the test fixture  public void testIncrement(), public void testDecrement()  These methods contain tests for the Counter methods increment(), decrement(), etc.  Note capitalization convention
  • 10. JUnit tests for Counter public class CounterTest extends junit.framework.TestCase { Counter counter1; public CounterTest() { } // default constructor protected void setUp() { // creates a (simple) test fixture counter1 = new Counter(); } public void testIncrement() { assertTrue(counter1.increment() == 1); assertTrue(counter1.increment() == 2); } public void testDecrement() { assertTrue(counter1.decrement() == -1); } } Note that each test begins with a brand new counter This means you don’t have to worry about the order in which the tests are run
  • 11. TestSuites  TestSuites collect a selection of tests to run them as a unit  Collections automatically use TestSuites, however to specify the order in which tests are run, write your own: public static Test suite() { suite.addTest(new TestBowl(“testBowl”)); suite.addTest(new TestBowl(“testAdding”)); return suite; }  Should seldom have to write your own TestSuites as each method in your TestCase should be independent of all others  Can create TestSuites that test a whole package: public static Test suite() { TestSuite suite = new TestSuite(); suite.addTestSuite(TestBowl.class); suite.addTestSuite(TestFruit.class); return suite; }
  • 12. JUnit in Eclipse  To create a test class, select File→ New→ Other... → Java, JUnit, TestCase and enter the name of the class you will test Fill this in This will be filled in automatically
  • 13. Running JUnit First, select a Test classSecond, use this pulldown menu Third, Run As → JUnit Test
  • 15. Unit testing for other languages  Unit testing tools differentiate between:  Errors (unanticipated problems caught by exceptions)  Failures (anticipated problems checked with assertions)  Basic unit of testing:  CPPUNIT_ASSERT(Bool) examines an expression  CPPUnit has variety of test classes (e.g. TestFixture)  Inherit from them and overload methods
  • 16. More Information  http://www.junit.org Download of JUnit Lots of information on using JUnit