Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
}

maybeRemoveImport(JUNIT);
maybeAddImport(ASSERTJ, "assertThat", false);
maybeAddImport(ASSERTJ, "assertThat");

List<Expression> args = md.getArguments();
Expression expected = args.get(0);
Expand All @@ -79,7 +79,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
.apply(getCursor(), md.getCoordinates().replace(), actual, message, expected);
}

maybeAddImport(ASSERTJ, "within", false);
maybeAddImport(ASSERTJ, "within");
Expression delta = args.get(2);
if (args.size() == 3) {
return JavaTemplate.builder("assertThat(#{anyArray()}).containsExactly(#{anyArray()}, within(" + widenedTo(deltaType, delta) + "));")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
}

maybeRemoveImport(JUNIT);
maybeAddImport(ASSERTJ, "assertThat", false);
maybeAddImport(ASSERTJ, "assertThat");

List<Expression> args = mi.getArguments();
Expression expected = args.get(0);
Expand Down Expand Up @@ -96,7 +96,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
.build()
.apply(getCursor(), mi.getCoordinates().replace(), actual, expected);
}
maybeAddImport(ASSERTJ, "within", false);
maybeAddImport(ASSERTJ, "within");
return JavaTemplate.builder("assertThat(" + widenedTo(deltaType, actual) + ").isCloseTo(#{any()}, within(" + widenedTo(deltaType, delta) + "));")
.staticImports(ASSERTJ + ".assertThat", ASSERTJ + ".within")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3"))
Expand All @@ -113,7 +113,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
.build()
.apply(getCursor(), mi.getCoordinates().replace(), actual, message, expected);
}
maybeAddImport(ASSERTJ, "within", false);
maybeAddImport(ASSERTJ, "within");
return JavaTemplate.builder("assertThat(" + widenedTo(deltaType, actual) + ").as(#{any()}).isCloseTo(#{any()}, within(" + widenedTo(deltaType, delta) + "));")
.staticImports(ASSERTJ + ".assertThat", ASSERTJ + ".within")
.imports("java.util.function.Supplier")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
}

maybeRemoveImport("org.junit.jupiter.api.Assertions");
maybeAddImport("org.assertj.core.api.Assertions", "assertThat", false);
maybeAddImport("org.assertj.core.api.Assertions", "assertThat");

List<Expression> args = mi.getArguments();
Expression actual = args.get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
}

maybeRemoveImport("org.junit.jupiter.api.Assertions");
maybeAddImport("org.assertj.core.api.Assertions", "assertThat", false);
maybeAddImport("org.assertj.core.api.Assertions", "assertThat");

Expression expected = mi.getArguments().get(0);
Expression actual = mi.getArguments().get(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
}

maybeRemoveImport(JUNIT);
maybeAddImport(ASSERTJ, "assertThat", false);
maybeAddImport(ASSERTJ, "assertThat");

List<Expression> args = mi.getArguments();
Expression expected = args.get(0);
Expand All @@ -84,7 +84,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
.apply(getCursor(), mi.getCoordinates().replace(), actual, message, expected);
}

maybeAddImport(ASSERTJ, "within", false);
maybeAddImport(ASSERTJ, "within");
Expression delta = args.get(2);
if (args.size() == 3) {
return JavaTemplate.builder("assertThat(" + widenedTo(deltaType, actual) + ").isNotCloseTo(#{any()}, within(" + widenedTo(deltaType, delta) + "));")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
}

maybeRemoveImport("org.junit.jupiter.api.Assertions");
maybeAddImport("org.assertj.core.api.Assertions", "assertThat", false);
maybeAddImport("org.assertj.core.api.Assertions", "assertThat");

List<Expression> args = mi.getArguments();
Expression actual = args.get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
}

maybeRemoveImport("org.junit.jupiter.api.Assertions");
maybeAddImport("org.assertj.core.api.Assertions", "assertThat", false);
maybeAddImport("org.assertj.core.api.Assertions", "assertThat");

List<Expression> args = mi.getArguments();
Expression actual = args.get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
}

maybeRemoveImport("org.junit.jupiter.api.Assertions");
maybeAddImport("org.assertj.core.api.Assertions", "assertThat", false);
maybeAddImport("org.assertj.core.api.Assertions", "assertThat");

List<Expression> args = mi.getArguments();
Expression expected = args.get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
}

maybeRemoveImport("org.junit.jupiter.api.Assertions");
maybeAddImport("org.assertj.core.api.Assertions", "assertThat", false);
maybeAddImport("org.assertj.core.api.Assertions", "assertThat");

