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

How to mock System with 1.16 #155

Closed
ascassel opened this issue Mar 17, 2015 · 4 comments
Closed

How to mock System with 1.16 #155

ascassel opened this issue Mar 17, 2015 · 4 comments
Assignees
Labels

Comments

@ascassel
Copy link

I used to mock java.System to ensure that the tested code uses System.exit() with the expected return code - but so far I only get java.lang.IllegalArgumentException: Matching real methods not found for the following mocks:
test.Test$4#exit(int)
at test.Test$4.(Test.java:178)
......
new MockUp<System>()
{
@mock
public void exit( int code )
{
//verification code here
}
};
....

Is this no longer possible? Note that I use a IBM JAVA 7

@marcdonovan
Copy link

Don't mock java internals. It only leads to misery. Ditto for using IBM java. If you are not on a mainframe, there is no excuse.

  From: ascassel <notifications@github.com>

To: jmockit/jmockit1 jmockit1@noreply.github.com
Sent: Monday, March 16, 2015 7:31 PM
Subject: [jmockit1] How to mock System with 1.16 (#155)

I used to mock java.System to ensure that the tested code uses System.exit() with the expected return code - but so far I only get java.lang.IllegalArgumentException: Matching real methods not found for the following mocks:
test.Test$4#exit(int)
at test.Test$4.(Test.java:178)
......
new MockUp()
{
@mock
public void exit( int code )
{
//verification code here
}
};

....Is this no longer possible? Note that I use a IBM JAVA 7 —
Reply to this email directly or view it on GitHub.

@rliesenfeld
Copy link
Member

I am unable to see any problem. The following test works as expected:

    @Test
    public void mockSystemExit() {
        new MockUp<System>() {
            @Mock
            void exit(int i) {}
        };

        System.exit(1);
    }

@ascassel
Copy link
Author

Rogiero, thx for your answer.
You are right, it works in an Oracle JVM - but fails using the IBM one.
And yes, I need to be running on ZOS (thx Marc for the helpful comment... 8-) )
What frustrates more is that it worked in the 1.9 version..
Regards,
Achim

@rliesenfeld
Copy link
Member

I discovered why this isn't working anymore. It is described in the release notes for version 1.11: http://jmockit.org/changes.html#1.11

On the IBM JRE, java.lang.System gets explicitly excluded from mocking with a MockUp. This was done after seeing the test execution thread hanging up, when said class was mocked. It appears to be a bug in the IBM JVM; so, for safety, I decided to exclude java.lang.System entirely.

This isn't the only problem with the IBM JRE. On a developer machine which likely can use an Oracle JDK, I would recommed to use that for test execution. But if you have a ZOS build machine which can only use the IBM JRE, then there is no choice but to avoid mocking the System class.

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

No branches or pull requests

3 participants