SlideShare a Scribd company logo
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

More Related Content

What's hot

JUnit 5
JUnit 5JUnit 5
Unit testing with JUnit
Unit testing with JUnitUnit testing with JUnit
Unit testing with JUnit
Thomas Zimmermann
 
Junit 4.0
Junit 4.0Junit 4.0
Junit
JunitJunit
3 j unit
3 j unit3 j unit
3 j unit
kishoregali
 
Java Unit Testing
Java Unit TestingJava Unit Testing
Java Unit Testing
Nayanda Haberty
 
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
Kiki Ahmadi
 
Testing with Junit4
Testing with Junit4Testing with Junit4
Testing with Junit4
Amila Paranawithana
 
JUNit Presentation
JUNit PresentationJUNit Presentation
JUNit Presentation
Animesh Kumar
 
Junit
JunitJunit
JUnit 5 - The Next Generation
JUnit 5 - The Next GenerationJUnit 5 - The Next Generation
JUnit 5 - The Next Generation
Kostadin Golev
 
JUnit- A Unit Testing Framework
JUnit- A Unit Testing FrameworkJUnit- A Unit Testing Framework
JUnit- A Unit Testing Framework
Onkar Deshpande
 
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
shaunthomas999
 
Advanced junit and mockito
Advanced junit and mockitoAdvanced junit and mockito
Advanced junit and mockito
Mathieu Carbou
 
JUnit Presentation
JUnit PresentationJUnit Presentation
JUnit Presentation
priya_trivedi
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first steps
Renato Primavera
 
JUnit Pioneer
JUnit PioneerJUnit Pioneer
JUnit Pioneer
Scott Leberknight
 
Unit Testing in Java
Unit Testing in JavaUnit Testing in Java
Unit Testing in Java
Ahmed M. Gomaa
 
Junit4&testng presentation
Junit4&testng presentationJunit4&testng presentation
Junit4&testng presentation
Sanjib Dhar
 

What's hot (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
 

Similar to Junit

J unit presentation
J unit presentationJ unit presentation
J unit presentation
Priya Sharma
 
8-testing.pptx
8-testing.pptx8-testing.pptx
8-testing.pptx
ssuserd0fdaa
 
Junit
JunitJunit
unit 1 (1).pptx
unit 1 (1).pptxunit 1 (1).pptx
unit 1 (1).pptx
SumitKumar918321
 
Test ng tutorial
Test ng tutorialTest ng tutorial
Test ng tutorial
Srikrishna k
 
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)
Amr E. Mohamed
 
TestNG vs Junit
TestNG vs JunitTestNG vs Junit
TestNG vs Junit
Büşra İçöz
 
Junit With Eclipse
Junit With EclipseJunit With Eclipse
Junit With Eclipse
Sunil kumar Mohanty
 
TestNG Session presented in PB
TestNG Session presented in PBTestNG Session presented in PB
TestNG Session presented in PB
Abhishek Yadav
 
Unit test
Unit testUnit test
Unit testing
Unit testingUnit testing
Unit testing
Pooya Sagharchiha
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Anand Kumar Rajana
 
Introduction to JUnit testing in OpenDaylight
Introduction to JUnit testing in OpenDaylightIntroduction to JUnit testing in OpenDaylight
Introduction to JUnit testing in OpenDaylight
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
Svetlin Nakov
 
Unit testing by Svetlin Nakov
Unit testing by Svetlin NakovUnit testing by Svetlin Nakov
Unit testing by Svetlin Nakov
it-tour
 
Pragmatic unittestingwithj unit
Pragmatic unittestingwithj unitPragmatic unittestingwithj unit
Pragmatic unittestingwithj unit
liminescence
 
SE2_Lec 21_ TDD and Junit
SE2_Lec 21_ TDD and JunitSE2_Lec 21_ TDD and Junit
SE2_Lec 21_ TDD and Junit
Amr E. Mohamed
 
jUnit
jUnitjUnit
jUnit
sundar22in
 
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
Knoldus Inc.
 
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
Tricode (part of Dept)
 

Similar to 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
 

Recently uploaded

一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
dakas1
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
Alina Yurenko
 
Lecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptxLecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptx
TaghreedAltamimi
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
mz5nrf0n
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Julian Hyde
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
ICS
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
Grant Fritchey
 
Preparing Non - Technical Founders for Engaging a Tech Agency
Preparing Non - Technical Founders for Engaging  a  Tech AgencyPreparing Non - Technical Founders for Engaging  a  Tech Agency
Preparing Non - Technical Founders for Engaging a Tech Agency
ISH Technologies
 
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
gapen1
 
Liberarsi dai framework con i Web Component.pptx
Liberarsi dai framework con i Web Component.pptxLiberarsi dai framework con i Web Component.pptx
Liberarsi dai framework con i Web Component.pptx
Massimo Artizzu
 
fiscal year variant fiscal year variant.
fiscal year variant fiscal year variant.fiscal year variant fiscal year variant.
fiscal year variant fiscal year variant.
AnkitaPandya11
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
kalichargn70th171
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
Quickdice ERP
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
Hornet Dynamics
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
Remote DBA Services
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
Green Software Development
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
dakas1
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
Philip Schwarz
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
brainerhub1
 
WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
Patrick Weigel
 

Recently uploaded (20)

一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
 
Lecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptxLecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptx
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
 
Preparing Non - Technical Founders for Engaging a Tech Agency
Preparing Non - Technical Founders for Engaging  a  Tech AgencyPreparing Non - Technical Founders for Engaging  a  Tech Agency
Preparing Non - Technical Founders for Engaging a Tech Agency
 
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
 
Liberarsi dai framework con i Web Component.pptx
Liberarsi dai framework con i Web Component.pptxLiberarsi dai framework con i Web Component.pptx
Liberarsi dai framework con i Web Component.pptx
 
fiscal year variant fiscal year variant.
fiscal year variant fiscal year variant.fiscal year variant fiscal year variant.
fiscal year variant fiscal year variant.
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
 
WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
 

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