Heart containing Coding Chica Java 101

Skipping A Flaky or Broken Test! JUnit’s Disabled Annotation

Table of Contents

  1. Table of Contents
  2. Introduction
  3. Format – JUnit 5
  4. Format JUnit 4
  5. Maven Build Output
  6. JaCoCo Minimum Code Coverage
  7. Summary

Introduction

Some times a test will work on one machine and break on another. Other times, a flaky test may succeed during one execution and fail on another run on the same machine. Either of these scenarios may warrant temporarily disabling a test until we have time to circle back and investigate if the issue is not a simple fix.

Format – JUnit 5

JUnit 5 supports an annotation that can be added to either an individual test method or an entire Java class containing tests in order to disable them.

I encourage you to use this annotation to document either a ticket number with details about the issue encountered, or if your project does not utilize issue tracking, a quick summary of the concern. We can also add a TODO comment, so that someone searching for known issues can find it that way, too.

// TODO ISSUEKEY-1234 Re-enable after troubleshooting race condition
@Disabled("ISSUEKEY-1234 Race Condition")
/** Unit test for simple App. */
public class AppTest {

Format JUnit 4

The same annotation exists in JUnit 4, but does not allow for in-annotation documentation as described above. Here, as well, it can either be applied to an individual test method or an entire Java class. Therefore, in JUnit 4, we can only utilize comments to document either the ticket number with details about the concern or what the issue might be.

// TODO ISSUEKEY-1234 Re-enable after troubleshooting race condition
@Disabled
public void testMain_givenNullArguments_whenInvoked_thenNoException() {

Maven Build Output

When a test or a class of tests is skipped in the output. In the example below, the

[INFO] Running codingchica.java101.AppTest
[WARNING] Tests run: 6, Failures: 0, Errors: 0, Skipped: 6, Time elapsed: 0.003 s -- in codingchica.java101.AppTest

As the test was skipped, it will no longer be included in unit test code coverage calculations.

JaCoCo Minimum Code Coverage

If a quality gate is enabled to enforce minimum code coverage, then disabling the test(s) may cause the project’s code coverage to fall below acceptable limit(s) and cause a build failure. We should do so with caution.

Summary

We can skip or disable unit tests in JUnit by adding a Disabled annotation to a test method or class. Doing so will cause the test(s) to be marked as skipped in the Maven build output and may also lower our unit test code coverage. If we have quality gate(s) enabled enforcing minimum unit test code coverage, this could cause the build to fail, so it should be done with caution.

Skipping A Flaky or Broken Test! JUnit’s Disabled Annotation

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.