SlideShare a Scribd company logo
1 of 32
CSE376
UNIT - 1
What Is Automation Testing?
 Automation Testing is a technique using an
automation tool to write and execute tester's test
scripts and cases..
 The testing tool takes actual outcomes and compares it
with the expected result to generate detailed test
reports.
 Since test automation demands a considerable
investment of resources and money,
 We should define:
1. when to do automation,
2. the scope of the automation
3. best tool of automation.
 Good planning practices can save us from high costs.
In the automation process, steps involved
are
1. Selecting the Test tool
2. Define scope of automation
3. Planning, design, and development
4. Test execution
5. Maintenance
 Testing is an essential part of product development,
especially to guarantee quality.
 Many smaller and mid-sized companies don’t give testing
much attention even though it’s essential for delivering a
strong product.
 Some companies prefer to do manual testing, although
that’s not the only best approach.
 The next logical step is automating your testing process.
 Manual testing should be minimized at all costs.
 Test automation increases overall software development
efficiency and allows for more robust tools to be built.
Benefits
 Faster Feedback Cycle
 Saves Time
 Reduced Business Expenses
 Higher Test Coverage
 Reusability of Test Suite
 Faster Time to Market
 Improved Accuracy
 Less Stress on QA Team
 Quick Determination the Stability of Your Build
 Eliminate Human Error
Benefits
 Ability To Perform Testing On Multiple Platforms In Parallel
 Accelerate Cross Browser Testing
 Distributed Test Execution
 Data Driven Testing
Most popular tools for automation
testing
 The most popular test tool for automation testing are
• QTP (HP UFT)
• Rational Robot
• Selenium
Why JUnit?
 JUnit is one of the most popular unit testing frameworks for Java development.
 Unit testing is an important part in Test Driven Development (TDD) as it helps
finding problems in the code as early as possible, especially when you make
changes to the existing code you can run unit tests again to make sure that the
changes do not break the application (regression).
 JUnit is supported by almost any Java IDEs and build tools, thus it is the default
choice of programmers to test their code.
 Eclipse has very good support for JUnit - the IDE is shipped with JUnit as its
default testing library.
 Thus writing and running unit tests with JUnit in Eclipse is quick, easy and
productive.
Structure of a Test Class
This Calculator class has two methods add() and subtract().
Now we want to write a test class to ensure that these
methods are working correctly. With JUnit, the test class
can have the following structure:
Annotations used in this test class
 @BeforeClass:
 Code in this method is executed only once, before all test methods in the test
class. Typically you put code that prepares test environment here, e.g.
initializing resources such as opening a database connection. The method
marked with this annotation must be static.

 @Before:
 Code in this method is executed before every test method in the test class. So
typically you put repeated code that must be executed before each test method
here.

 @Test:
 This annotation specifies the annotated method is a test method. You can notice the
naming convention for test method is the method name starts with test, followed by
the name of the method in the class being tested. This Calculator class has two
methods add() and subtract(), hence the test methods
are testAdd() and testSubtract().
 @After:
 Code in this method is executed after every test method in the test class. So typically
you put repeated code that must be executed after each test method here.
NOTE:
The above-mentioned annotations are in JUnit 4.
 @AfterClass:
 Code in this method is executed only once, after all test methods in the test
class. Due to this behavior, you can put code to clean up test environment here,
e.g. closing the database connection. Note that this kind of method must be
static.
 Except the test methods, the other kinds of method are optional. So you
implement them in the test class only if it is necessary.
JUnit 5 changes in annotations
 From JUnit 5,
 @BeforeClass changes to @BeforeAll;
 @Before changes to @BeforeEach;
 @After changes to @AfterEach;
 @AfterClass changes to @AfterAll.
 This change in JUnit 5 reflects better meaning of the annotations.
With JUnit 5, the structure of a test class looks like this:
To be noted:
 In JUnit 4, you need to import the annotations from
the org.junit package, but in JUnit 5 you need to import them from
the org.junit.jupiter.api package.
Using Assert Statement in Test Methods
 In a test method, we use an assert statement to verify whether the code under
test produces the result as expected or not.
 If the result is as expected, the tested code passes the test case. Otherwise
the tested code fails the test case.
 We use the assertEquals() method from the class org.junit.Assert via
static import:
 you can use the fail() method to fail the test immediately, for example:
Use the fail() method to indicate that a method is not
yet implemented so when the test class is executed, it will
fail.
 The assertEquals() method compares two values: the expected result
and the actual value returned from the tested code.
 If the two values are equal, the method returns normally indicating the tested
code passes the test case.
 If the two values are not equal, the method
throws java.lang.AssertionError indicating the tested code fails the
test case.
 And programmers must fix the code until it passes the test case.
JUnit provides many assert()methods, and
here are some commonly used ones:
• assertEquals(expected, actual): asserts that two values are equal. The values
can be of any primitive types or Objects.
• assertNotEquals(expected, actual): asserts that two arguments are not equal.
The values can be of any primitive types or Object.
• assertTrue(boolean condition): asserts that a condition is true.
• assertFalse(boolean condition): asserts that a condition is false.
• assertNull(Object): asserts that an object is null.
• assertNotNull(Object): asserts that an object is not null.
• assertSame(Object expected, Object actual): asserts that two objects
refer to the same object.
• assertNotSame(Object unexpected, Object actual): asserts that two
objects do not refer to the same object.
You can see the green indicator bar in the upper-right
corner indicating that the test case has been executed
successfully - the code under test passes the test case.
If the test fails, the status indicator becomes red and the
failure trace is printed:
This is the result of running the testSubtract() method,
The class Calculator passes two test methods in the CalculatorTest class.

More Related Content

Similar to unit 1 (1).pptx

Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++Hong Le Van
 
Java Unit Test - JUnit
Java Unit Test - JUnitJava Unit Test - JUnit
Java Unit Test - JUnitAktuğ Urun
 
Test driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + EclipseTest driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + EclipseUTC Fire & Security
 
Unit testing
Unit testingUnit testing
Unit testingmedsherb
 
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.pptxKnoldus Inc.
 
Unit Testng with PHP Unit - A Step by Step Training
Unit Testng with PHP Unit - A Step by Step TrainingUnit Testng with PHP Unit - A Step by Step Training
Unit Testng with PHP Unit - A Step by Step TrainingRam Awadh Prasad, PMP
 
Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.Mohamed Taman
 
AUTOCODECOVERGEN: PROTOTYPE OF DATA DRIVEN UNIT TEST GENRATION TOOL THAT GUAR...
AUTOCODECOVERGEN: PROTOTYPE OF DATA DRIVEN UNIT TEST GENRATION TOOL THAT GUAR...AUTOCODECOVERGEN: PROTOTYPE OF DATA DRIVEN UNIT TEST GENRATION TOOL THAT GUAR...
AUTOCODECOVERGEN: PROTOTYPE OF DATA DRIVEN UNIT TEST GENRATION TOOL THAT GUAR...acijjournal
 
Effective Unit Test Style Guide
Effective Unit Test Style GuideEffective Unit Test Style Guide
Effective Unit Test Style GuideJacky Lai
 

Similar to unit 1 (1).pptx (20)

Unit testing with JUnit
Unit testing with JUnitUnit testing with JUnit
Unit testing with JUnit
 
Introduction to JUnit
Introduction to JUnitIntroduction to JUnit
Introduction to JUnit
 
TDD Workshop UTN 2012
TDD Workshop UTN 2012TDD Workshop UTN 2012
TDD Workshop UTN 2012
 
Workshop unit test
Workshop   unit testWorkshop   unit test
Workshop unit test
 
Testing
TestingTesting
Testing
 
Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++
 
Junit
JunitJunit
Junit
 
Java Unit Test - JUnit
Java Unit Test - JUnitJava Unit Test - JUnit
Java Unit Test - JUnit
 
software testing
software testingsoftware testing
software testing
 
Test driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + EclipseTest driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + Eclipse
 
8-testing.pptx
8-testing.pptx8-testing.pptx
8-testing.pptx
 
Unit testing
Unit testingUnit testing
Unit testing
 
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 Testng with PHP Unit - A Step by Step Training
Unit Testng with PHP Unit - A Step by Step TrainingUnit Testng with PHP Unit - A Step by Step Training
Unit Testng with PHP Unit - A Step by Step Training
 
Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.
 
AUTOCODECOVERGEN: PROTOTYPE OF DATA DRIVEN UNIT TEST GENRATION TOOL THAT GUAR...
AUTOCODECOVERGEN: PROTOTYPE OF DATA DRIVEN UNIT TEST GENRATION TOOL THAT GUAR...AUTOCODECOVERGEN: PROTOTYPE OF DATA DRIVEN UNIT TEST GENRATION TOOL THAT GUAR...
AUTOCODECOVERGEN: PROTOTYPE OF DATA DRIVEN UNIT TEST GENRATION TOOL THAT GUAR...
 
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
 
Test automation
Test automationTest automation
Test automation
 
TDD Best Practices
TDD Best PracticesTDD Best Practices
TDD Best Practices
 

Recently uploaded

CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service 🧵
CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service  🧵CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service  🧵
CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service 🧵anilsa9823
 
Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...
Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...
Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...babafaisel
 
Petrosains Drama Competition (PSDC).pptx
Petrosains Drama Competition (PSDC).pptxPetrosains Drama Competition (PSDC).pptx
Petrosains Drama Competition (PSDC).pptxIgnatiusAbrahamBalin
 
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)amitlee9823
 
Design Inspiration for College by Slidesgo.pptx
Design Inspiration for College by Slidesgo.pptxDesign Inspiration for College by Slidesgo.pptx
Design Inspiration for College by Slidesgo.pptxTusharBahuguna2
 
VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130
VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130
VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130Suhani Kapoor
 
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun serviceCALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun serviceanilsa9823
 
SCRIP Lua HTTP PROGRACMACION PLC WECON CA
SCRIP Lua HTTP PROGRACMACION PLC  WECON CASCRIP Lua HTTP PROGRACMACION PLC  WECON CA
SCRIP Lua HTTP PROGRACMACION PLC WECON CANestorGamez6
 
Nepali Escort Girl Gomti Nagar \ 9548273370 Indian Call Girls Service Lucknow...
Nepali Escort Girl Gomti Nagar \ 9548273370 Indian Call Girls Service Lucknow...Nepali Escort Girl Gomti Nagar \ 9548273370 Indian Call Girls Service Lucknow...
Nepali Escort Girl Gomti Nagar \ 9548273370 Indian Call Girls Service Lucknow...nagunakhan
 
VIP Russian Call Girls in Saharanpur Deepika 8250192130 Independent Escort Se...
VIP Russian Call Girls in Saharanpur Deepika 8250192130 Independent Escort Se...VIP Russian Call Girls in Saharanpur Deepika 8250192130 Independent Escort Se...
VIP Russian Call Girls in Saharanpur Deepika 8250192130 Independent Escort Se...Suhani Kapoor
 
SD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxSD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxjanettecruzeiro1
 
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...Suhani Kapoor
 
NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...
NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...
NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...Amil baba
 
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779Delhi Call girls
 
Kurla Call Girls Pooja Nehwal📞 9892124323 ✅ Vashi Call Service Available Nea...
Kurla Call Girls Pooja Nehwal📞 9892124323 ✅  Vashi Call Service Available Nea...Kurla Call Girls Pooja Nehwal📞 9892124323 ✅  Vashi Call Service Available Nea...
Kurla Call Girls Pooja Nehwal📞 9892124323 ✅ Vashi Call Service Available Nea...Pooja Nehwal
 
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779Delhi Call girls
 
DragonBall PowerPoint Template for demo.pptx
DragonBall PowerPoint Template for demo.pptxDragonBall PowerPoint Template for demo.pptx
DragonBall PowerPoint Template for demo.pptxmirandajeremy200221
 

Recently uploaded (20)

CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service 🧵
CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service  🧵CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service  🧵
CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service 🧵
 
Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...
Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...
Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...
 
B. Smith. (Architectural Portfolio.).pdf
B. Smith. (Architectural Portfolio.).pdfB. Smith. (Architectural Portfolio.).pdf
B. Smith. (Architectural Portfolio.).pdf
 
Petrosains Drama Competition (PSDC).pptx
Petrosains Drama Competition (PSDC).pptxPetrosains Drama Competition (PSDC).pptx
Petrosains Drama Competition (PSDC).pptx
 
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
 
