Java Testing with JUnit 5

Java Training Overview

This course introduces experienced Java developers to the fundamentals and best practices in unit testing. It uses the JUnit 5 and Mockito libraries, both of which are ubiquitous in the Java community. It is intended for both developers who are new to testing, as well as those who are already familiar with it, but want more experience with testing using JUnit 5.

Note that JUnit 5 is substantively different than JUnit 4. Although the core testing principles are the same, there are substantial implementation differences, important new features, and a brand new extension model. Developers with JUnit 3 and 4 experience will be well-served by the course.

Testing with mocks is covered in detail, using Mockito. Mocking is a standard technique, and we'll go beyond the basics to introduce more flexible options, as well as Mockito's dependency injection facilities. We use the new MockitoExtension to integrate Mockito into JUnit tests.

We explore testing enterprise components in the persistence, service, and web layers, outlining the issues involved in each. In the persistence layer, we show how to set up and use an embedded database, and contrast this to using mocks or fakes as a standalone alternative. We examine the unique issues in database testing with regard to test repeatability and independence, using various techniques for auto-rollback of transactions.

In-container testing is introduced as a more meaningful approach to testing enterprise components, vs. standalone testing with mocks, and we examine this at each of the main layers in an enterprise application.

Best practices are emphasized and demonstrated throughout, and our goal is to make you "test infected," so that your development effort is as much about good testing and writing testable code as it is about writing the business code itself.

The student is expected to have a good working knowledge of Java and OO, including the use of interfaces, abstract classes, collections, factories, and generics. Experience with Java 8 lambda expressions is helpful, but not strictly required. A brief primer on the Java 8 features employed by JUnit is included in the course and covered if necessary.

All labs are done with the Eclipse IDE, and the lab instructions include detailed directions for setting up and using it. The standard applicaiton server used is TomEE, but it is available for other major app servers, including WildFly/JBoss AS and IBM WebSphere AS, with advanced notice and possible additional charge.

Java Training Skills Gained

  • Understand new JUnit 5 library structure, role of each component, and how they interact
  • Configure IDE projects to run tests natively, and via Maven Surefire
  • Write cohesive and effective tests, and design classes for testability
  • Understand the full test lifecycle, and employ it to configure test fixtures
  • Run tests using all available mechanisms: IDE, Maven, JUnit Console Launcher, Launcher API
  • Use test discovery and filtering to define and run test plans, including conditional test execution
  • Employ naming conventions at every level - test name, classname, display name
  • Organize tests with assertion groups and nested tests
  • Use test interfaces to apply good OO principles to testing
  • Understand the new JUnit extension model, and how to write and use them
  • Understand JUnit 4 compatibility and migration
  • Use mock objects with Mockito to support isolated testing
  • Explore Mockito's facilities for dependency injection of mocks
  • Use argument matchers for more generalized testing with mocks
  • Implement partial mocking with spies
  • Understand the issues in testing enterprise components
  • Understand the two basic approaches: standalone testing with mocks, and in-container testing
  • Test database access components, using both fakes and an embedded database
  • Understand the additional issues involved in testing the service and web layers
  • Exercise best practices throughout the testing effort

Minimum 50% hands-on

Supported Platforms

Eclipse, Java 8. IntelliJ and IBM RAD available with advanced notice and possible additional charge.

Java Training Course Duration

2 Days

Java Training Course outline

Session 1: Unit Testing with JUnit 5

  • Overview
    • Unit Testing and JUnit Overview
    • New Features in JUnit 5
    • JUnit 5 Library Components
    • Naming Conventions and Organizing Tests
  • Tests and Assertions
    • Writing Test Methods
    • Assertions
    • Assertion Messages
  • Test Fixtures and Test Lifecycle
    • Creating and Using Text Fixtures
    • Test Run Lifecycle: @BeforeEach and @AfterEach, @BeforeAll and @AfterAll
    • Controlling Test Instances

Session 2: Writing and Running Tests (includes brief primer on Java 8 new features)

  • Additional Testing Needs
    • Testing for Exceptions
    • Setting Timeouts
    • Assertion Groups
  • Running Test
    • IDE Support: Eclipse, IntelliJ IDEA
    • Maven Configuration
    • JUnit Platform Console Launcher
    • Launcher API
    • Test Discovery and Selection
    • Display Names
    • Grouping and Filtering with Tags
    • Configuration Parameters
    • Nested Tests
  • Advanced Capabilities
    • Custom Composed Annotations
    • Inheritance with Test Classes
    • Extensions
    • Conditional Test Execution
    • Parameterized Tests
  • JUnit 4 Migration
    • The Do-Nothing Case
    • Using a JUnit 4 Runner
    • API Changes
    • JUnit 4 Runners and Rules
    • JUnit 4 Test Suites
  • Best Practices
    • Testing Void and Private Methods
    • Test Cohesion and Assertion Scope
    • Characteristics of Good Tests
    • Writing Testable Code
    • Testing Anti-Patterns

Session 3: Testing with Mocks

  • Overview
    • Mock Objects as Collaborators
    • Mockito Overview
  • Creating and Using Mocks
    • Basic Steps in Mocking
    • The Mockito Class
    • Mock Creation with @Mock
    • JUnit 5 MockitoExtension
    • Stubbing
  • Additional Capabilities
    • Argument Matchers
    • Partial Mocking with Spies
    • Mocking the Unmockable
    • Dependency Injection of Mocks

Session 4: Testing Enterprise Components

  • Overview
    • Unit Testing vs. Integration Testing
    • Testing with Mocks vs. In-Container Testing
    • Mocks vs. Fakes
  • Testing the Persistence Layer
    • Database Options: Installed, Embedded, Embedded-in-Memory
    • Standalone vs. In-Container Testing
    • Test Independence and Transaction Rollback
    • In-Container Testing with Arquillian [Overview]
  • Testing Services
    • Similar Issues, Different Layer
    • Working with External Resources
    • The Argument for In-Container Testing
  • Testing Web Components
    • Interfacing with External Clients
    • Difficulties in Standalone Testing with Mocks
    • Manual vs. Automated Testing
    • Automated Testing with Selenium [Overview]

Wintrac Inc.
16523 SW McGwire Ct.
Beaverton OR 97007
Wintrac, Inc. All rights reserved.                                                                               Site Map   |   Terms of Use   |   Privacy Policy