Session 4 -Junit- Testing Exceptions, Junit hooks.pptx

trainingdecorpo 8 views 21 slides Aug 08, 2024
Slide 1
Slide 1 of 21
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21

About This Presentation

na na


Slide Content

JUNIT BASICS

JUNIT BASICS TESTING EXCEPTIONS, JUNIT HOOKS

TESTING EXCEPTIONS Testing exceptions in JUnit is essential to ensure that certain parts of code behave as expected and throw the correct exceptions when necessary. JUnit provides different ways to test for exceptions. Some common techniques using JUnit 4 and JUnit 5. 3

TESTING EXCEPTIONS public class MyMath { public int divide( int a, int b) { if (b == 0) { throw new ArithmeticException ("Division by zero is not allowed."); } return a / b; } } java Assuming you have a method that might throw an exception: 4

TESTING EXCEPTIONS import org.junit.Test ; import static org.junit.Assert.assertEquals ; public class MyMathTest { @Test(expected = ArithmeticException.class ) public void testDivideByZero () { MyMath myMath = new MyMath (); myMath.divide (10, 0); } java 1. Using JUnit 4 @Test with expected attribute: 5

TESTING EXCEPTIONS @Test public void testValidDivision () { MyMath myMath = new MyMath (); int result = myMath.divide (10, 2); assertEquals (5, result); } } Java(Cont…) 1. Using JUnit 4 @Test with expected attribute: 6

TESTING EXCEPTIONS In the testDivideByZero method, the divide method will throw an ArithmeticException when dividing by zero. Using the expected attribute of the @Test annotation, this exception should be thrown during the test is indicated. In the testValidDivision method, the divide method will perform a valid division when the denominator is not zero. assertEquals is used to verify that the result is as expected. 7

TESTING EXCEPTIONS import org.junit.jupiter.api.Test ; import static org.junit.jupiter.api.Assertions.assertEquals ; import static org.junit.jupiter.api.Assertions.assertThrows ; public class MyMathTest { @Test public void testDivideByZero () { MyMath myMath = new MyMath (); assertThrows ( ArithmeticException.class , () -> myMath.divide (10, 0)); } java 2. Using JUnit 5 assertThrows method: 8

TESTING EXCEPTIONS @Test public void testValidDivision () { MyMath myMath = new MyMath (); int result = myMath.divide (10, 2); assertEquals (5, result); } } Java(Cont…) 2. Using JUnit 5 assertThrows method: 9

TESTING EXCEPTIONS With JUnit 5, use the assertThrows method, which takes the expected exception class and a lambda expression representing the code that should throw the exception. If the specified exception is thrown during the lambda execution, the test passes. Both approaches are valid for testing exceptions in JUnit . But latest version is best 10

JUNIT HOOKS JUnit doesn't have built-in hooks in the same way that some other testing frameworks do. However, it provides annotations and methods that can be used to perform setup and teardown actions before and after test methods or test classes are executed. These actions can serve a similar purpose as hooks in other testing frameworks. 11

JUNIT HOOKS I n JUnit 4, the commonly used setup and teardown methods are @Before and @After, respectively. In JUnit 5, the equivalent annotations are @ BeforeEach and @ AfterEach . Additionally, there are class-level setup and teardown methods in JUnit 4 (@ BeforeClass and @ AfterClass ) and JUnit 5 (@ BeforeAll and @ AfterAll ). 12

JUNIT HOOKS @ BeforeClass : Used with static methods to perform setup actions before any test method is executed in the test class. The method is executed only once per test class. @Before: Used to perform setup actions before each test method in the test class. Here's an overview of these annotations : JUnit 4 Hooks: 13

JUNIT HOOKS @After: Used to perform cleanup actions after each test method in the test class. @ AfterClass : Used with static methods to perform cleanup actions after all test methods have been executed in the test class. The method is executed only once per test class. Here's an overview of these annotations : JUnit 4 Hooks: 14

JUNIT HOOKS import org.junit .*; public class MyTestClass { @ BeforeClass public static void setUpClass () { // Code to set up resources or configurations before all test methods } @Before public void setUp () { // Code to set up resources or configurations before each test method } @Test public void testMethod1() { // Test method 1 } Java Example usage in JUnit 4: 15

JUNIT HOOKS @Test public void testMethod2() { // Test method 2 } @After public void tearDown () { // Code to release resources or clean up after each test method } @ AfterClass public static void tearDownClass () { // Code to perform cleanup after all test methods have been executed } } Java(Cont…) Example usage in JUnit 4: 16

JUNIT HOOKS @ BeforeAll : Used with static methods to perform setup actions before any test method is executed in the test class. The method is executed only once per test class. @ BeforeEach : Used to perform setup actions before each test method in the test class. JUnit 5 Hooks : 17

JUNIT HOOKS @ AfterEach : Used to perform cleanup actions after each test method in the test class. @ AfterAll : Used with static methods to perform cleanup actions after all test methods have been executed in the test class. The method is executed only once per test class. JUnit 5 Hooks : 18

JUNIT HOOKS import org.junit.jupiter.api .*; public class MyTestClass { @ BeforeAll public static void setUpClass () { // Code to set up resources or configurations before all test methods } @ BeforeEach public void setUp () { // Code to set up resources or configurations before each test method } @Test public void testMethod1() { // Test method 1 } Java Example usage in JUnit 5: 19

JUNIT HOOKS @Test public void testMethod2() { // Test method 2 } @ AfterEach public void tearDown () { // Code to release resources or clean up after each test method } @ AfterAll public static void tearDownClass () { // Code to perform cleanup after all test methods have been executed } } Java(Cont…) Example usage in JUnit 5: 20

JUNIT HOOKS These setup and teardown methods provide a way to perform common actions across test methods, such as setting up a test database, initializing test data, or cleaning up resources after tests are done. 21
Tags