2

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!

Community
  • 1
  • 1
CorayThan
  • 17,174
  • 28
  • 113
  • 161
  • 1
    Your code looks almost exactly like the code in the question at the link you posted. Did you try something that looked like the Answer that person came up with? – femtoRgon Dec 04 '12 at 20:25
  • It looks almost exactly like that code because I copied their solution. I'm not sure if it didn't work for me because I did something differently / wrong, or if it doesn't work for Thread.sleep specifically. – CorayThan Dec 04 '12 at 20:29
  • Don't just copy the code, Read what they wrote. I think you're missing the context of their solution. – femtoRgon Dec 04 '12 at 20:40

1 Answers1

3

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.

femtoRgon
  • 32,893
  • 7
  • 60
  • 87
  • I tried it with anyInt() and I didn't include my assert statement in my snippet which does excecute the Thread.sleep method, so I think I've already done all that. Sorry, I should've been more complete in my original question! – CorayThan Dec 04 '12 at 21:18
  • Ah, okay. Thought that was the full extent of your test method. You mentioned debugging the method. Have you tried breaking on you call to Thread.sleep(1) in your unit under test, and see what gets executed there (whether it executes the actual Thread.sleep, or steps into some sort of Mockito architecture)? – femtoRgon Dec 04 '12 at 22:33