openrewrite / rewrite-testing-frameworks

OpenRewrite recipes that perform common Java testing migration tasks.
Apache License 2.0
68 stars 57 forks source link

JMock to Mockito Migration #529

Open alfarhanzahedi opened 1 week ago

alfarhanzahedi commented 1 week ago

What problem are you trying to solve?

I am trying to migrate from JMock to Mockito. Multiple of my projects use a combination of JMock and Mockito for unit testing. I am trying to consolidate the tests to just use Mockito.

What precondition(s) should be checked before applying this recipe?

Mockito v5.x should be preferable.

Describe the situation before applying the recipe

    @Test
    public void sampleTest() {
        ...
        mockery.checking(new Expectations() {

            {
                allowing(objectOne).getPropertyOne();
                will(returnValue(propertyOneValue));
                allowing(objectTwo).getPropertyTwo();
                will(returnValue(propertyTwoValue));
            }
        });
        ...
    }

Describe the situation after applying the recipe

    @Test
    public void sampleTest() {
        when(objectOne.getPropertyOne()).thenReturn(propertyOneValue);
        when(objectTwo.getPropertyTwo()).thenReturn(propertyTwoValue);
    }

Have you considered any alternatives or workarounds?

Apart from doing a manual migration, no.

Are you interested in contributing this recipe to OpenRewrite?

Yes. I am interested :D

alfarhanzahedi commented 1 week ago

If there's already a recipe that solves this, do share. I could not find one.

timtebeek commented 1 week ago

hi @alfarhanzahedi ! There certainly is no shortage of mocking libraries with Java is there? 😅

We already have recipes to go from JMockit to Mockito, as well as from PowerMock and older versions of Mockito to v5. We also have requests to cover additional libraries & cases such as

Thanks for the offer to help cover JMock as well! From the recipes we have already I think there should be some decent examples in there on how to achieve such a migration. Feel free to start a draft PR with just some tests early to get feedback on where to go next. Typically we find that some initial steps are easy to add, and then from there look at what's needed to get decent coverage for a mostly automated migration.

dionmeijer commented 3 days ago

I would be interested in using this recipe too. 👍

timtebeek commented 3 days ago

Glad to hear! Feel free to kick this off with a draft pull request containing just the tests, and then I'll try to guide you from there.