Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8960223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:37:17+00:00 2026-06-15T15:37:17+00:00

I am trying to mock the static method Thread.sleep(1); to return an InterruptedException when

  • 0

I am trying to mock the static method Thread.sleep(1); to return an InterruptedException when it is called. I found a SO question that seemed to address my issue, but after setting up my code to be identical to the answer from that question it still did not work.

The SO question I found is: How to mock a void static method to throw exception with Powermock?

Here is the snippet of my method I’m trying to test:

try {
    Thread.sleep(1);
} catch (InterruptedException ie) {
    LOGGER.error("failure to sleep thread for 1 millisecond when persisting
        checkpoint. exception is: " + ie.getMessage());
}

And here’s a snippet from my test class which shows my attempt to mock Thread.sleep(1) to do what I want:

@RunWith(PowerMockRunner.class)
@PrepareForTest(Thread.class)
public class TestCheckpointDaoNoSQL {

        @Test
        public void test() throws InterruptedException {

        PowerMockito.mockStatic(Thread.class);
        PowerMockito.doThrow(new InterruptedException()).when(Thread.class);
        Thread.sleep(1);
        }
}

I also tried mocking the InterruptedException to throw instead of creating a new one, but that didn’t help. I can tell that the exception is not being thrown because ECLEMMA is not showing code coverage for that part of the method, and I debugged through the method to verify the catch phrase was never getting hit.

Thanks for taking a look at my issue!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:37:17+00:00Added an answer on June 15, 2026 at 3:37 pm

    Reading the answer indicates to me, that you still haven’t actually called Thread.sleep yet, but rather have just finished setting up the mock:

        @Test
        public void test() throws InterruptedException {
    
        PowerMockito.mockStatic(Thread.class);
        PowerMockito.doThrow(new InterruptedException()).when(Thread.class);
        Thread.sleep(1); //This is still setting up the mock, not actually invoking the method.
        }
    

    Note what is said there, toward the top: “Unless I make the two invocations of Adder.add() with the same argument, the mocked IOException won’t be thrown.” and later, “in fact Adder.add(12) above is part of setting up mock static method”.

    You should probably use a matcher like anyInt() in the first ‘call’ to Thread.sleep, then move on to executing the test.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to mock a method to have it return one specific object: private
Im trying to mock a method that is ineherited from a parent class that
I'm trying to mock a method call that takes a call-by-name argument: import org.scalatest.WordSpec
I am trying to mock out a private method that is making a JNDI
I am trying to Mock an object that is being passed into another object,
I'm trying to use mock to verify that an index property has been set.
I've been trying to get to mock a method with vararg parameters using Mockito:
I have a utility method that uses Display Templates to generate HTML: public static
I'm trying to write a unit test for a method that calls a FileWriter.
I'm trying to use FakeItEasy to mock an object that is a member of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.