openrewrite / openrewrite/rewrite-testing-frameworks

JMockit to Mockito - rewrite expectation results with the correct type

Open
#508 5 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement mockito
Dominant language
Java
Stars
100
Forks
105
Avg merge
2h 25m
Merged PRs (30d)
9

Description

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 - https://github.com/openrewrite/rewrite-testing-frameworks/pull/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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the existing JMockitExpectationsToMockito recipe and the related pull request 415, then compare its handling of result assignments with the examples in this issue. Clarify whether the type corrections belong in that recipe or a separate one; done means the converted long and List expectations compile with the correct Mockito return expressions.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
testing-qa, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.