Design Inspiration for College by Slidesgo.pptx
Design Inspiration for College by Slidesgo.pptxDesign Inspiration for College by Slidesgo.pptx
Design Inspiration for College by Slidesgo.pptx
 
VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130
VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130
VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130
 
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun serviceCALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
 
SCRIP Lua HTTP PROGRACMACION PLC WECON CA
SCRIP Lua HTTP PROGRACMACION PLC  WECON CASCRIP Lua HTTP PROGRACMACION PLC  WECON CA
SCRIP Lua HTTP PROGRACMACION PLC WECON CA
 
Nepali Escort Girl Gomti Nagar \ 9548273370 Indian Call Girls Service Lucknow...
Nepali Escort Girl Gomti Nagar \ 9548273370 Indian Call Girls Service Lucknow...Nepali Escort Girl Gomti Nagar \ 9548273370 Indian Call Girls Service Lucknow...
Nepali Escort Girl Gomti Nagar \ 9548273370 Indian Call Girls Service Lucknow...
 
VIP Russian Call Girls in Saharanpur Deepika 8250192130 Independent Escort Se...
VIP Russian Call Girls in Saharanpur Deepika 8250192130 Independent Escort Se...VIP Russian Call Girls in Saharanpur Deepika 8250192130 Independent Escort Se...
VIP Russian Call Girls in Saharanpur Deepika 8250192130 Independent Escort Se...
 
SD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxSD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptx
 
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...
 
NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...
NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...
NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...
 
young call girls in Vivek Vihar🔝 9953056974 🔝 Delhi escort Service
young call girls in Vivek Vihar🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Vivek Vihar🔝 9953056974 🔝 Delhi escort Service
young call girls in Vivek Vihar🔝 9953056974 🔝 Delhi escort Service
 
