SlideShare une entreprise Scribd logo
1  sur  16
JUnit
A tool for test-driven developmen
Introduction
 Unit Testing tool for JAVA
 JUnit is an open source framework,
which is used for writing and running
tests. JUnit tests allow you to write
codes faster, which increases quality.
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…
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
Assert methods
 Assert is a method useful in determining Pass or
Fail status of a test case, The assert methods are
provided by the class org.junit.Assert which extends
java.lang.Object class. There are various types
of assertions like Boolean, Null, Identical etc.
 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
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)
Fixtures
 A test fixture is a fixed state of a set of objects used as a
baseline for running tests.
The purpose of a test fixture is to ensure that there is a well
known and fixed environment in which tests are run so that
results are repeatable. Examples of fixtures:
 Preparation of input data and setup/creation of fake or
mock objects
 JUnit provides annotations so that test classes can have
fixture run before or after every test, or one time fixtures that
run before and after only once for all test methods in a class.
Annotations
Here’re some basic JUnit annotations you should understand:
1.@BeforeClass – Run once before any of the
test methods in the class, public static void
2.@AfterClass – Run once after all the tests
in the class have been run, public static void
3.@Before – Run before @Test, public void
4.@After – Run after @Test, public void
5.@Test – This is the test method to run, public void
JUnit tests for Counter
public class CounterTest extends junit.framework.TestCase {
Counter counter1;
public CounterTest() { } // default constructor
@before
protected void setUp() { // creates a (simple) test fixture
counter1 = new Counter();
}
@test
public void testIncrement() {
assertTrue(counter1.increment() == 1);
assertTrue(counter1.increment() == 2);
}
@test
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
Runner class
 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
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;
}
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
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

Contenu connexe

Tendances (20)

JUnit 5
JUnit 5JUnit 5
JUnit 5
 
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
 
Junit
JunitJunit
Junit
 
3 j unit
3 j unit3 j unit
3 j unit
 
Java Unit Testing
Java Unit TestingJava Unit Testing
Java Unit Testing
 
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
 
Testing with Junit4
Testing with Junit4Testing with Junit4
Testing with Junit4
 
JUNit Presentation
JUNit PresentationJUNit Presentation
JUNit Presentation
 
Junit
JunitJunit
Junit
 
JUnit 5 - The Next Generation
JUnit 5 - The Next GenerationJUnit 5 - The Next Generation
JUnit 5 - The Next Generation
 
JUnit- A Unit Testing Framework
JUnit- A Unit Testing FrameworkJUnit- A Unit Testing Framework
JUnit- A Unit Testing Framework
 
Junit and testNG
Junit and testNGJunit and testNG
Junit and testNG
 
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
 
Advanced junit and mockito
Advanced junit and mockitoAdvanced junit and mockito
Advanced junit and mockito
 
JUnit Presentation
JUnit PresentationJUnit Presentation
JUnit Presentation
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first steps
 
JUnit Pioneer
JUnit PioneerJUnit Pioneer
JUnit Pioneer
 
Unit Testing in Java
Unit Testing in JavaUnit Testing in Java
Unit Testing in Java
 
Junit4&testng presentation
Junit4&testng presentationJunit4&testng presentation
Junit4&testng presentation
 

Similaire à Junit (20)

J unit presentation
J unit presentationJ unit presentation
J unit presentation
 
8-testing.pptx
8-testing.pptx8-testing.pptx
8-testing.pptx
 
Junit
JunitJunit
Junit
 
unit 1 (1).pptx
unit 1 (1).pptxunit 1 (1).pptx
unit 1 (1).pptx
 
Test ng tutorial
Test ng tutorialTest ng tutorial
Test ng tutorial
 
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)
 
TestNG vs Junit
TestNG vs JunitTestNG vs Junit
TestNG vs Junit
 
Junit With Eclipse
Junit With EclipseJunit With Eclipse
Junit With Eclipse
 
TestNG Session presented in PB
TestNG Session presented in PBTestNG Session presented in PB
TestNG Session presented in PB
 
Unit test
Unit testUnit test
Unit test
 
Unit testing
Unit testingUnit testing
Unit testing
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Introduction to JUnit testing in OpenDaylight
Introduction to JUnit testing in OpenDaylightIntroduction to JUnit testing in OpenDaylight
Introduction to JUnit testing in OpenDaylight
 
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
 
Pragmatic unittestingwithj unit
Pragmatic unittestingwithj unitPragmatic unittestingwithj unit
Pragmatic unittestingwithj unit
 
SE2_Lec 21_ TDD and Junit
SE2_Lec 21_ TDD and JunitSE2_Lec 21_ TDD and Junit
SE2_Lec 21_ TDD and Junit
 
jUnit
jUnitjUnit
jUnit
 
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
 
Unit testing php-unit - phing - selenium_v2
Unit testing   php-unit - phing - selenium_v2Unit testing   php-unit - phing - selenium_v2
Unit testing php-unit - phing - selenium_v2
 

Dernier

Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benonimasabamasaba
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...masabamasaba
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...chiefasafspells
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationJuha-Pekka Tolvanen
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxAnnaArtyushina1
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 

Dernier (20)

Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 

Junit

  • 1. JUnit A tool for test-driven developmen
  • 2. Introduction  Unit Testing tool for JAVA  JUnit is an open source framework, which is used for writing and running tests. JUnit tests allow you to write codes faster, which increases quality.
  • 3. 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
  • 4. 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…
  • 5. 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
  • 6. Assert methods  Assert is a method useful in determining Pass or Fail status of a test case, The assert methods are provided by the class org.junit.Assert which extends java.lang.Object class. There are various types of assertions like Boolean, Null, Identical etc.  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
  • 7. 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)
  • 8. Fixtures  A test fixture is a fixed state of a set of objects used as a baseline for running tests. The purpose of a test fixture is to ensure that there is a well known and fixed environment in which tests are run so that results are repeatable. Examples of fixtures:  Preparation of input data and setup/creation of fake or mock objects  JUnit provides annotations so that test classes can have fixture run before or after every test, or one time fixtures that run before and after only once for all test methods in a class.
  • 9. Annotations Here’re some basic JUnit annotations you should understand: 1.@BeforeClass – Run once before any of the test methods in the class, public static void 2.@AfterClass – Run once after all the tests in the class have been run, public static void 3.@Before – Run before @Test, public void 4.@After – Run after @Test, public void 5.@Test – This is the test method to run, public void
  • 10. JUnit tests for Counter public class CounterTest extends junit.framework.TestCase { Counter counter1; public CounterTest() { } // default constructor @before protected void setUp() { // creates a (simple) test fixture counter1 = new Counter(); } @test public void testIncrement() { assertTrue(counter1.increment() == 1); assertTrue(counter1.increment() == 2); } @test 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. Runner class  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
  • 12. 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; }
  • 13. 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
  • 14. 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
  • 15. Running JUnit First, select a Test classSecond, use this pulldown menu Third, Run As  JUnit Test