A quick introduction to Unit Testing with JUnit Cleveland Java Sig March 13, 2002 [email_address]
Extreme Programming  The other XP The Planning Game Small Releases Metaphor Simple Design Testing Refactoring Pair Programming Collective Ownership Continuous Integration 40-Hour week On-Site Customer Coding Standards
In XP how you get started Start with a User Story Break the User Story into tasks Estimate and sign up for tasks Find someone to pair with you on a task
The process of Testing First pick a small goal within the task think of how you'll accomplish this goal write the code that will test this goal (several of these may be needed -- write just one)
Example -- Conway's Life Assume... There is infrastructure to run the testing framework. A suite of some sort will keep track of all of the classes containing tests that need to be run. Each of these classes will extend TestCase and will use reflection to run any method named textXXX()
Example -- Conway's Life Start by writing the test code first...
Example -- Conway's Life public void testNewCellIsDead(){ Cell cell = new Cell(); assertTrue(!cell.isAlive()); }
Example -- Conway's Life public void testNewCellIsDead(){ Cell cell = new Cell(); assertTrue(! cell.isAlive() ); } This won't even compile
Next task in Testing first Get the test code to compile. We need Cell class with a Cell() no argument constructor and a isAlive() method that returns a boolean Note how much we know about the code we need just from the test we've written In XP we do the simplest thing that could possibly work
Cell class public class Cell { protected boolean isAlive(){ return true; } }
Test compiles but fails Compile the code (minor success) Run the tests  The test fails -- in fact it's often a good idea to make sure that your test fails before it passes so that you know it's actually being called simplest thing might be just to change return to false
Temporary success Tests pass
Another test public void testLiveCellIsAlive(){ Cell cell = new Cell(); cell. setAlive(true); assertTrue(cell. isAlive() ); }
Need to: add the setAlive() method to Cell so code compiles fix isAlive() method so that both tests pass let's assume you can do this
Clean up test code a little public void testNewCellIsDead(){ Cell cell = new Cell(); assertTrue(!cell.isAlive()); } public void testLiveCellIsAlive(){ Cell cell = new Cell(); cell.setAlive(true);  assertTrue(cell.isAlive()); }
Result import junit.framework.TestCase; public class TestCell extends TestCase{ Cell cell; //more...
Result (cont.) public TestCell(String name){ super(name); } protected void setUp() { cell = new Cell(); }  //more...
Result public void testNewCellIsDead(){ assertTrue(!cell.isAlive()); } public void testLiveCellIsAlive(){ cell.setAlive(true); assertTrue(cell.isAlive()); } }
The Test Suite import junit.framework.Test; import junit.framework.TestSuite; public class AllTests { public static void main  (String[] args) { junit.textui.TestRunner. run (suite()); } //more...
The Test Suite (cont.) public static Test suite ( ) { TestSuite suite= new TestSuite("All JUnit Tests"); suite.addTest( new TestSuite( life.model.TestCell.class ) ); return suite; } }
Running the suite Want tests automated so you can run them all the time Can use from ANT and other tools also Can just run AllTests (it has a main())
From Command line
From within IDE
Using GUI TestRunner
Show Failures
Show Test Hierarchy
Notes JUnit is available at  www.junit.org   Other languages have other frameworks Challenges in the J2ME world Challenges in distributed world Mock objects and other solutions Issues with acceptance tests
 
Resources JUnit is available at  www.junit.org   More resources at  www. xprogramming .com   Contact me at DSteinberg@core.com

More Related Content

PPT
Unit testing with Spock Framework
PDF
Spock Framework
PPTX
Smarter Testing With Spock
PDF
Javascript Unit Testing
PPTX
J unit스터디슬라이드
PPTX
TestNG vs Junit
PDF
JUnit & Mockito, first steps
Unit testing with Spock Framework
Spock Framework
Smarter Testing With Spock
Javascript Unit Testing
J unit스터디슬라이드
TestNG vs Junit
JUnit & Mockito, first steps

What's hot (20)

