How do I disable a test with @Disabled annotation?

In JUnit 5, you can disable a test by annotating it with @Disabled from the org.junit.jupiter.api package.

Disable a Single Test Method

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

class CalculatorTest {

    @Test
    @Disabled
    void divisionByZeroTest() {
        // This test will be skipped
    }
}

When you run the test suite, this test will not be executed.

Add a Reason

It is a good practice to include a reason so other developers know why the test is disabled.

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

class CalculatorTest {

    @Test
    @Disabled("Temporarily disabled until division-by-zero handling is fixed")
    void divisionByZeroTest() {
        // This test will be skipped
    }
}

Disable an Entire Test Class

You can also place @Disabled on a test class to skip all tests inside it.

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

@Disabled("Disabled while refactoring the calculator module")
class CalculatorTest {

    @Test
    void additionTest() {
        // This test will be skipped
    }

    @Test
    void subtractionTest() {
        // This test will also be skipped
    }
}

Summary

Use:

@Disabled

or preferably:

@Disabled("Reason why this test is disabled")

The required import is:

import org.junit.jupiter.api.Disabled;

@Disabled is useful for temporarily skipping tests that are incomplete, unstable, or depend on functionality that is not ready yet.

How do I add disabled icon for JTabbedPane tabs?

When adding a tabs to JTabbedPane you can define the icon for the tabs. This icon will be displayed beside the tab’s title in enable or disable state of the tabs. To make the user interface better you can also define a disabled icon for the tab. This icon will be displayed when the state of the tabs are disabled.

To assign a disabled icon for the tab use the JTabbedPane‘s setDisabledIconAt(int index, Icon icon) method. As always the index parameter is zero-based which means the first tab is at index number 0. The icon parameter is the disabled icon of your tabs.

package org.kodejava.swing;

import javax.swing.*;
import java.awt.*;
import java.util.Objects;

public class TabbedPaneDisableIcon extends JPanel {
    private final ImageIcon[] disableIcons = {
            new ImageIcon(Objects.requireNonNull(
                    this.getClass().getResource("/images/test-pass-icon-disable.png"))),
            new ImageIcon(Objects.requireNonNull(
                    this.getClass().getResource("/images/test-fail-icon-disable.png"))),
            new ImageIcon(Objects.requireNonNull(
                    this.getClass().getResource("/images/test-error-icon-disable.png"))),
    };

    public TabbedPaneDisableIcon() {
        initializeUI();
    }

    public static void showFrame() {
        JPanel panel = new TabbedPaneDisableIcon();
        panel.setOpaque(true);

        JFrame frame = new JFrame("JTabbedPane Demo");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setContentPane(panel);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(TabbedPaneDisableIcon::showFrame);
    }

    private void initializeUI() {
        this.setLayout(new BorderLayout());
        this.setPreferredSize(new Dimension(500, 200));

        JTabbedPane pane = new JTabbedPane();

        ImageIcon tab1Icon = new ImageIcon(Objects.requireNonNull(
                this.getClass().getResource("/images/test-pass-icon.png")));
        ImageIcon tab2Icon = new ImageIcon(Objects.requireNonNull(
                this.getClass().getResource("/images/test-fail-icon.png")));
        ImageIcon tab3Icon = new ImageIcon(Objects.requireNonNull(
                this.getClass().getResource("/images/test-error-icon.png")));
        JPanel content1 = new JPanel();
        JPanel content2 = new JPanel();
        JPanel content3 = new JPanel();

        pane.addTab("Pass", tab1Icon, content1);
        pane.addTab("Fail", tab2Icon, content2);
        pane.addTab("Error", tab3Icon, content3);

        for (int i = 0; i < pane.getTabCount(); i++) {
            pane.setDisabledIconAt(i, disableIcons[i]);
        }

        // Disable the last tab to see the disabled icon displayed.
        pane.setEnabledAt(pane.getTabCount() - 1, false);

        this.add(pane, BorderLayout.CENTER);
    }
}

The result of the code snippet above is:

Set Disabled Icon for JTabbedPane Tabs

Set Disabled Icon for JTabbedPane Tabs