[JAVA] How to test interrupts during Thread.sleep with JUnit

Introduction

I've investigated how to raise a Thread.sleep interrupt exception in JUnit, so I'll leave it as a reminder.

■ Environment Java 8 JUnit 4

Overview

Wake up "interrupt thread" from "test running thread". Interrupts from the "interrupt thread" to the "test running thread".

In order to interrupt, it is necessary to tell "thread running test" to "thread for interrupt" You can get the thread that is executing processing with Thread.currentThread ().

code

Code to be tested


public class SampleClass {
    public void sample() throws InterruptedException {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw e;
        }
    }
}

Test code


@Test
public void testSample() {
    try {
        //Define a thread for interrupts
        final class InterruptThread extends Thread {
            Thread targetThread = null;

            public InterruptThread(Thread thread) {
                targetThread = thread;
            }

            @Override
            public void run() {
                try {
                    Thread.sleep(100);
                    targetThread.interrupt();
                } catch (InterruptedException e) {
                }
            }
        }

        //Start an interrupt thread
        InterruptThread th = new InterruptThread(Thread.currentThread());
        th.start();

        //Run the code under test
        SampleClass target = new SampleClass();
        target.sample();
        fail();
    } catch (InterruptedException e) {
        assertEquals(e.getMessage(), "sleep interrupted");
    }
}

Recommended Posts

How to test interrupts during Thread.sleep with JUnit
How to test private scope with JUnit
How to filter JUnit Test in Gradle
How to test private methods with arrays or variadic arguments in JUnit
JUnit 5: How to write test cases in enum
How to use JUnit 5
How to write test code with Basic authentication
Unit test with Junit.
How to perform UT with Excel as test data with Spring Boot + JUnit5 + DBUnit
How to unit test with JVM with source using RxAndroid
How to use JUnit (beginner)
How to number (number) with html.erb
How to update with activerecord-import
How to write Junit 5 organized
How to migrate from JUnit4 to JUnit5
[Creating] How to use JUnit
How to run only specific files with gem's rake test
How to test a private method with RSpec for yourself
How to erase test image after running Rspec test with CarrierWave
[Java] I want to test standard input & standard output with JUnit
How to scroll horizontally with ScrollView
How to unit test Spring AOP
How to run JUnit in Eclipse
How to enclose any character with "~"
How to use mssql-tools with alpine
[Java] Test private methods with JUnit
How to get along with Rails
[RSpec] How to write test code
Test Spring framework controller with Junit
How to start Camunda with Docker
How to deal with the event that Committee :: InvalidRequest occurs in committee during Rspec file upload test
How to crop an image with libGDX
How to share files with Docker Toolbox
[Java] How to compare with equals method
Control test order in Junit4 with enumeration
[Android] How to deal with dark themes
How to use BootStrap with Play Framework
[Rails] How to use rails console with docker
How to switch thumbnail images with JavaScript
[Note] How to get started with Rspec
How to do API-based control with cancancan
How to fix system date in JUnit
How to achieve file download with Feign
How to update related models with accepts_nested_attributes_for
How to set JAVA_HOME with Maven appassembler-maven-plugin
How to implement TextInputLayout with validation function
How to handle sign-in errors with devise
How to delete data with foreign key
JUnit 5 gradle test fails with lombok annotation
How to monitor nginx with docker-compose with datadog
Java automated test implementation with JUnit 5 + Gradle
How to deal with Precompiling assets failed.
How to write an RSpec controller test
[SpringBoot] How to write a controller test
How to achieve file upload with Feign
How to run Blazor (C #) with Docker
How to build Rails 6 environment with Docker
How to test a class that handles application.properties with SpringBoot (request: pointed out)
How to download Oracle JDK 8 rpm with curl
How to mock each case with Mockito 1x
I want to test Action Cable with RSpec test