jmockit / jmockit1

Advanced Java library for integration testing, mocking, faking, and code coverage
Other
464 stars 239 forks source link

Unsupported fake for private method #714

Open ondrejlerch opened 3 years ago

ondrejlerch commented 3 years ago

From JMockit 1.47 it is not possible to mock private methods, such attempt leads to "Unsupported fake for private method" exception,

We find private method mocking useful in a situation when we call third party libraries out of our control.

E.g. We are using spring-ws and want to check what kind of soap request was actually sent. With JMockit <= 1.46 we have used:

new MockUp<WebServiceTemplate>() {
    @Mock
    private void sendRequest(Invocation invocation, WebServiceConnection connection, WebServiceMessage request) throws IOException {
        // some assert here
        invocation.proceed(connection, request);
    }
};

Obviously WebServiceTemplate code is out of our control: https://github.com/spring-projects/spring-ws/blob/main/spring-ws-core/src/main/java/org/springframework/ws/client/core/WebServiceTemplate.java#L626

JMockit was a great mocking tool for such corner cases and it allowed us to mock anything, including private methods of third party libraries.

We have been happily using JMockit for a long time on many projects and now we are stuck with 1.46 since we don't want to investigate how to replace all our private method mocks with some protected or public counterparts which is not straightforward, desirable or even possible.

Therefore we suggest to put private method mocking back. Or at least disable it by default and enable it somehow via JMockit API if you find it dangerous.

PS: We have noticed that JMockit was not released since 2019. We would be happy if this project continues since we find it great.

Many thanks ✌

AlexFTrapp commented 2 years ago

I too would like to have tha abbility back to mock private methods. In my case these private methods are mocked in order to reduce complexity of test cases. For example the method under test calls some private method and this private method provides access to cached items and has a fallback if an item is not in cache (load from db). In any case I want to mock the private method to return items with special field values matching the test set. Using the cache or accessing the DB is NOT of concern when testing the method under test.

AskSantoshYadav commented 1 year ago

It would be great if ability to mock private class and methods brought back. In my case all java classes and methods are private.

Therefore we request to put private class/ method mocking back. If there there is any alternative please share, or Like ondrejlerch mentioned at least disable it by default and enable it somehow via JMockit API if you find it dangerous.

Thanks in advance.