Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PowerMockRule interacts poorly with mocked member variables #362

Open
GoogleCodeExporter opened this issue Sep 23, 2015 · 1 comment
Open

Comments

@GoogleCodeExporter
Copy link

What steps will reproduce the problem?

Following the methodology here: 
http://code.google.com/p/powermock/wiki/PowerMockRule

Set up a test that mocks some sort of static class.  In this case it's a custom 
SQLUtils object that takes a java.sql.Connection and a string.  For our 
purposes here I am using xstream, but I have been able to reproduce it with 
objenesis.  Here's a standard pattern for testing:
@PrepareForTest({SQLUtils.class})
public class PowerMockTest {

    @Rule
    public final PowerMockRule powermock = new PowerMockRule();

    private final Connection conn = mock(Connection.class);

    @Before
    public void setUp() throws Exception {
        reset(conn);
        PowerMockito.mockStatic(SQLUtils.class);
    }

    @Test
    public void testCall() throws Exception {

        SQLUtils.execute(conn, "my sql");
    }
}

What is the expected output? What do you see instead?

I'd expect it to run.  Instead it throws the attached exception.

What version of the product are you using?

1.4.11 with Mockito 1.9 and junit 4.10.  Running in Eclipse, dependencies being 
managed by Maven. 

Please provide any additional information below.

The following code executes without issue:

@PrepareForTest({SQLUtils.class})
public class PowerMockTest {

    @Rule
    public final PowerMockRule powermock = new PowerMockRule();

    private Connection conn;

    @Before
    public void setUp() throws Exception {
        conn = mock(Connection.class);
        PowerMockito.mockStatic(SQLUtils.class);
    }

    @Test
    public void testCall() throws Exception {
        SQLUtils.execute(conn, "my sql");
    }
}

It also executes without any issue if I use the @RunWith(PowerMockRunner.class) 
annotation instead of the PowerMockRule.

The following things were tried to get the Rule to work:

1) I could not find any combination of things I could put into @PowerMockIgnore 
that would fix the issue (attempting ignoring java.sql.* java.sql.Connection 
org.mockito.* etc). Bizarrely, ignoring SQLUtils worked, but then the class 
wasn't mocked. 
2) Initializing the objects via a constructor.
3) Marking the rule as Static. 
4) Changing the dependency ordering (if this is the problem, I couldn't find 
the exact combination).
5) Putting the Rule declaration in a parent class (along with putting the 
declarations in a parent class). 

So far the only solution I've found is to initialize all of my mocks in the 
setUp() method.  I can do this as a workaround, but  I couldn't find any 
documentation indicating that what I am doing is not supported.

Original issue reported on code.google.com by nachtr...@gmail.com on 18 Jan 2012 at 10:15

Attachments:

@GoogleCodeExporter
Copy link
Author

Hmmm... It seems like it fails to deep clone the mock because it's not loaded 
by the same classloader.

Feel free to try the javaagent based Rule instead.

Original comment by johan.ha...@gmail.com on 20 Jan 2012 at 4:10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant