SE401 · Unit Testing and JUnit · Annotated SlidesUnit Testing and JUnit
Original
Overview

Unit Testing and JUnit

What unit testing is and why, JUnit 5 test classes and methods, verdicts, assertion methods (assertEquals, assertTrue, assertNull, assertSame, assertArrayEquals, assertThrows), floating-point deltas, expected exceptions, and best practices with @BeforeEach, @AfterEach, @BeforeAll, @AfterAll and repeated tests.

How to read this page

Every original slide is shown unchanged. Yellow marks on a slide point to a numbered note: hover or tap a mark to jump to its note, and hover a note to see its marks. Questions tagged Past exam appeared on a real paper; Prediction questions are likely angles, not confirmed. Pick an answer to each question: a wrong pick tells you why it is wrong, so you can try again.

All 69 slides
  1. 1 Slide 1
  2. 2 • Unit Testing and JUnit
  3. 3 Slide 3
  4. 4 • Testing of an individual software unit
  5. 5 • Unit testing: Looking for errors in a subsystem in isolation.
  6. 6 • Allows you to write code faster while increasing quality • Elegantly simple
  7. 7 • JUnit helps the programmer:
  8. 8 • JUnit is a framework for writing unit tests
  9. 9 import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test;
  10. 10 • A test method doesn ’ t return a result • If the tests run correctly, a test method does nothing
  11. 11 • In practice, you want to run a group of related tests (e.g. all the tests for a class)
  12. 12 • Create test cases in the same package as the code under test • For each Java package in your application, define a TestSuite class that
  13. 13 • Separate production and test code • But typically in the same packages
  14. 14 • Tests need failure atomically (ability to know exactly what failed).
  15. 15 • JUnit is designed to call methods and compare the results they return against expected results
  16. 16 Slide 16
  17. 17 • A unit testing tool for Java programs
  18. 18 • Requires Java 8 (or higher) at runtime
  19. 19 • JUnit has been integrated into most IDE’s
  20. 20 A verdict is the result of executing a single test case.
  21. 21 • A JUnit test is represented as a class (test class). • Each test case is a method in a test class.
  22. 22 • Assertions are Boolean expressions
  23. 23 /** Test of setName method, of class Value */
  24. 24 /** Test of setName method, of class Value */
  25. 25 /** Test of setName method, of class Value */
  26. 26 /** Test of setName method, of class Value */
  27. 27 /** Test of setName method, of class Value */
  28. 28 • Each test method represents a single test case
  29. 29 • Download and install JDK ▪ Download and install Eclipse/NetBeans IDE for Java Developers
  30. 30 • Download the Sample Code
  31. 31 The Example Program: The Class Under Test
  32. 32 The JUnit Test
  33. 33 The JUnit Test (cont’d)
  34. 34 • Start Eclipse IDE
  35. 35 • Click “Next” • Java Settings
  36. 36 • Add Library
  37. 37 Run JUnit in Eclipse: An Example
  38. 38 Run JUnit in Eclipse: An Example
  39. 39 Slide 39
  40. 40 During execution of a test case: • If an assertion is true ,
  41. 41 • Static methods defined in org.junit.Assert • Assert a Boolean condition is true or false
  42. 42 • Assert an object references is null or non-null
  43. 43 • Assert two object references are identical
  44. 44 • Examples
  45. 45 • Assert two objects are equal:
  46. 46 • Examples
  47. 47 • Assert two arrays are equal:
  48. 48 • Examples
  49. 49 • For comparing floating point values ( double or float ) requires an additional parameter delta .
  50. 50 • A.k.a., robustness testing • The expected outcome of a test is an exception.
  51. 51 • Specify an expected exception in a test case
  52. 52 • Assertion methods
  53. 53 • Catch exceptions, and use if not thrown
  54. 54 • Unit testing refers to the practice of testing certain functions and areas – or units – of our code. This gives us the ability to verify that our
  55. 55 Slide 55
  56. 56 • Each test case should be independent. • Test cases should be independent of execution order.
  57. 57 • The context in which a test case is executed. • Typically include:
  58. 58 • Tasks that must be done prior to each test case • Examples:
  59. 59 • Tasks to clean up after execution of each test case. • Ensures
  60. 60 annotation: set-up
  61. 61 public class OutputTest {
  62. 62 1. createOutputFile 2. test 1 WithFile
  63. 63 annotation on a static method • @BeforeAll
  64. 64 annotation on a static method • After All
  65. 65 • Useful for simple performance test
  66. 66 • JUnit 5 Documentation
  67. 67 JUnit 5 Unit Testing Framework
  68. 68 • Repeat a test case multiple times with different data • Define a parameterized test
  69. 69 Parameterized Test Example
Slide 1 / 69

Slide 1

Slide 1: Slide 1
Slide 2 / 69

• Unit Testing and JUnit

Slide 2: • Unit Testing and JUnit
Slide 3 / 69

Slide 3

Slide 3: Slide 3
Slide 4 / 69

• Testing of an individual software unit

Slide 4: • Testing of an individual software unit

Notes

  1. Unit testing: test one unit (method/class) in isolation, written by developers, run automatically and often.

Slide 5 / 69

• Unit testing: Looking for errors in a subsystem in isolation.

Slide 5: • Unit testing: Looking for errors in a subsystem in isolation.
Slide 6 / 69

• Allows you to write code faster while increasing quality • Elegantly simple

Slide 6: • Allows you to write code faster while increasing quality • Elegantly simple
Slide 7 / 69

• JUnit helps the programmer:

Slide 7: • JUnit helps the programmer:
Slide 8 / 69

• JUnit is a framework for writing unit tests

Slide 8: • JUnit is a framework for writing unit tests

Notes

  1. JUnit: a framework for writing repeatable automated Java tests; a test method is annotated @Test and returns void.

Slide 9 / 69

import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test;

Slide 9: import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test;
Slide 10 / 69

• A test method doesn ’ t return a result • If the tests run correctly, a test method does nothing

Slide 10: • A test method doesn ’ t return a result • If the tests run correctly, a test method does nothing
Slide 11 / 69

• In practice, you want to run a group of related tests (e.g. all the tests for a class)

Slide 11: • In practice, you want to run a group of related tests (e.g. all the tests for a class)
Slide 12 / 69

• Create test cases in the same package as the code under test • For each Java package in your application, define a TestSuite class that

Slide 12: • Create test cases in the same package as the code under test • For each Java package in your application, define a TestSuite class that
Slide 13 / 69

• Separate production and test code • But typically in the same packages

Slide 13: • Separate production and test code • But typically in the same packages
Slide 14 / 69

• Tests need failure atomically (ability to know exactly what failed).

Slide 14: • Tests need failure atomically (ability to know exactly what failed).
Slide 15 / 69

• JUnit is designed to call methods and compare the results they return against expected results

Slide 15: • JUnit is designed to call methods and compare the results they return against expected results
Slide 16 / 69

Slide 16

Slide 16: Slide 16
Slide 17 / 69

• A unit testing tool for Java programs

Slide 17: • A unit testing tool for Java programs
Slide 18 / 69

• Requires Java 8 (or higher) at runtime

Slide 18: • Requires Java 8 (or higher) at runtime
Slide 19 / 69

• JUnit has been integrated into most IDE’s

Slide 19: • JUnit has been integrated into most IDE’s
Slide 20 / 69

A verdict is the result of executing a single test case.

Slide 20: A verdict is the result of executing a single test case.
Slide 21 / 69

• A JUnit test is represented as a class (test class). • Each test case is a method in a test class.

Slide 21: • A JUnit test is represented as a class (test class). • Each test case is a method in a test class.
Slide 22 / 69

• Assertions are Boolean expressions

Slide 22: • Assertions are Boolean expressions
Slide 23 / 69

/** Test of setName method, of class Value */

Slide 23: /** Test of setName method, of class Value */
Slide 24 / 69

/** Test of setName method, of class Value */

Slide 24: /** Test of setName method, of class Value */
Slide 25 / 69

/** Test of setName method, of class Value */

Slide 25: /** Test of setName method, of class Value */
Slide 26 / 69

/** Test of setName method, of class Value */

Slide 26: /** Test of setName method, of class Value */
Slide 27 / 69

/** Test of setName method, of class Value */

Slide 27: /** Test of setName method, of class Value */
Slide 28 / 69

• Each test method represents a single test case

Slide 28: • Each test method represents a single test case
Slide 29 / 69

• Download and install JDK ▪ Download and install Eclipse/NetBeans IDE for Java Developers

Slide 29: • Download and install JDK ▪ Download and install Eclipse/NetBeans IDE for Java Developers
Slide 30 / 69

• Download the Sample Code

Slide 30: • Download the Sample Code
Slide 31 / 69

The Example Program: The Class Under Test

Slide 31: The Example Program: The Class Under Test
Slide 32 / 69

The JUnit Test

Slide 32: The JUnit Test
Slide 33 / 69

The JUnit Test (cont’d)

Slide 33: The JUnit Test (cont’d)
Slide 34 / 69

• Start Eclipse IDE

Slide 34: • Start Eclipse IDE
Slide 35 / 69

• Click “Next” • Java Settings

Slide 35: • Click “Next” • Java Settings
Slide 36 / 69

• Add Library

Slide 36: • Add Library
Slide 37 / 69

Run JUnit in Eclipse: An Example

Slide 37: Run JUnit in Eclipse: An Example
Slide 38 / 69

Run JUnit in Eclipse: An Example

Slide 38: Run JUnit in Eclipse: An Example
Slide 39 / 69

Slide 39

Slide 39: Slide 39
Slide 40 / 69

During execution of a test case: • If an assertion is true ,

Slide 40: During execution of a test case: • If an assertion is true ,
Slide 41 / 69

• Static methods defined in org.junit.Assert • Assert a Boolean condition is true or false

Slide 41: • Static methods defined in org.junit.Assert • Assert a Boolean condition is true or false

Notes

  1. assertEquals(expected, actual), assertTrue, assertFalse, assertNull, assertNotNull, assertSame (same reference), assertArrayEquals.

  2. Trap assertEquals uses equals(); assertSame checks the two references are the SAME object.

Exam angle

PredictionQ1

Which assertion checks that two references point to the same object?

Slide 42 / 69

• Assert an object references is null or non-null

Slide 42: • Assert an object references is null or non-null
Slide 43 / 69

• Assert two object references are identical

Slide 43: • Assert two object references are identical
Slide 44 / 69

• Examples

Slide 44: • Examples
Slide 45 / 69

• Assert two objects are equal:

Slide 45: • Assert two objects are equal:
Slide 46 / 69

• Examples

Slide 46: • Examples
Slide 47 / 69

• Assert two arrays are equal:

Slide 47: • Assert two arrays are equal:
Slide 48 / 69

• Examples

Slide 48: • Examples
Slide 49 / 69

• For comparing floating point values ( double or float ) requires an additional parameter delta .

Slide 49: • For comparing floating point values ( double or float ) requires an additional parameter delta .
Slide 50 / 69

• A.k.a., robustness testing • The expected outcome of a test is an exception.

Slide 50: • A.k.a., robustness testing • The expected outcome of a test is an exception.
Slide 51 / 69

• Specify an expected exception in a test case

Slide 51: • Specify an expected exception in a test case

Notes

  1. Expected exceptions: assertThrows(IllegalArgumentException.class, () -> obj.method(-1));

Exam angle

PredictionQ1

JUnit 5 checks that code throws an exception with:

Slide 52 / 69

• Assertion methods

Slide 52: • Assertion methods
Slide 53 / 69

• Catch exceptions, and use if not thrown

Slide 53: • Catch exceptions, and use if not thrown
Slide 54 / 69

• Unit testing refers to the practice of testing certain functions and areas – or units – of our code. This gives us the ability to verify that our

Slide 54: • Unit testing refers to the practice of testing certain functions and areas – or units – of our code. This gives us the ability to verify that our
Slide 55 / 69

Slide 55

Slide 55: Slide 55
Slide 56 / 69

• Each test case should be independent. • Test cases should be independent of execution order.

Slide 56: • Each test case should be independent. • Test cases should be independent of execution order.

Notes

  1. Each test must be independent of the others (no shared mutable state, any order).

Slide 57 / 69

• The context in which a test case is executed. • Typically include:

Slide 57: • The context in which a test case is executed. • Typically include:
Slide 58 / 69

• Tasks that must be done prior to each test case • Examples:

Slide 58: • Tasks that must be done prior to each test case • Examples:

Notes

  1. @BeforeEach / @AfterEach run before/after EVERY test; @BeforeAll / @AfterAll run once per class and must be static.

Exam angle

PredictionQ1

Setup code that must run once before all tests in the class uses:

Slide 59 / 69

• Tasks to clean up after execution of each test case. • Ensures

Slide 59: • Tasks to clean up after execution of each test case. • Ensures
Slide 60 / 69

annotation: set-up

Slide 60: annotation: set-up
Slide 61 / 69

public class OutputTest {

Slide 61: public class OutputTest {
Slide 62 / 69

1. createOutputFile 2. test 1 WithFile

Slide 62: 1. createOutputFile 2. test 1 WithFile
Slide 63 / 69

annotation on a static method • @BeforeAll

Slide 63: annotation on a static method • @BeforeAll
Slide 64 / 69

annotation on a static method • After All

Slide 64: annotation on a static method • After All
Slide 65 / 69

• Useful for simple performance test

Slide 65: • Useful for simple performance test
Slide 66 / 69

• JUnit 5 Documentation

Slide 66: • JUnit 5 Documentation
Slide 67 / 69

JUnit 5 Unit Testing Framework

Slide 67: JUnit 5 Unit Testing Framework
Slide 68 / 69

• Repeat a test case multiple times with different data • Define a parameterized test

Slide 68: • Repeat a test case multiple times with different data • Define a parameterized test
Slide 69 / 69

Parameterized Test Example

Slide 69: Parameterized Test Example