CIS*2430 October 5, 2023 Announcements and Reminders ´quiz today ´Asynchronous virtual lectures coming up ´October 12 and 17 ´No Labs next week ´No Quiz next week topics for the next two lectures are better ‘done’ than ‘listened to’ Agenda ´Testing and Junit ´Practice with Junit ´Quiz What is Unit Testing • Unit testing is a procedure used to validate that individual units of source code are working properly • A unit is the smallest testable part of an application (class method, function, procedure) Unit Testing • A test case: – Runs completely by itself – Pass or fail – Runs in isolation • Each test case is independent from the others Unit Testing 6 code reports test cases programmer Category of Test Test Case Expected Result all vowels are replaced correctly `fagehiwoquryt` fbgfhjwpqrrzt a vowel at the beginning of a string is replaced properly `abrtswghbc` bbrtswgjbc a vowel at the end of a string is replaced properly `plkjhrwe` plkjhrwf digits are not replaced in mixed character/digit string `rwsd54t` and `rea34p` rwsd54t and rfb34p when `y` is used as a vowel it is replaced properly `sdfrytr` sdfrztr the vowel `a` is replaced properly `prtasr` prtbsr the vowel `e` is replaced properly `trert` trfrt the vowel `i` is replaced properly `pibnm` pjbnm the vowel `o` is replaced properly `wsopxs` wsppxs the vowel `u` is replaced properly `funsw` fvnsw an empty string produces no output Unit Testing: test cases 7 code interface unit data structures test cases Category of Test Test Case Expected Result all vowels are replaced correctly `fagehiwoquryt` fbgfhjwpqrrzt a vowel at the beginning of a string is replaced properly `abrtswghbc` bbrtswgjbc a vowel at the end of a string is replaced properly `plkjhrwe` plkjhrwf digits are not replaced in mixed character/digit string `rwsd54t` and `rea34p` rwsd54t and rfb34p when `y` is used as a vowel it is replaced properly `sdfrytr` sdfrztr the vowel `a` is replaced properly `prtasr` prtbsr the vowel `e` is replaced properly `trert` trfrt the vowel `i` is replaced properly `pibnm` pjbnm the vowel `o` is replaced properly `wsopxs` wsppxs the vowel `u` is replaced properly `funsw` fvnsw an empty string produces no output boundary conditions success paths error handling paths Isolating Modules for Unit Tests 8 Driver for Testing (human or software) Report Code to be tested test cases Stub or Mock Stub or Mock Category of Test Test Case Expected Result all vowels are replaced correctly `fagehiwoquryt` fbgfhjwpqrrzt a vowel at the beginning of a string is replaced properly `abrtswghbc` bbrtswgjbc a vowel at the end of a string is replaced properly `plkjhrwe` plkjhrwf digits are not replaced in mixed character/digit string `rwsd54t` and `rea34p` rwsd54t and rfb34p when `y` is used as a vowel it is replaced properly `sdfrytr` sdfrztr the vowel `a` is replaced properly `prtasr` prtbsr the vowel `e` is replaced properly `trert` trfrt the vowel `i` is replaced properly `pibnm` pjbnm the vowel `o` is replaced properly `wsopxs` wsppxs the vowel `u` is replaced properly `funsw` fvnsw an empty string produces no output Junit • Libraries that provide the basic tools to test the methods of classes – setup and teardown of objects – assertions letting you test equality of results – record keeping We are using Junit 5 for the course The solutions to the practice problems in the e-text still use Junit 4 Creating Junit Tests • need a class to test • need some test cases • need the junit plugin in gradle •or whatever build tool you use • need to write some tests Testing Calculator.java this class is in the e-text problem solutions with junit 4 tests Test Case Charts Setup and TearDown ´Use @BeforeEach and @AfterEach annotations to set up and tear down resources before and after each test method execution ´The @BeforeEach annotated method runs before each test method. ´The @AfterEach annotated method runs after each test method. ´Multiple methods can be annotated Test Case Methods ´Test Case Annotation: To define a test case method, annotate it with @Test ´Expected Exception: Used if you expect an exception to be thrown. ´@Test(expected = IndexOutOfBoundsException.class) ´@Test(expected = ArithmeticException.class) ´Ignoring Incomplete Test Cases: To ignore an incomplete test case, use the @Disabled annotation ´JUnit 5's test runner will report the number of ignored tests Assertions ´Assertions are used to validate expected outcomes in your test cases. Common Assertion Methods: • assertEquals(expected, actual): Checks if two values are equal. • assertNotEquals(expected, actual): Checks if two values are not equal. • assertTrue(condition): Checks if a given condition is true. • assertFalse(condition): Checks if a given condition is false. • assertNull(actual): Checks if a value is null. • assertNotNull(actual): Checks if a value is not null. get Junit working with the XandO game Next week we are on a break • no labs/tutorials • no synchronous lecture • no quiz Looking Ahead Asynchronous Virtual Lectures • October 12 • October 17 • Study list will be emailed to you • topics are things that need to be done, not heard • Topics be on the Oct 19 quiz