JUnit
JUnit
JUnit
Chithra Sadhu
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
Write Assertions:
Junit Test
Common assertion methods include
`assertEquals`, `assertTrue`,
`assertFalse`, and others.
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.