JUnit

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 14

Junit

A tool for test driven development

Chithra Sadhu
JUnit

• Junit is an open source framework that


has been designed for the purpose of
writing and running tests in the java
programming language.
• Why we need tests instead of manual
testing?
• We conduct testing instead of
writing test cases, but when future
modifications are required and we
need to retest, manual testing
becomes more challenging.
Why Junit

• Test Runs
• Preparation
• Provide test inputs
• Run the tests
• Provide expect output
• Verify results
• Do something to alert the developer if tests failed
• In Addition Junit provides a framework to keep tests simple and small.
• Failed tests are shown in red flag and passed tests are shown in green.
• These all process will make sure that the modifications in the code will not break
your system without your knowledge.
• We can run multiple tests concurrently.
Junit Architecture

JUnit Platform : To execute or lunch JUnit Test cases builds


tools, IDEs and plugins required to include and extend platform
APIs.

JUnit Jupiter : It is the combination of the new programming


model and extension model for writing tests and extensions in
JUnit 5. It has newly added annotations for running Jupiter
based tests on the platform.

JUnit Vintage : It has the main purpose to support running


JUnit 3 and JUnit 4 written tests on the JUnit 5 platform. It
provides assurance for backward compatibility.
Adding Dependencies
Maven Project : Gradle Project :
1. Create a Test Class: Add Test Methods:
• First, create a Java class that will • Inside your test class, add methods
contain your test methods. The class that will serve as your test cases. Each
should have a name that ends with test method should be annotated with
"Test" to follow JUnit naming `@Test` to indicate that it's a test case.
conventions. These methods will contain the code to
test specific parts of your application.

Write Assertions:

Creating a • Within each test method, use JUnit's


assertion methods to verify that the
expected behavior of your code is met. Run the Tests

Junit Test
Common assertion methods include
`assertEquals`, `assertTrue`,
`assertFalse`, and others.

View Test Results Refactor and Iterate


Junit Annotations
JUnit uses annotations to define and control the execution of test methods. Here's an explanation of some common
JUnit annotations
• @Test
- This annotation is used to mark a method as a test method.
- JUnit will run methods annotated with `@Test` when you execute your test suite.
• @Before and @After
- `@Before` and `@After` annotations are used to mark methods that should run before and after each test method,
respectively.
- They are useful for setting up and cleaning up common resources needed for multiple test methods.
• @BeforeClass and @AfterClass:
- `@BeforeClass` and `@AfterClass` annotations are used to mark methods that should run once before and after all test
methods in the test class.
- They are typically used for expensive setup and teardown operations.
• @Ignore
• `@Ignore` is used to temporarily disable a test method. It can be helpful when you have known issues in your tests that
you want to skip.
Junit Assertions

JUnit provides several assertion methods that you can use to check the expected
results of your test cases. Here are some commonly used assertions in JUnit along with
example code for each one:
1. assertEquals(expected, actual):
- Compares if the expected and actual values are equal.
2. assertTrue(condition):
- Checks if the given condition is `true`.
3. assertFalse(condition):
- Checks if the given condition is `false`.
4. assertNotNull(object):
- Verifies that the given object is not `null`.
Junit Assertions

5. assertNull(object):
- Verifies that the given object is `null`.
6. assertSame(expected, actual):
- Checks if the expected and actual references point to the same object.
7. assertNotSame(expected, actual):
- Verifies that the expected and actual references do not point to the same object.
8. assertArrayEquals(expected, actual):
- Compares two arrays for equality.
• Name of the test class must end with Test.
• Name of a test method should be in form
shouldReturnWhen.
• Return type of a test method should be void.
Coding • Test method must not throw any exception.

Convention • Test method must not have any parameter.


• Each test method checks a condition and reports back to
for Junit Class the test runner whether the test failed or passed.
• For each java package in your application, define a
TestSuite class that contains all the tests for validating the
code in the package.
• Follow AAA method to arrange and organize the test code
to make tests to clear and understandable.
Example : Counter class
• For the sake of example, we will create and test a trivial “counter”
class
- The constructor will create a counter and set it to zero
- The increment method will add one to the counter and return
the new value
- The decrement method will subtract one from the counter and
return the new value
• We write the test methods before we write the code
The Counter class
Junit tests for
Counter
Thank you
Questions & Feedback

You might also like