PPTX
TestNG with selenium
PDF
Python unittest
PPTX
Introduction of TestNG framework and its benefits over Junit framework
PDF
Unit testing with JUnit
PDF
TestNG introduction
PPT
PPTX
Test ng tutorial
PPT
Simple Unit Testing With Netbeans 6.1
PPT
Mxunit
PDF
TestNG vs. JUnit4
PPTX
TestNG Framework
PDF
Sample Chapter of Practical Unit Testing with TestNG and Mockito
PPTX
Junit4&testng presentation
PPT
Python testing
PPTX
Introduction to JUnit
PPTX
Test NG Framework Complete Walk Through
PDF
An introduction to Google test framework
PDF
Test ng for testers
TestNG with selenium
Python unittest
Introduction of TestNG framework and its benefits over Junit framework
Unit testing with JUnit
TestNG introduction
Test ng tutorial
Simple Unit Testing With Netbeans 6.1
Mxunit
TestNG vs. JUnit4
TestNG Framework
Sample Chapter of Practical Unit Testing with TestNG and Mockito
Junit4&testng presentation
Python testing
Introduction to JUnit
Test NG Framework Complete Walk Through
An introduction to Google test framework
Test ng for testers
Ad

Viewers also liked (6)

PDF
Présentation Haute Maurienne
PPT
Universal American School
ODP
VERTEBRADOS
PPT
Virtual Tour of the Solar System
PPT
PPS
Derecho de Autor en España
Présentation Haute Maurienne
Universal American School
VERTEBRADOS
Virtual Tour of the Solar System
Derecho de Autor en España
Ad

Similar to jUnit (20)

PDF
junit-160729073220 eclipse software testing.pdf
PPT
05 junit
PPSX
PPTX
8-testing.pptx
PPS
J unit presentation
PPS
JUnit Presentation
PPTX
Testing with Junit4
PPTX
unit 1 (1).pptx
PPT
3 j unit
DOCX
Junit With Eclipse
PPT
PPTX
Unit Testing in Java
PDF
L08 Unit Testing
PDF
Unit Testing on Android - Droidcon Berlin 2015
PPTX
Le Tour de xUnit
PDF
J unit a starter guide
PDF
GeeCON 2012 Bad Tests, Good Tests
PDF
Unit testing with Junit
PPTX
PDF
Confitura 2012 Bad Tests, Good Tests
junit-160729073220 eclipse software testing.pdf
05 junit
8-testing.pptx
J unit presentation
JUnit Presentation
Testing with Junit4
unit 1 (1).pptx
3 j unit
Junit With Eclipse
Unit Testing in Java
L08 Unit Testing
Unit Testing on Android - Droidcon Berlin 2015
Le Tour de xUnit
J unit a starter guide
GeeCON 2012 Bad Tests, Good Tests
Unit testing with Junit
Confitura 2012 Bad Tests, Good Tests

Recently uploaded (20)

PPTX
Modernising the Digital Integration Hub
PPTX
Tartificialntelligence_presentation.pptx
PDF
1 - Historical Antecedents, Social Consideration.pdf
PPT
Module 1.ppt Iot fundamentals and Architecture
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PDF
Hybrid model detection and classification of lung cancer
PDF
WOOl fibre morphology and structure.pdf for textiles
PPTX
Web Crawler for Trend Tracking Gen Z Insights.pptx
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
August Patch Tuesday
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PDF
STKI Israel Market Study 2025 version august
PPTX
Chapter 5: Probability Theory and Statistics
PDF
Getting Started with Data Integration: FME Form 101
PDF
DP Operators-handbook-extract for the Mautical Institute
PDF
sustainability-14-14877-v2.pddhzftheheeeee
PDF
Zenith AI: Advanced Artificial Intelligence
PDF
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
Modernising the Digital Integration Hub
Tartificialntelligence_presentation.pptx
1 - Historical Antecedents, Social Consideration.pdf
Module 1.ppt Iot fundamentals and Architecture
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
Hybrid model detection and classification of lung cancer
WOOl fibre morphology and structure.pdf for textiles
Web Crawler for Trend Tracking Gen Z Insights.pptx
Group 1 Presentation -Planning and Decision Making .pptx
August Patch Tuesday
A contest of sentiment analysis: k-nearest neighbor versus neural network
STKI Israel Market Study 2025 version august
Chapter 5: Probability Theory and Statistics
Getting Started with Data Integration: FME Form 101
DP Operators-handbook-extract for the Mautical Institute
sustainability-14-14877-v2.pddhzftheheeeee
Zenith AI: Advanced Artificial Intelligence
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game

