Powermock: Method "PowerMockito.doThrow" works incorrect for void static method.

Created on 26 Aug 2016  路  6Comments  路  Source: powermock/powermock

Steps to reproduce:

  1. Create simple class:
  package PowerMock;
  import java.io.IOException;

  public class A {
     public static void someMethod() throws Exception{
        throw new IOException();
    }
  }
  1. Write test:
package PowerMock;

import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockTestCase;
import org.testng.annotations.Test;

import static org.powermock.api.mockito.PowerMockito.mockStatic;

@PrepareForTest(value = A.class)
public class TestClass extends PowerMockTestCase {

    @Test(expectedExceptions = IllegalStateException.class)
    public void tt() throws Exception {
        mockStatic(A.class);
        PowerMockito.doThrow(new IllegalStateException()).when(A.class);

        A.someMethod();
    }
}

This test will fail. This test works fine only in case we launch "someMethod" twice:

@PrepareForTest(value = A.class)
public class TestClass extends PowerMockTestCase {

    @Test(expectedExceptions = IllegalStateException.class)
    public void tt() throws Exception {
        mockStatic(A.class);
        PowerMockito.doThrow(new IllegalStateException()).when(A.class);

        A.someMethod();
       A.someMethod();
    }
}

Detail information: testng 6.8.8, powerMock 1.6.5

Not an issue

Most helpful comment

I was just looking at this issue. I wonder if this is a Mockito issue ?

import java.io.IOException;

public class A 
{
   public void someMethod() throws Exception
   {
      throw new IOException();
   }   
}

with the following test

import org.mockito.Mockito;
import org.testng.annotations.Test;

public class MockitoTest 
{

    @Test(expectedExceptions = IllegalStateException.class)
    public void tt() throws Exception {
        A a = Mockito.mock(A.class);
        Mockito.doThrow(new IllegalStateException()).when(a);

        a.someMethod();
      }
}

This Test does fail. It works only when we called a.someMethod() twice.

All 6 comments

@AndrienkoAleksandr @thekingnothing Reproduced the bug with JUnit 4.12 as well. Also, the other weird thing is that no breakpoints, sysouts are working inside someMethod().

I was just looking at this issue. I wonder if this is a Mockito issue ?

import java.io.IOException;

public class A 
{
   public void someMethod() throws Exception
   {
      throw new IOException();
   }   
}

with the following test

import org.mockito.Mockito;
import org.testng.annotations.Test;

public class MockitoTest 
{

    @Test(expectedExceptions = IllegalStateException.class)
    public void tt() throws Exception {
        A a = Mockito.mock(A.class);
        Mockito.doThrow(new IllegalStateException()).when(a);

        a.someMethod();
      }
}

This Test does fail. It works only when we called a.someMethod() twice.

@pankajb64 ,

Thank you of investigation. As I understand, it means that for Mockito 1, we cannot fix the issue.
And for mockito 2, fixing the issue doesn't make sense, because PowerMock can work only with old beta version.

Could you create and commit a test for this case with suffix Defect like the FieldMockDefect test, please? We will have test for the future, when we will implement supporting mockito 2.

Sorry,

I was going to create an issue for Mockito and double checked the code. I found out that it's incorrect using of Mockito API.

When you mock method with using doThrow/doReturn then you have to call this method after when().

So, it's incorrect:

Mockito.doThrow(new IllegalStateException()).when(a);

Correct version:

Mockito.doThrow(new IllegalStateException()).when(a).someMethod();

The same true for static method.

Incorrect:

        PowerMockito.mockStatic(A.class);
        PowerMockito.doThrow(new IllegalStateException()).when(A.class);

Correct:

        PowerMockito.mockStatic(A.class);
        PowerMockito.doThrow(new IllegalStateException()).when(A.class).someMethod();

So, I'm closing the issue as not a defect.

I am glad to know that this is not an issue, but if one follows the Powermock README or this, there is no syntax that supports

PowerMockito.doThrow(new IllegalStateException()).when(A.class).someMethod();

I achieved it by doing this instead

PowerMockito.spy(A.class); try {PowerMockito.doThrow(new CustomException()).when(A.class, "someMethod", anyArgs); } catch (Exception e) { e.printStackTrace(); }

I did it this way

mockStatic(System.class);
doThrow(new SystemExitException()).when(System.class);
System.exit(1);

Where SystemExitException is class SystemExitException extends RuntimeException.

Was this page helpful?
0 / 5 - 0 ratings