SlideShare una empresa de Scribd logo
1 de 45
Learning  J Unit  for Unit Testing JUnit Tutorial Dr. Robert L. Probert S.I.T.E., University of Ottawa Sept. 2004 Acknowledgement
Contents ,[object Object],[object Object],[object Object],[object Object],[object Object]
Introduction ,[object Object],[object Object],[object Object]
The Problem, (The Vicious Cycle) ,[object Object]
Example ,[object Object],[object Object]
Example, Currency Handler Program ,[object Object],[object Object]
Money.class ,[object Object],[object Object],[object Object]
Money.class ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Money.class – Let’s Code ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],When you add two Moneys of the same currency, the resulting Money has as its amount the sum of the other two amounts.
Let’s Start Testing ,[object Object],[object Object]
Starting JUnit JUnit comes with a graphical interface to run tests.  To start up the JUnit interface, go in to DOS and set all the classpaths. You must set the classpath of the junit.jar file in DOS for JUnit to run.  Type: set classpath=%classpath%;INSTALL_DIRunit3.8.1unit.jar Also, you may need to set the classpath for your class directory. Type: set classpath=%classpath%;CLASS_DIR
Starting JUnit To run JUnit Type: for the batch TestRunner type:       java junit.textui.TestRunner NameOfTest for the graphical TestRunner type:       java junit.awtui.TestRunner NameOfTest for the Swing based graphical TestRunner type:       java junit.swingui.TestRunner NameOfTest
Testing Money.class ,[object Object],[object Object],[object Object],[object Object]
Testing Money.class (MoneyTest.class) import junit.framework.*; public class MoneyTest extends TestCase { //… public void testSimpleAdd() { Money m12CHF= new Money(12, "CHF");  // (1) Money m14CHF= new Money(14, "CHF");  Money expected= new Money(26, "CHF"); Money result= m12CHF.add(m14CHF);  // (2) Assert.assertTrue(expected.equals(result));  // (3) Assert.assertEquals(m12CHF, new Money(12, "CHF")); // (4) } } (1) Code which creates the objects we will interact with during the test. This testing context is commonly referred to as a test's  fixture . All we need for the testSimpleAdd test are some Money objects.  (2) Code which exercises the objects in the fixture.  (3) Code which verifies the results.  If not true, JUnit will return an error. (4) Since assertions for equality are very common, there is also an Assert.assertEquals convenience method.  If not equal JUnit will return an error.
Assert Function ,[object Object]
Assert Functions ,[object Object],[object Object],[object Object],[object Object]
Write the “equals” method in Money.class ,[object Object]
Write the “equals” method in Money.class public boolean equals(Object anObject) { if (anObject instanceof Money) { Money aMoney= (Money)anObject; return aMoney.currency().equals(currency()) && amount() == aMoney.amount(); } return false; } With an equals method in hand we can verify the outcome of testSimpleAdd. In JUnit you do so by a calling  Assert.assertTrue , which triggers a failure that is recorded by JUnit when the argument isn't true.
setUP Method For Testing ,[object Object]
MoneyTest.class public class MoneyTest extends TestCase { private Money f12CHF; private Money f14CHF;  protected void setUp() { f12CHF= new Money(12, "CHF"); f14CHF= new Money(14, "CHF"); } public void testEquals() { Assert.assertTrue(!f12CHF.equals(null)); Assert.assertEquals(f12CHF, f12CHF); Assert.assertEquals(f12CHF, new Money(12, "CHF")); Assert.assertTrue(!f12CHF.equals(f14CHF)); } public void testSimpleAdd() { Money expected= new Money(26, "CHF"); Money result= f12CHF.add(f14CHF); Assert.assertTrue(expected.equals(result)); } } setUp method for initializing inputs
Running Tests Statically or Dynamically ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Calling Tests Statically ,[object Object],TestCase test= new MoneyTest("simple add") { public void runTest() { testSimpleAdd(); } }
Calling Tests Dynamically ,[object Object],[object Object],TestCase test= new MoneyTest("testSimpleAdd");
Test Suites in JUnit ,[object Object]
Test Suites in JUnit public static Test suite() { TestSuite suite= new TestSuite(); suite.addTest(new MoneyTest("testEquals")); suite.addTest(new MoneyTest("testSimpleAdd")); return suite; } Since JUnit 2.0 there is an even simpler dynamic way. You only pass the class with the tests to a TestSuite and it extracts the test methods automatically.  public static Test suite() { return new TestSuite(MoneyTest.class); }
Static Test Suite Here is the corresponding code using the static way.  public static Test suite() { TestSuite suite= new TestSuite(); suite.addTest( new MoneyTest("money equals") { protected void runTest() { testEquals(); } } ); suite.addTest( new MoneyTest("simple add") { protected void runTest() { testSimpleAdd(); } } ); return suite; }
Running Your Tests To run JUnit Type: for the batch TestRunner type:       java junit.textui.TestRunner NameOfTest for the graphical TestRunner type:       java junit.awtui.TestRunner NameOfTest for the Swing based graphical TestRunner type:       java junit.swingui.TestRunner NameOfTest
Successful Test When the test results are valid and no errors are found, the progress bar will be completely green.  If there are 1 or more errors, the progress bar will turn red.  The errors and failures box will notify you to where to bug has occurred.
Example Continued (Money Bags) ,[object Object],[object Object],[object Object],[object Object]
MoneyBag.class class MoneyBag { private Vector fMonies= new Vector(); MoneyBag(Money m1, Money m2) { appendMoney(m1); appendMoney(m2); } MoneyBag(Money bag[]) { for (int i= 0; i < bag.length; i++) appendMoney(bag[i]); } } appendMoney is an internal helper method that adds a Money to the list of Moneys and takes care of consolidating Monies with the same currency
MoneyBag Test ,[object Object],protected void setUp() { f12CHF= new Money(12, &quot;CHF&quot;); f14CHF= new Money(14, &quot;CHF&quot;); f7USD=  new Money( 7, &quot;USD&quot;); f21USD= new Money(21, &quot;USD&quot;); fMB1= new MoneyBag(f12CHF, f7USD); fMB2= new MoneyBag(f14CHF, f21USD); } //With this fixture the testBagEquals test case becomes:  public void testBagEquals() { Assert.assertTrue(!fMB1.equals(null)); Assert.assertEquals(fMB1, fMB1); Assert.assertTrue(!fMB1.equals(f12CHF)); Assert.assertTrue(!f12CHF.equals(fMB1)); Assert.assertTrue(!fMB1.equals(fMB2)); }
Fixing the Add Method ,[object Object],[object Object],public Money add(Money m) { if (m.currency().equals(currency()) ) return new Money(amount()+m.amount(), currency()); return new MoneyBag(this, m); }
IMoney Interface ,[object Object],interface IMoney { public abstract IMoney add(IMoney aMoney); //… }
More Testing ,[object Object],public void testMixedSimpleAdd() {  // [12 CHF] + [7 USD] == {[12 CHF][7 USD]}  Money bag[]= { f12CHF, f7USD };  MoneyBag expected= new MoneyBag(bag);  Assert.assertEquals(expected, f12CHF.add(f7USD));  }
Update the Test Suite ,[object Object],[object Object],[object Object],[object Object],[object Object],public static Test suite() { TestSuite suite= new TestSuite(); suite.addTest(new MoneyTest(&quot;testMoneyEquals&quot;)); suite.addTest(new MoneyTest(&quot;testBagEquals&quot;)); suite.addTest(new MoneyTest(&quot;testSimpleAdd&quot;)); suite.addTest(new MoneyTest(&quot;testMixedSimpleAdd&quot;)); suite.addTest(new MoneyTest(&quot;testBagSimpleAdd&quot;)); suite.addTest(new MoneyTest(&quot;testSimpleBagAdd&quot;)); suite.addTest(new MoneyTest(&quot;testBagBagAdd&quot;)); return suite; }
Implementation ,[object Object],[object Object]
Implementation class Money implements IMoney { public IMoney add(IMoney m) { return m.addMoney(this); } //… } class MoneyBag implements IMoney { public IMoney add(IMoney m) { return m.addMoneyBag(this); } //… } In order to get this to compile we need to extend the interface of IMoney with the two helper methods:  interface IMoney { //… IMoney addMoney(Money aMoney); IMoney addMoneyBag(MoneyBag aMoneyBag); }
Implementation ,[object Object],public IMoney addMoney(Money m) { if (m.currency().equals(currency()) ) return new Money(amount()+m.amount(), currency()); return new MoneyBag(this, m); } public IMoney addMoneyBag(MoneyBag s) { return s.addMoney(this); }
Implementation ,[object Object],public IMoney addMoney(Money m) { return new MoneyBag(m, this); } public IMoney addMoneyBag(MoneyBag s) { return new MoneyBag(s, this); }
Are there more errors that can occur? ,[object Object]
Review ,[object Object]
Review ,[object Object],[object Object]
Testing Practices ,[object Object]
Testing Practices ,[object Object]
Testing Practices ,[object Object],[object Object],[object Object]

Más contenido relacionado

La actualidad más candente

Beginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NETBeginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NETBaskar K
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first stepsRenato Primavera
 
TestNG - The Next Generation of Unit Testing
TestNG - The Next Generation of Unit TestingTestNG - The Next Generation of Unit Testing
TestNG - The Next Generation of Unit TestingBethmi Gunasekara
 
Mocking in Java with Mockito
Mocking in Java with MockitoMocking in Java with Mockito
Mocking in Java with MockitoRichard Paul
 
TestNG Annotations in Selenium | Edureka
TestNG Annotations in Selenium | EdurekaTestNG Annotations in Selenium | Edureka
TestNG Annotations in Selenium | EdurekaEdureka!
 
Testing with Spring: An Introduction
Testing with Spring: An IntroductionTesting with Spring: An Introduction
Testing with Spring: An IntroductionSam Brannen
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And MockingJoe Wilson
 
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and MockitoAn Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockitoshaunthomas999
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDDDror Helper
 
Unit testing best practices
Unit testing best practicesUnit testing best practices
Unit testing best practicesnickokiss
 

La actualidad más candente (20)

Introduction to JUnit
Introduction to JUnitIntroduction to JUnit
Introduction to JUnit
 
JUnit Presentation
JUnit PresentationJUnit Presentation
JUnit Presentation
 
Unit Testing in Java
Unit Testing in JavaUnit Testing in Java
Unit Testing in Java
 
JUnit 5
JUnit 5JUnit 5
JUnit 5
 
Beginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NETBeginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NET
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first steps
 
Junit
JunitJunit
Junit
 
TestNG - The Next Generation of Unit Testing
TestNG - The Next Generation of Unit TestingTestNG - The Next Generation of Unit Testing
TestNG - The Next Generation of Unit Testing
 
Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674
 
Unit testing
Unit testingUnit testing
Unit testing
 
TestNG with selenium
TestNG with seleniumTestNG with selenium
TestNG with selenium
 
Mocking in Java with Mockito
Mocking in Java with MockitoMocking in Java with Mockito
Mocking in Java with Mockito
 
TestNG Annotations in Selenium | Edureka
TestNG Annotations in Selenium | EdurekaTestNG Annotations in Selenium | Edureka
TestNG Annotations in Selenium | Edureka
 
Testing with Spring: An Introduction
Testing with Spring: An IntroductionTesting with Spring: An Introduction
Testing with Spring: An Introduction
 
Unit Test
Unit TestUnit Test
Unit Test
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And Mocking
 
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and MockitoAn Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
 
TestNG
TestNGTestNG
TestNG
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDD
 
Unit testing best practices
Unit testing best practicesUnit testing best practices
Unit testing best practices
 

Destacado

Destacado (8)

Test Driven Development and JUnit
Test Driven Development and JUnitTest Driven Development and JUnit
Test Driven Development and JUnit
 
JUnit Sample
JUnit SampleJUnit Sample
JUnit Sample
 
JUnit PowerUp
JUnit PowerUpJUnit PowerUp
JUnit PowerUp
 
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
 
Unit testing with Junit
Unit testing with JunitUnit testing with Junit
Unit testing with Junit
 
Junit 4.0
Junit 4.0Junit 4.0
Junit 4.0
 
Introduction To UnitTesting & JUnit
Introduction To UnitTesting & JUnitIntroduction To UnitTesting & JUnit
Introduction To UnitTesting & JUnit
 
Android Unit Tesing at I/O rewind 2015
Android Unit Tesing at I/O rewind 2015Android Unit Tesing at I/O rewind 2015
Android Unit Tesing at I/O rewind 2015
 

Similar a Junit

Similar a Junit (20)

J unit a starter guide
J unit a starter guideJ unit a starter guide
J unit a starter guide
 
J unit presentation
J unit presentationJ unit presentation
J unit presentation
 
Junit4&testng presentation
Junit4&testng presentationJunit4&testng presentation
Junit4&testng presentation
 
Junit
JunitJunit
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)
 
Junit With Eclipse
Junit With EclipseJunit With Eclipse
Junit With Eclipse
 
Testing Options in Java
Testing Options in JavaTesting Options in Java
Testing Options in Java
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Effective Unit Test Style Guide
Effective Unit Test Style GuideEffective Unit Test Style Guide
Effective Unit Test Style Guide
 
unit 1 (1).pptx
unit 1 (1).pptxunit 1 (1).pptx
unit 1 (1).pptx
 
Unit testing
Unit testingUnit testing
Unit testing
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Google mock training
Google mock trainingGoogle mock training
Google mock training
 
Junit and cactus
Junit and cactusJunit and cactus
Junit and cactus
 
maXbox Starter 36 Software Testing
maXbox Starter 36 Software TestingmaXbox Starter 36 Software Testing
maXbox Starter 36 Software Testing
 
J Unit
J UnitJ Unit
J Unit
 
3 j unit
3 j unit3 j unit
3 j unit
 
Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit Testing
 
8-testing.pptx
8-testing.pptx8-testing.pptx
8-testing.pptx
 
SE2_Lec 21_ TDD and Junit
SE2_Lec 21_ TDD and JunitSE2_Lec 21_ TDD and Junit
SE2_Lec 21_ TDD and Junit
 

Junit

  • 1. Learning J Unit for Unit Testing JUnit Tutorial Dr. Robert L. Probert S.I.T.E., University of Ottawa Sept. 2004 Acknowledgement
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11. Starting JUnit JUnit comes with a graphical interface to run tests. To start up the JUnit interface, go in to DOS and set all the classpaths. You must set the classpath of the junit.jar file in DOS for JUnit to run. Type: set classpath=%classpath%;INSTALL_DIRunit3.8.1unit.jar Also, you may need to set the classpath for your class directory. Type: set classpath=%classpath%;CLASS_DIR
  • 12. Starting JUnit To run JUnit Type: for the batch TestRunner type:     java junit.textui.TestRunner NameOfTest for the graphical TestRunner type:     java junit.awtui.TestRunner NameOfTest for the Swing based graphical TestRunner type:     java junit.swingui.TestRunner NameOfTest
  • 13.
  • 14. Testing Money.class (MoneyTest.class) import junit.framework.*; public class MoneyTest extends TestCase { //… public void testSimpleAdd() { Money m12CHF= new Money(12, &quot;CHF&quot;); // (1) Money m14CHF= new Money(14, &quot;CHF&quot;); Money expected= new Money(26, &quot;CHF&quot;); Money result= m12CHF.add(m14CHF); // (2) Assert.assertTrue(expected.equals(result)); // (3) Assert.assertEquals(m12CHF, new Money(12, &quot;CHF&quot;)); // (4) } } (1) Code which creates the objects we will interact with during the test. This testing context is commonly referred to as a test's fixture . All we need for the testSimpleAdd test are some Money objects. (2) Code which exercises the objects in the fixture. (3) Code which verifies the results. If not true, JUnit will return an error. (4) Since assertions for equality are very common, there is also an Assert.assertEquals convenience method. If not equal JUnit will return an error.
  • 15.
  • 16.
  • 17.
  • 18. Write the “equals” method in Money.class public boolean equals(Object anObject) { if (anObject instanceof Money) { Money aMoney= (Money)anObject; return aMoney.currency().equals(currency()) && amount() == aMoney.amount(); } return false; } With an equals method in hand we can verify the outcome of testSimpleAdd. In JUnit you do so by a calling Assert.assertTrue , which triggers a failure that is recorded by JUnit when the argument isn't true.
  • 19.
  • 20. MoneyTest.class public class MoneyTest extends TestCase { private Money f12CHF; private Money f14CHF; protected void setUp() { f12CHF= new Money(12, &quot;CHF&quot;); f14CHF= new Money(14, &quot;CHF&quot;); } public void testEquals() { Assert.assertTrue(!f12CHF.equals(null)); Assert.assertEquals(f12CHF, f12CHF); Assert.assertEquals(f12CHF, new Money(12, &quot;CHF&quot;)); Assert.assertTrue(!f12CHF.equals(f14CHF)); } public void testSimpleAdd() { Money expected= new Money(26, &quot;CHF&quot;); Money result= f12CHF.add(f14CHF); Assert.assertTrue(expected.equals(result)); } } setUp method for initializing inputs
  • 21.
  • 22.
  • 23.
  • 24.
  • 25. Test Suites in JUnit public static Test suite() { TestSuite suite= new TestSuite(); suite.addTest(new MoneyTest(&quot;testEquals&quot;)); suite.addTest(new MoneyTest(&quot;testSimpleAdd&quot;)); return suite; } Since JUnit 2.0 there is an even simpler dynamic way. You only pass the class with the tests to a TestSuite and it extracts the test methods automatically. public static Test suite() { return new TestSuite(MoneyTest.class); }
  • 26. Static Test Suite Here is the corresponding code using the static way. public static Test suite() { TestSuite suite= new TestSuite(); suite.addTest( new MoneyTest(&quot;money equals&quot;) { protected void runTest() { testEquals(); } } ); suite.addTest( new MoneyTest(&quot;simple add&quot;) { protected void runTest() { testSimpleAdd(); } } ); return suite; }
  • 27. Running Your Tests To run JUnit Type: for the batch TestRunner type:     java junit.textui.TestRunner NameOfTest for the graphical TestRunner type:     java junit.awtui.TestRunner NameOfTest for the Swing based graphical TestRunner type:     java junit.swingui.TestRunner NameOfTest
  • 28. Successful Test When the test results are valid and no errors are found, the progress bar will be completely green. If there are 1 or more errors, the progress bar will turn red. The errors and failures box will notify you to where to bug has occurred.
  • 29.
  • 30. MoneyBag.class class MoneyBag { private Vector fMonies= new Vector(); MoneyBag(Money m1, Money m2) { appendMoney(m1); appendMoney(m2); } MoneyBag(Money bag[]) { for (int i= 0; i < bag.length; i++) appendMoney(bag[i]); } } appendMoney is an internal helper method that adds a Money to the list of Moneys and takes care of consolidating Monies with the same currency
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37. Implementation class Money implements IMoney { public IMoney add(IMoney m) { return m.addMoney(this); } //… } class MoneyBag implements IMoney { public IMoney add(IMoney m) { return m.addMoneyBag(this); } //… } In order to get this to compile we need to extend the interface of IMoney with the two helper methods: interface IMoney { //… IMoney addMoney(Money aMoney); IMoney addMoneyBag(MoneyBag aMoneyBag); }
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.