SE401 · Annotated SlidesUnit Testing and JUnit — Unit Testing and JUnit
Unit Testing and JUnit · Annotated slides

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.

Slide text Study notes Highlight: key words Tips, tricks & traps Seen again elsewhere How it can come up
Slide 1: Slide 1
Exam weight Low
    Slide 2: • Unit Testing and JUnit
    Exam weight Low

      Unit Testing and JUnit

      3
      Exam weight Low
        Slide 4: • Testing of an individual software unit
        Exam weight Low
        1. Unit testing: test one unit (method/class) in isolation, written by developers, run automatically and often.
        Slide 5: • Unit testing: Looking for errors in a subsystem in isolation.
        Exam weight Low
          Slide 6: • Allows you to write code faster while increasing quality • Elegantly simple
          Exam weight Low
            Slide 7: • JUnit helps the programmer:
            Exam weight Low
              Slide 8: • JUnit is a framework for writing unit tests
              Exam weight Low
              1. JUnit: a framework for writing repeatable automated Java tests; a test method is annotated @Test and returns void.
              Slide 9: import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test;
              Exam weight Low
                Slide 10: • A test method doesn ’ t return a result • If the tests run correctly, a test method does nothing
                Exam weight Low
                  Slide 11: • In practice, you want to run a group of related tests (e.g. all the tests for a class)
                  Exam weight Low
                    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
                    Exam weight Low
                      Slide 13: • Separate production and test code • But typically in the same packages
                      Exam weight Low
                        Slide 14: • Tests need failure atomically (ability to know exactly what failed).
                        Exam weight Low
                          Slide 15: • JUnit is designed to call methods and compare the results they return against expected results
                          Exam weight Low

                            An Introduction to JUnit

                            Part 1: The Basics

                            16
                            Exam weight Low
                              Slide 17: • A unit testing tool for Java programs
                              Exam weight Low
                                Slide 18: • Requires Java 8 (or higher) at runtime
                                Exam weight Low
                                  Slide 19: • JUnit has been integrated into most IDE’s
                                  Exam weight Low
                                    Slide 20: A verdict is the result of executing a single test case.
                                    Exam weight Low
                                    1. Verdicts: pass, fail (assertion failed), error (unexpected exception).
                                    Slide 21: • A JUnit test is represented as a class (test class). • Each test case is a method in a test class.
                                    Exam weight Low
                                      Slide 22: • Assertions are Boolean expressions
                                      Exam weight Low
                                      1. Assertions: Boolean checks that make the test fail if they are false.
                                      Slide 23: /** Test of setName method, of class Value */
                                      Exam weight Low
                                        Slide 24: /** Test of setName method, of class Value */
                                        Exam weight Low
                                          Slide 25: /** Test of setName method, of class Value */
                                          Exam weight Low
                                            Slide 26: /** Test of setName method, of class Value */
                                            Exam weight Low
                                              Slide 27: /** Test of setName method, of class Value */
                                              Exam weight Low
                                                Slide 28: • Each test method represents a single test case
                                                Exam weight Low
                                                  Slide 29: • Download and install JDK ▪ Download and install Eclipse/NetBeans IDE for Java Developers
                                                  Exam weight Low
                                                    Slide 30: • Download the Sample Code
                                                    Exam weight Low
                                                      Slide 31: Slide 31
                                                      Exam weight Low
                                                        Slide 32: Slide 32
                                                        Exam weight Low
                                                          Slide 33: Slide 33
                                                          Exam weight Low
                                                            Slide 34: • Start Eclipse IDE
                                                            Exam weight Low
                                                              Slide 35: • Click “Next” • Java Settings
                                                              Exam weight Low
                                                                Slide 36: • Add Library
                                                                Exam weight Low
                                                                  Slide 37: Slide 37
                                                                  Exam weight Low
                                                                    Slide 38: Slide 38
                                                                    Exam weight Low

                                                                      Assertion Methods

                                                                      39
                                                                      Exam weight Low
                                                                        Slide 40: During execution of a test case: • If an assertion is true ,
                                                                        Exam weight Low
                                                                          Slide 41: • Static methods defined in org.junit.Assert • Assert a Boolean condition is true or false
                                                                          Exam weight High
                                                                          1. assertEquals(expected, actual), assertTrue, assertFalse, assertNull, assertNotNull, assertSame (same reference), assertArrayEquals.
                                                                          Exam trapassertEquals uses equals(); assertSame checks the two references are the SAME object.
                                                                          🎯 How this can come up · 1 question
                                                                          MCQ

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

                                                                          Slide 42: • Assert an object references is null or non-null
                                                                          Exam weight Low
                                                                            Slide 43: • Assert two object references are identical
                                                                            Exam weight Low
                                                                              Slide 44: • Examples
                                                                              Exam weight Low
                                                                                Slide 45: • Assert two objects are equal:
                                                                                Exam weight Low
                                                                                  Slide 46: • Examples
                                                                                  Exam weight Low
                                                                                    Slide 47: • Assert two arrays are equal:
                                                                                    Exam weight Low
                                                                                      Slide 48: • Examples
                                                                                      Exam weight Low
                                                                                        Slide 49: • For comparing floating point values ( double or float ) requires an additional parameter delta .
                                                                                        Exam weight Low
                                                                                        1. Floating point: assertEquals(expected, actual, delta), e.g. assertEquals(0.3, 0.1 + 0.2, 0.0001).
                                                                                        Slide 50: • A.k.a., robustness testing • The expected outcome of a test is an exception.
                                                                                        Exam weight Low
                                                                                          Slide 51: • Specify an expected exception in a test case
                                                                                          Exam weight High
                                                                                          1. Expected exceptions: assertThrows(IllegalArgumentException.class, () -> obj.method(-1));
                                                                                          🎯 How this can come up · 1 question
                                                                                          MCQ

                                                                                          JUnit 5 checks that code throws an exception with:

                                                                                          Slide 52: • Assertion methods
                                                                                          Exam weight Low
                                                                                            Slide 53: • Catch exceptions, and use if not thrown
                                                                                            Exam weight Low
                                                                                              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
                                                                                              Exam weight Low

                                                                                                JUnit Best Practices

                                                                                                55
                                                                                                Exam weight Low
                                                                                                  Slide 56: • Each test case should be independent. • Test cases should be independent of execution order.
                                                                                                  Exam weight Low
                                                                                                  1. Each test must be independent of the others (no shared mutable state, any order).
                                                                                                  Slide 57: • The context in which a test case is executed. • Typically include:
                                                                                                  Exam weight Low
                                                                                                    Slide 58: • Tasks that must be done prior to each test case • Examples:
                                                                                                    Exam weight High
                                                                                                    1. @BeforeEach / @AfterEach run before/after EVERY test; @BeforeAll / @AfterAll run once per class and must be static.
                                                                                                    🎯 How this can come up · 1 question
                                                                                                    MCQ

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

                                                                                                    Slide 59: • Tasks to clean up after execution of each test case. • Ensures
                                                                                                    Exam weight Low
                                                                                                      Slide 60: annotation: set-up
                                                                                                      Exam weight Low
                                                                                                        Slide 61: public class OutputTest {
                                                                                                        Exam weight Low
                                                                                                          Slide 62: 1. createOutputFile 2. test 1 WithFile
                                                                                                          Exam weight Low
                                                                                                            Slide 63: annotation on a static method • @BeforeAll
                                                                                                            Exam weight Low
                                                                                                              Slide 64: annotation on a static method • After All
                                                                                                              Exam weight Low
                                                                                                                Slide 65: • Useful for simple performance test
                                                                                                                Exam weight Low
                                                                                                                  Slide 66: • JUnit 5 Documentation
                                                                                                                  Exam weight Low
                                                                                                                    Slide 67: Slide 67
                                                                                                                    Exam weight Low
                                                                                                                      Slide 68: • Repeat a test case multiple times with different data • Define a parameterized test
                                                                                                                      Exam weight Low
                                                                                                                      1. @RepeatedTest(n) runs a test n times.
                                                                                                                      Slide 69: Slide 69
                                                                                                                      Exam weight Low