Repository navigation
Use MethodMatcher, AnnotationMatcher and variable types instead of name strings - #1228
Merged
Merged
Conversation
…ockito, JMockit and EasyMock recipes
23 of 76 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Several recipes decided what a call or annotation was by comparing simple names, so a user's own
equalTo(..),setUp()orreturns(..)method, or a variable that merely shared a name with a mock, could be picked up. This switches those spots toMethodMatcher,AnnotationMatcherandJavaType.Variablecomparisons.Matchers instead of name strings:
MigrateJUnitTestCase:setName(String),setUp()andtearDown()are matched onjunit.framework.TestCase, which replaces theisTestCaseMethodhelper plus name checks. Thesuite()removal now uses a*..* suite()matcher, so only the no-argumentsuite()that JUnit 3 calls is removed.AssertToAssertions: the two-argumentassertSame/assertNotSame/assertEquals/assertNotEqualscheck and theassertThatexclusion useMethodMatchers onorg.junit.Assert.HamcrestMatcherToJUnit5: eachReplacementgets anorg.hamcrest..* <name>(..)matcher. This replacesReplacement.valueOf(name.toUpperCase()), which used a caughtIllegalArgumentExceptionas control flow.MockitoUtils.getFirstTestMethodmatches the JUnit 4, JUnit 5 and TestNG@Testannotations rather than any annotation namedTest.WhiteboxToReflectionVisitormatches@java.lang.Override.RemoveTestPrefix,SimplifyTestThrowsandTestMethodsShouldBeVoid: thehasJUnit5MethodAnnotationchains use a list ofAnnotationMatchers, asTestsShouldNotBePublicdoes. Each recipe keeps its own annotation set. Meta-annotation matching is left off, so composed annotations are still not picked up and behaviour is unchanged.Variable and type identity:
ArgumentMatchersRewriteronly treatsanyString,anyIntand the like as JMockit matchers when the field is declared onmockit.Invocations.JMockitBlockRewriterrecognisesreturns(..)with amockit.Expectations returns(..)matcher, so a mocked method namedreturnsis no longer mistaken for it. TheFullVerifications/VerificationsInOrdermock de-duplication compares mocks withSemanticallyEqualinstead of by simple name.SetupStatementsRewritertracks spies as identifiers and compares them withSemanticallyEqualas well.EasyMockVerifyToMockitoVerifycompares theverify(..)argument with theexpect(..)receiver the same way.SemanticallyEqualrequires equal simple names and equal field types. Unattributed identifiers (both field typesnull) therefore still match by name, as before.Skipped from the item:
ParameterizedRunnerToParameterized"super".equals(..): this checks for thesuperkeyword in a constructor call. A type check cannot tellsuper(..)fromthis(..)without comparing against the class's supertype, which would be more code than the keyword check for no gain.ArgumentMatchersRewriter"this".equals(..): this is also a keyword check. Matchers are now identified by type inisJmockitArgumentMatcher.SetupStatementsRewriter.getVariableNames: it detects clashes between local variable names when setup statements move out of the block. Different declarations never share aJavaType.Variable, so names are the correct key there.Verified with
./gradlew test --tests 'org.openrewrite.java.testing.{jmockit,easymock,hamcrest,cleanup,junit5,mockito,testng}.*'(each package passed as its own--testsfilter): 171 classes and 1434 tests, 12 skipped, no failures. This covers the JMockit, EasyMock and JUnit 4-to-5 composite tests.Fixes item N7 of #1211.