Skip to content

JMockit to Mockito - rewrite expectation results with the correct type #508

Description

@kernanj

What problem are you trying to solve?

Within an Expectations block in JMockit, we can record method invocations and the expected result. JMockit has some internal logic to handle cases where there is a type mismatch between the result and the return type of the method and performs auto conversion.

Raising this ticket to kick off a discussion about creating a standalone recipe to clean this up or to add to the existing recipe created here - #415

See https://javadoc.io/static/org.jmockit/jmockit/1.49/mockit/Expectations.html#result for more info

Example

Consider the following class below:

class SomeObject {
        public long longMethod() {
            return 1L;
        }

        public List<Long> listMethod() {
            return Arrays.asList(1L);
        }
}

A test mocking the methods above might look something like follows:

@Injectable SomeObject someObject;

@Test
void test() {

    new Expectations() {{
        someObject.longMethod();
        result = 1;

        someObject.listMethod();
        result = 2;
    }};

    ....
}

Running the existing JMockitExpectationsToMockito recipe

@Mock SomeObject someObject;

@Test
void test() {
    
    when(someObject.longMethod()).thenReturn(1);  // Should be converted to the long literal 1L 
    when(someObject.listMethod()).thenReturn(2);  // Also doesn't compile because of type mismatch
    
   ...
}

As you can see above, the existing recipe to rewrite expectations doesn't account for this type conversion. Should we look at creating a separate recipe to first look at fixing up these issues and then apply the JMockitExpectationsToMockito recipe?

Are you interested in contributing this recipe to OpenRewrite?

Yes

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status
    Recipes Wanted

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions