rewrite-testing-frameworks icon indicating copy to clipboard operation
rewrite-testing-frameworks copied to clipboard

EasyMock to Mockito

Open jpraet opened this issue 2 years ago • 1 comments

What problem are you trying to solve?

Migrate from EasyMock to Mockito.

Resources:

  • https://wiki.magnolia-cms.com/display/DEV/Converting+Easymock-Tests+to+Mockito
  • https://gist.github.com/stefanbirkner/1095194/904909cc229b6acb55c18f529e396089129e20e9

jpraet avatar Jan 07 '24 15:01 jpraet

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?

timtebeek avatar Jan 08 '24 10:01 timtebeek