List<Expression> args = mi.getArguments();
Expression actual = args.get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
}

maybeRemoveImport(JUNIT + ".fail");
maybeAddImport(ASSERTJ, "fail", false);
maybeAddImport(ASSERTJ, "fail");

List<Expression> arguments = mi.getArguments();
String anyArgs = String.join(",", nCopies(arguments.size(), "#{any()}"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,7 @@ private void rewriteResult(J.MethodInvocation invocation, List<Expression> resul
}

setNextStatementCoordinates(++numStatementsAdded);
// do this last making sure rewrite worked and specify onlyifReferenced=false because framework cannot find static
// reference for when method invocation when another static mockit reference is added
visitor.maybeAddImport(MOCKITO_IMPORT_FQN_PREFX, "when", false);
visitor.maybeAddImport(MOCKITO_IMPORT_FQN_PREFX, "when");
if (lenient) {
visitor.maybeAddImport(MOCKITO_IMPORT_FQN_PREFX, "lenient");
}
Expand Down Expand Up @@ -431,15 +429,15 @@ private void rewriteVerify(J.MethodInvocation invocation, @Nullable Expression t
setNextStatementCoordinates(++numStatementsAdded);
}

visitor.maybeAddImport(MOCKITO_IMPORT_FQN_PREFX, "verify", false);
visitor.maybeAddImport(MOCKITO_IMPORT_FQN_PREFX, "verify");
if (!verificationMode.isEmpty()) {
visitor.maybeAddImport(MOCKITO_IMPORT_FQN_PREFX, verificationMode);
}
}

private void rewriteFullVerify(List<Object> mocks) {
if (rewriteMultipleMocks(mocks, VERIFY_NO_INTERACTIONS_TEMPLATE_PREFIX)) { // verifyNoMoreInteractions(mock1, mock2 ...
visitor.maybeAddImport(MOCKITO_IMPORT_FQN_PREFX, "verifyNoMoreInteractions", false);
visitor.maybeAddImport(MOCKITO_IMPORT_FQN_PREFX, "verifyNoMoreInteractions");
}
}

Expand All @@ -450,7 +448,7 @@ private void rewriteInOrderVerify(List<Object> mocks) {
}
sb.append(VERIFY_IN_ORDER_TEMPLATE_PREFIX_2); // InOrder inOrder1 = inOrder(
if (rewriteMultipleMocks(mocks, sb.toString())) { // InOrder inOrder = inOrder(mock1, mock2 ..)
visitor.maybeAddImport(MOCKITO_IMPORT_FQN_PREFX, "inOrder", false);
visitor.maybeAddImport(MOCKITO_IMPORT_FQN_PREFX, "inOrder");
visitor.maybeAddImport(IN_ORDER_IMPORT_FQN);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDecl
}

maybeAddImport(MOCKITO_CONSTRUCTION_IMPORT);
maybeAddImport("org.mockito.Answers", "CALLS_REAL_METHODS", false);
maybeAddImport("org.mockito.AdditionalAnswers", "delegatesTo", false);
maybeAddImport("org.mockito.Answers", "CALLS_REAL_METHODS");
maybeAddImport("org.mockito.AdditionalAnswers", "delegatesTo");
}

List<Statement> statements = md.getBody().getStatements();
Expand Down Expand Up @@ -300,15 +300,15 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDecl

maybeRemoveImport(JMOCKIT_MOCK_IMPORT);
maybeRemoveImport(JMOCKIT_MOCKUP_IMPORT);
maybeAddImport(MOCKITO_ALL_IMPORT.replace(".*", ""), "*", false);
maybeAddImport(MOCKITO_ALL_IMPORT.replace(".*", ""), "*");

doAfterVisit(new LambdaBlockToExpression().getVisitor());
doAfterVisit(ShortenFullyQualifiedTypeReferences.modifyOnly(md));
return maybeAutoFormat(methodDecl, md, ctx);
}

