UNC COMP 301 - F26/Lec 23 (Last) - Testing Principles and phases; Debugging (stack trace)
Overview
The lecture presents practical software-testing principles using JUnit and a playlist application: tests should be fast, independent, repeatable, self-validating, and timely, with clear setup, execution, verification, and teardown phases. It demonstrates JUnit fixtures, exception assertions, dependency injection and test doubles, unit versus integration testing, reproducible randomness with a fixed seed, and how Java stack traces expose exception causes and call paths.
Key takeaways
- JUnit tests should be independent and repeatable: @BeforeEach creates fresh setup for every test, so an add-song test and a remove-song test do not share modified playlist state.
- Test doubles let developers isolate behavior from slow or side-effecting dependencies: a stub supplies fixed answers, a spy records calls, and a fake provides a simplified but functional replacement.
- A test is more convincing when it passes for correct behavior and fails after a relevant defect is introduced, as demonstrated by removing the model's observer notification.
- Fixed-seed pseudorandom generators make shuffle tests reproducible; using seed 43 in both the model and test allows deterministic comparisons.
- Java stack traces provide a practical debugging path: inspect the exception type and message first, then follow application stack frames and any nested “Caused by” exception.
Chapters
- The final COMP 301 lecture focuses mainly on software testing, then briefly covers debugging and Java stack traces.
- The FIRST acronym introduces five testing qualities, beginning with fast: suites may contain hundreds or thousands of tests run after small code changes.
- Database access, network connections, and file I/O can slow tests, motivating techniques covered later in the lecture.
- JUnit tests in the same class are not guaranteed to run in source-code order and should not depend on one another.
- Repeatable tests return the same result across runs instead of relying on external state, static methods, or execution order.
- Self-validating tests use assertions to determine pass or fail, while timely tests are written before implementation through TDD or immediately afterward.
- Keep each test focused on one behavior rather than combining several checks into a single test method.
- Use sentence-like names such as “move up from top does nothing” to make the expected behavior clear.
- For an ArrayList with valid indices from 0 to n-1, test index 0, index n-1, an empty list, and invalid boundary values such as -1 or n.
- Structure a test as setup, execution, verification, and teardown.
- For a playlist containing songs A, B, C, and D, moving index 0 to index 2 should produce B, C, A, D.
- Garbage collection usually handles temporary in-memory objects, but files and connections may require explicit cleanup.
- A shared fixture can hold test fields and a method annotated with @BeforeEach, avoiding repeated playlist setup code.
- JUnit creates a fresh test-class instance and runs the setup method before each test method.
- An add-song test can assert a count of five after starting with four, while a separate remove-song test starts fresh and can assert four.
- A playlist exporter writes song titles, artists, and ratings to a file, so its test needs cleanup after execution.
- An @AfterEach method can delete the temporary file created by the test.
- On Windows, an open file may be locked and cause deletion to fail; closing it allows teardown to complete.
- @BeforeAll runs once before the test methods, and @AfterAll runs once after them; these methods are static in the lifecycle shown.
- @BeforeEach and @AfterEach instead run around every individual test.
- One-time setup can avoid repeatedly opening files or connecting to a database or network, helping keep test suites fast.
- JUnit's assertThrows takes the expected exception class and a lambda containing the operation that should throw it.
- Setting a song rating to -1 or 6 should throw IllegalArgumentException when the valid range is 0 through 5.
- Store the returned exception to assert its message as well as its type.
- Injecting a notifier into a checkout service lets tests substitute a fake instead of sending real email or SMS messages.
- Test doubles replace real dependencies that are slow, unpredictable, difficult to configure, side-effecting, or not yet implemented by another team member.
- A dummy satisfies a parameter requirement, a stub returns canned answers, a fake is a simplified working substitute, a spy records interactions, and a mock checks expected behavior.
- A RecordingObserver implements the model observer interface and records how many updates it receives and which model sent the last update.
- After moving a song from index 0 to index 2, a test can verify that the spy received exactly one update from the expected model.
- After removing the spy, adding a song should produce zero updates; removing the model's notification call makes the first test fail.
- A test should be checked against both working behavior and an introduced defect to confirm that it can detect failure.
- Unit tests target an individual method or class, while integration tests check behavior across components such as an MVC model and controller.
- A controller unit test can inject a stub model that reports a fixed song count without creating actual songs.
- The same test double can spy on calls and record old and new indices or the last song added.
- For a song already at index 0, the controller's “move up from top” behavior should make zero model calls.
- An integration test connects the real controller to a fake model that behaves more like the actual model than a simple stub.
- To avoid unpredictable shuffle results, the fake can reverse the song list, producing a deterministic transformation such as A, B, C, D becoming D, C, B, A.
- The controlled transformation helps test surrounding effects, such as whether songs are accidentally removed or added, without depending on random ordering.
- A pseudorandom number generator follows an algorithm, so the same seed reproduces the same sequence of values.
- The playlist model accepts a Random object through an overloaded constructor, allowing tests to control its random-number source.
- Using the same seed, such as 43, in the model and test makes shuffled results comparable and helps reveal unintended changes to the song list.
- The first line of an exception report identifies the thread, exception type, and message; removing index 0 from an empty playlist produces IndexOutOfBoundsException.
- Stack frames appear from the most recent call at the top to earlier calls below, so follow frames from Java library code to the first relevant application line.
- IDE links in stack frames can jump directly to source locations, such as the failing line and the methods that called it.
- A “Caused by” section exposes nested exceptions; a NumberFormatException can be wrapped in a custom PlaylistFormatException with a message such as “bad rating.”
Transcript excerpt
0:00 Right, good afternoon, everyone. Welcome to the last lecture for the semester. As I mentioned earlier, we will be finishing off before Thanksgiving, so there won't be any lecture on Tuesday on week from today. So in the last lecture I mentioned that one of the topics that I thought could deserve more attention was the topic of software testing. And I'll I'd like to spend most of today talking about software testing in practice. And we'll take a look at number of core principles that relate to testing and take a look at different aspects of testing which includes which includes
0:49 the four phases of a test and so on. And then time permitting, hopefully we'll have a few minutes at the end to take a look at debugging very briefly, take a look at a stack trace. And hopefully these topics should be useful for you when you go out in the industry. So let's take a look at some of the core principles. We'll start with the core principles that relate to good practices in testing. So an acronym that is used is referred to as first. And it corresponds to five good principles that are used in testing. And the first is stands the F here stands for fast. And essentially it's a core principle.
1:39 It implies that tests have to be fast. Now in practice you are typically running hundreds if not thousands of tests very frequently. And the reason is that as you write your tests and you make a small modification somewhere in your code, you have to run all your tests once Right? And so it's important that that your tests run fast. Now, there's a lot of reasons why tests can become slow. Some of these reasons are that, you know, you could have, for example, a
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Muhammad Sayeed Ghani.