jUnit

  • 1. A quick introduction to Unit Testing with JUnit Cleveland Java Sig March 13, 2002 [email_address]
  • 2. Extreme Programming The other XP The Planning Game Small Releases Metaphor Simple Design Testing Refactoring Pair Programming Collective Ownership Continuous Integration 40-Hour week On-Site Customer Coding Standards
  • 3. In XP how you get started Start with a User Story Break the User Story into tasks Estimate and sign up for tasks Find someone to pair with you on a task
  • 4. The process of Testing First pick a small goal within the task think of how you'll accomplish this goal write the code that will test this goal (several of these may be needed -- write just one)
  • 5. Example -- Conway's Life Assume... There is infrastructure to run the testing framework. A suite of some sort will keep track of all of the classes containing tests that need to be run. Each of these classes will extend TestCase and will use reflection to run any method named textXXX()
  • 6. Example -- Conway's Life Start by writing the test code first...
  • 7. Example -- Conway's Life public void testNewCellIsDead(){ Cell cell = new Cell(); assertTrue(!cell.isAlive()); }
  • 8. Example -- Conway's Life public void testNewCellIsDead(){ Cell cell = new Cell(); assertTrue(! cell.isAlive() ); } This won't even compile
  • 9. Next task in Testing first Get the test code to compile. We need Cell class with a Cell() no argument constructor and a isAlive() method that returns a boolean Note how much we know about the code we need just from the test we've written In XP we do the simplest thing that could possibly work
  • 10. Cell class public class Cell { protected boolean isAlive(){ return true; } }
  • 11. Test compiles but fails Compile the code (minor success) Run the tests The test fails -- in fact it's often a good idea to make sure that your test fails before it passes so that you know it's actually being called simplest thing might be just to change return to false
  • 13. Another test public void testLiveCellIsAlive(){ Cell cell = new Cell(); cell. setAlive(true); assertTrue(cell. isAlive() ); }
  • 14. Need to: add the setAlive() method to Cell so code compiles fix isAlive() method so that both tests pass let's assume you can do this
  • 15. Clean up test code a little public void testNewCellIsDead(){ Cell cell = new Cell(); assertTrue(!cell.isAlive()); } public void testLiveCellIsAlive(){ Cell cell = new Cell(); cell.setAlive(true); assertTrue(cell.isAlive()); }
  • 16. Result import junit.framework.TestCase; public class TestCell extends TestCase{ Cell cell; //more...
  • 17. Result (cont.) public TestCell(String name){ super(name); } protected void setUp() { cell = new Cell(); } //more...
  • 18. Result public void testNewCellIsDead(){ assertTrue(!cell.isAlive()); } public void testLiveCellIsAlive(){ cell.setAlive(true); assertTrue(cell.isAlive()); } }
  • 19. The Test Suite import junit.framework.Test; import junit.framework.TestSuite; public class AllTests { public static void main (String[] args) { junit.textui.TestRunner. run (suite()); } //more...
  • 20. The Test Suite (cont.) public static Test suite ( ) { TestSuite suite= new TestSuite("All JUnit Tests"); suite.addTest( new TestSuite( life.model.TestCell.class ) ); return suite; } }
  • 21. Running the suite Want tests automated so you can run them all the time Can use from ANT and other tools also Can just run AllTests (it has a main())
  • 27. Notes JUnit is available at www.junit.org Other languages have other frameworks Challenges in the J2ME world Challenges in distributed world Mock objects and other solutions Issues with acceptance tests
  • 28.  
  • 29. Resources JUnit is available at www.junit.org More resources at www. xprogramming .com Contact me at DSteinberg@core.com