openrewrite / rewrite-testing-frameworks

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

EasyMock to Mockito #454

Open jpraet opened 6 months ago

jpraet commented 6 months ago

What problem are you trying to solve?

Migrate from EasyMock to Mockito.

Resources:

timtebeek commented 6 months ago

Hi @jpraet ! Thanks for the suggestion; looking at the gist link this appears relatively straightforward:

  sed -i 's/org.easymock.EasyMock.createNiceMock/org.mockito.Mockito.mock/g' $item
  sed -i 's/org.easymock.EasyMock.createMock/org.mockito.Mockito.mock/g' $item
  sed -i 's/org.easymock.EasyMock.expect/org.mockito.Mockito.when/g' $item
  sed -i 's/org.easymock.EasyMock/org.mockito.Mockito/g' $item
  sed -i 's/EasyMock/Mockito/g' $item
  sed -i 's/createNiceMock/mock/g' $item
  sed -i 's/createMock/mock/g' $item
  sed -i 's/expect(/when(/g' $item
  sed -i 's/andReturn/thenReturn/g' $item
  sed -i 's/\.anyTimes()//g' $item
  sed -i '/replay[^A-Za-z0-9]/d' $item

At first glance most, if not all of these can be converted to use recipes we already have which you can compose in our recipe builder at https://app.moderne.io/recipes/builder For instance the first conversion could be replaced by

type: specs.openrewrite.org/v1beta/recipe
name: java.testing.easymock.EasyMockToMockito
displayName: Migrate from EasyMock to Mockito
description: This recipe will apply changes commonly needed when migrating from EasyMock to Mockito.
recipeList:
  - org.openrewrite.java.ChangeMethodTargetToStatic:
      methodPattern: org.easymock.EasyMock createNiceMock(..)
      fullyQualifiedTargetTypeName: org.mockito.Mockito
  - org.openrewrite.java.ChangeMethodName:
      methodPattern: org.mockito.Mockito createNiceMock(..)
      newMethodName: mock
...

We already have a very similar recipe for JMockit: https://github.com/openrewrite/rewrite-testing-frameworks/blob/main/src/main/resources/META-INF/rewrite/jmockit.yml

Would you be willing to create a first draft of these migration recipes to get this started?