Join Simon Allardice for an in-depth discussion in this video Creating a test for expected exceptions, part of Foundations of Programming: Test-Driven Development.
- [Voiceover] It's common in our application to create…methods that deal with incorrect input or some other issue…by throwing a known exception,…which can then be caught and handled…elsewhere in the application.…But to test this individually, meaning we're testing this…happening at the unit test level,…we need a way to fully expect when an exception can occur.…Now, this is one of the areas in unit testing…where the way you do this will differ a little based…on the language, so let's take a look.…Starting in JUnit.…I am creating a calculator class here, and I want to check…its divide functionality.…
Now, I'll have some other tests to check it works correctly…with good data, but here I want to check its behavior…if we pass any bad data.…In this case, asking it to divide by zero.…So I want this divide method to actually throw an exception.…I'll write this test first, I'll run it, and it will fail…because the functionality doesn't exist yet.…That's fine.…I go ahead and write the divide method.…Now, I'm not going to jump over into that.…
The course explores the jargon of TDD—test suites, test harness, mock and stub objects, and more—and covers how TDD is used in the most common programming languages and environments. Plus learn to create, run, and manage the tests and move to a test-first mindset.
- What is test-driven development?
- Using unit testing frameworks
- Creating tests
- Using assertions
- Creating multiple test methods
- Naming unit tests and test methods
- Testing return values
- Setting up and tearing down
- Introducing mock objects
- Measuring code coverage