escort service sasti (*~Call Girls in Prasad Nagar Metro❤️9953056974
escort service sasti (*~Call Girls in Prasad Nagar Metro❤️9953056974escort service sasti (*~Call Girls in Prasad Nagar Metro❤️9953056974
escort service sasti (*~Call Girls in Prasad Nagar Metro❤️9953056974
 
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
 
Kurla Call Girls Pooja Nehwal📞 9892124323 ✅ Vashi Call Service Available Nea...
Kurla Call Girls Pooja Nehwal📞 9892124323 ✅  Vashi Call Service Available Nea...Kurla Call Girls Pooja Nehwal📞 9892124323 ✅  Vashi Call Service Available Nea...
Kurla Call Girls Pooja Nehwal📞 9892124323 ✅ Vashi Call Service Available Nea...
 
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
 
DragonBall PowerPoint Template for demo.pptx
DragonBall PowerPoint Template for demo.pptxDragonBall PowerPoint Template for demo.pptx
DragonBall PowerPoint Template for demo.pptx
 

unit 1 (1).pptx

  • 2. What Is Automation Testing?  Automation Testing is a technique using an automation tool to write and execute tester's test scripts and cases..  The testing tool takes actual outcomes and compares it with the expected result to generate detailed test reports.
  • 3.  Since test automation demands a considerable investment of resources and money,  We should define: 1. when to do automation, 2. the scope of the automation 3. best tool of automation.  Good planning practices can save us from high costs.
  • 4. In the automation process, steps involved are 1. Selecting the Test tool 2. Define scope of automation 3. Planning, design, and development 4. Test execution 5. Maintenance
  • 5.  Testing is an essential part of product development, especially to guarantee quality.  Many smaller and mid-sized companies don’t give testing much attention even though it’s essential for delivering a strong product.  Some companies prefer to do manual testing, although that’s not the only best approach.
  • 6.  The next logical step is automating your testing process.  Manual testing should be minimized at all costs.  Test automation increases overall software development efficiency and allows for more robust tools to be built.
  • 7. Benefits  Faster Feedback Cycle  Saves Time  Reduced Business Expenses  Higher Test Coverage  Reusability of Test Suite  Faster Time to Market  Improved Accuracy  Less Stress on QA Team  Quick Determination the Stability of Your Build  Eliminate Human Error
  • 8. Benefits  Ability To Perform Testing On Multiple Platforms In Parallel  Accelerate Cross Browser Testing  Distributed Test Execution  Data Driven Testing
  • 9. Most popular tools for automation testing  The most popular test tool for automation testing are • QTP (HP UFT) • Rational Robot • Selenium
  • 10. Why JUnit?  JUnit is one of the most popular unit testing frameworks for Java development.  Unit testing is an important part in Test Driven Development (TDD) as it helps finding problems in the code as early as possible, especially when you make changes to the existing code you can run unit tests again to make sure that the changes do not break the application (regression).  JUnit is supported by almost any Java IDEs and build tools, thus it is the default choice of programmers to test their code.  Eclipse has very good support for JUnit - the IDE is shipped with JUnit as its default testing library.  Thus writing and running unit tests with JUnit in Eclipse is quick, easy and productive.
  • 11. Structure of a Test Class This Calculator class has two methods add() and subtract(). Now we want to write a test class to ensure that these methods are working correctly. With JUnit, the test class can have the following structure:
  • 12.
  • 13. Annotations used in this test class  @BeforeClass:  Code in this method is executed only once, before all test methods in the test class. Typically you put code that prepares test environment here, e.g. initializing resources such as opening a database connection. The method marked with this annotation must be static.   @Before:  Code in this method is executed before every test method in the test class. So typically you put repeated code that must be executed before each test method here. 
  • 14.  @Test:  This annotation specifies the annotated method is a test method. You can notice the naming convention for test method is the method name starts with test, followed by the name of the method in the class being tested. This Calculator class has two methods add() and subtract(), hence the test methods are testAdd() and testSubtract().  @After:  Code in this method is executed after every test method in the test class. So typically you put repeated code that must be executed after each test method here. NOTE: The above-mentioned annotations are in JUnit 4.
  • 15.  @AfterClass:  Code in this method is executed only once, after all test methods in the test class. Due to this behavior, you can put code to clean up test environment here, e.g. closing the database connection. Note that this kind of method must be static.  Except the test methods, the other kinds of method are optional. So you implement them in the test class only if it is necessary.
  • 16. JUnit 5 changes in annotations  From JUnit 5,  @BeforeClass changes to @BeforeAll;  @Before changes to @BeforeEach;  @After changes to @AfterEach;  @AfterClass changes to @AfterAll.  This change in JUnit 5 reflects better meaning of the annotations.
  • 17. With JUnit 5, the structure of a test class looks like this:
  • 18. To be noted:  In JUnit 4, you need to import the annotations from the org.junit package, but in JUnit 5 you need to import them from the org.junit.jupiter.api package.
  • 19. Using Assert Statement in Test Methods  In a test method, we use an assert statement to verify whether the code under test produces the result as expected or not.  If the result is as expected, the tested code passes the test case. Otherwise the tested code fails the test case.
  • 20.  We use the assertEquals() method from the class org.junit.Assert via static import:  you can use the fail() method to fail the test immediately, for example: Use the fail() method to indicate that a method is not yet implemented so when the test class is executed, it will fail.
  • 21.  The assertEquals() method compares two values: the expected result and the actual value returned from the tested code.  If the two values are equal, the method returns normally indicating the tested code passes the test case.  If the two values are not equal, the method throws java.lang.AssertionError indicating the tested code fails the test case.  And programmers must fix the code until it passes the test case.
  • 22. JUnit provides many assert()methods, and here are some commonly used ones: • assertEquals(expected, actual): asserts that two values are equal. The values can be of any primitive types or Objects. • assertNotEquals(expected, actual): asserts that two arguments are not equal. The values can be of any primitive types or Object. • assertTrue(boolean condition): asserts that a condition is true. • assertFalse(boolean condition): asserts that a condition is false. • assertNull(Object): asserts that an object is null. • assertNotNull(Object): asserts that an object is not null. • assertSame(Object expected, Object actual): asserts that two objects refer to the same object. • assertNotSame(Object unexpected, Object actual): asserts that two objects do not refer to the same object.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29. You can see the green indicator bar in the upper-right corner indicating that the test case has been executed successfully - the code under test passes the test case.
  • 30. If the test fails, the status indicator becomes red and the failure trace is printed:
  • 31. This is the result of running the testSubtract() method,
  • 32. The class Calculator passes two test methods in the CalculatorTest class.