private String getMatcher(JavaType s) {
maybeAddImport(MOCKITO_MATCHER_IMPORT.replace(".*", ""), "*", false);
maybeAddImport(MOCKITO_MATCHER_IMPORT.replace(".*", ""), "*");
if (s instanceof JavaType.Primitive) {
switch (s.toString()) {
case "int":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,17 @@ private Statement extractExpressionArguments(J.Block body, Statement lambdaState
// TODO look into possibly employing `TypeUtils.toString()` here, possibly with some changes upstream allowing for non-fully-qualified names
variableTypeShort = buildParameterizedTypeName(paramType);
variableTypeFqn = paramType;
maybeAddImport(paramType.getFullyQualifiedName(), false);
maybeAddImport(paramType.getFullyQualifiedName());
for (JavaType typeParam : paramType.getTypeParameters()) {
if (typeParam instanceof JavaType.FullyQualified) {
maybeAddImport(((JavaType.FullyQualified) typeParam).getFullyQualifiedName(), false);
maybeAddImport(((JavaType.FullyQualified) typeParam).getFullyQualifiedName());
}
}
} else if (type instanceof JavaType.FullyQualified) {
JavaType.FullyQualified aClass = (JavaType.FullyQualified) type;
variableTypeShort = aClass.getClassName();
variableTypeFqn = aClass;
maybeAddImport(aClass.getFullyQualifiedName(), false);
maybeAddImport(aClass.getFullyQualifiedName());
}

Cursor blockCursor = new Cursor(getCursor(), body);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public J.Block visitBlock(J.Block block, ExecutionContext ctx) {
exceptionClass = "Exception.class";
}

maybeAddImport("org.junit.jupiter.api.Assertions", "assertThrows", false);
maybeAddImport("org.junit.jupiter.api.Assertions", "assertThrows");
Statement firstExpectedExceptionMethodInvocation = getCursor().getMessage(FIRST_EXPECTED_EXCEPTION_METHOD_INVOCATION);
String templateString = exceptionClass instanceof String ? "#{}assertThrows(#{}, () -> #{any()});" : "#{}assertThrows(#{any()}, () -> #{any()});";
b = JavaTemplate.builder(templateString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
.apply(updateCursor(m), m.getCoordinates().replaceBody(), cta.expectedException, lambda);
// Only remove the throws clause for the specific exception being tested
m = removeSpecificThrowsClause(m, cta.expectedException);
// Unconditionally add the import for assertThrows, got a report where the above template adds the method successfully
// but with missing type attribution for assertThrows so the import was missing
maybeAddImport("org.junit.jupiter.api.Assertions", "assertThrows", false);
maybeAddImport("org.junit.jupiter.api.Assertions", "assertThrows");
}
}
if (cta.timeout != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
J.MethodInvocation mi = super.visitMethodInvocation(method, ctx);
if (ANY_STRING.matches(mi)) {
maybeRemoveImport("org.mockito.Mockito.anyString");
maybeAddImport("org.mockito.ArgumentMatchers", "nullable", false);
maybeAddImport("org.mockito.ArgumentMatchers", "nullable");
return JavaTemplate.builder("nullable(String.class)")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "mockito-core-3.12"))
.staticImports("org.mockito.ArgumentMatchers.nullable")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,11 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
J.ClassDeclaration after = JavaTemplate
.builder(String.format("private%s MockedStatic<%s> %s;", staticSetup ? " static" : "", className, variableName))
.contextSensitive()
.imports("org.mockito.MockedStatic")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "mockito-core-5"))
.build()
.apply(updateCursor(classDecl), classDecl.getBody().getCoordinates().firstStatement());
maybeAddImport("org.mockito.MockedStatic");

List<Statement> afterStatements = after.getBody().getStatements();
AnnotationMatcher specificBeforeMatcher = new AnnotationMatcher(matchedAnnotation);
Expand Down Expand Up @@ -298,7 +301,7 @@ private JavaTemplate javaTemplateMockStatic(J.MethodInvocation stubbing, String
if (((J.MethodInvocation) requireNonNull(stubbing.getSelect())).getSelect() != null) {
maybeRemoveImport("org.mockito.Mockito");
}
maybeAddImport("org.mockito.MockedStatic", false);
maybeAddImport("org.mockito.MockedStatic");
maybeAddImport("org.mockito.Mockito", "mockStatic");
return JavaTemplate.builder(code)
.contextSensitive()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
}

maybeRemoveImport("org.testng.Assert");
maybeAddImport(ASSERTJ, "assertThat", false);
maybeAddImport(ASSERTJ, "assertThat");

List<Expression> args = method.getArguments();
Expression actual = args.get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
}

maybeRemoveImport("org.testng.Assert");
maybeAddImport("org.assertj.core.api.Assertions", "assertThat", false);
maybeAddImport("org.assertj.core.api.Assertions", "assertThat");

List<Expression> args = method.getArguments();
Expression actual = args.get(0);
Expand Down
Loading
Loading