Skip to content

Added implementation to extract any expression arguments from last statement in assertThrows #724

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

JohannisK
Copy link
Contributor

What's changed?

This PR changes the recipe to extract all expression arguments into variables outside the assertThrows() call.

What's your motivation?

assertThrows(..) should only contain the expression that is expected to throw.

@Test
void test() {
    assertThrows(Exception.class, () -> {
        doA();
        doB();
        testThing(getC());
    });
}

breaks that convention because getC() could throw.

@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite May 9, 2025
github-actions[bot]

This comment was marked as resolved.

JohannisK and others added 3 commits May 9, 2025 12:20
…OnLastStatement.java

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…OnLastStatement.java

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…OnLastStatementTest.java

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@timtebeek timtebeek added the enhancement New feature or request label May 9, 2025
github-actions[bot]

This comment was marked as spam.

github-actions[bot]

This comment was marked as spam.

github-actions[bot]

This comment was marked as spam.

@timtebeek timtebeek self-requested a review May 9, 2025 16:44
Comment on lines +146 to +202
private Statement extractExpressionArguments(Statement methodStatement, Statement lambdaStatement, List<Statement> statements) {
if (lambdaStatement instanceof J.MethodInvocation) {
J.MethodInvocation mi = (J.MethodInvocation) lambdaStatement;
List<Expression> lambdaArguments = new ArrayList<>(mi.getArguments().size());
for (Expression e : mi.getArguments()) {
if (e instanceof J.Identifier || e instanceof J.Literal || e instanceof J.Empty) {
lambdaArguments.add(e);
continue;
}

JavaTemplate.Builder builder = JavaTemplate.builder("#{} " + getVariableName(e) + " = #{any()};\n")
.javaParser(JavaParser.fromJavaVersion().classpath(JavaParser.runtimeClasspath()).logCompilationWarningsAndErrors(true));

Object type = "Object";
if (e.getType() instanceof JavaType.Primitive) {
type = e.getType().toString();
} else if (e.getType() != null && TypeUtils.asClass(e.getType()) != null) {
type = TypeUtils.asClass(e.getType()).getClassName();
maybeAddImport(TypeUtils.asFullyQualified(e.getType()).getFullyQualifiedName(), false);
}

J.VariableDeclarations varDecl = new J.VariableDeclarations(Tree.randomId(),
Space.EMPTY,
Markers.EMPTY,
Collections.emptyList(),
Collections.emptyList(),
new J.Identifier(Tree.randomId(),
Space.EMPTY,
Markers.EMPTY,
Collections.emptyList(),
type.toString(),
e.getType(),
null),
null,
Collections.emptyList(),
Arrays.asList(JRightPadded.build(new J.VariableDeclarations.NamedVariable(Tree.randomId(),
Space.SINGLE_SPACE,
Markers.EMPTY,
new J.Identifier(Tree.randomId(),
Space.EMPTY,
Markers.EMPTY,
Collections.emptyList(),
getVariableName(e),
e.getType(),
new JavaType.Variable(null, 0, getVariableName(e), null, e.getType(), null)),
Collections.emptyList(),
JLeftPadded.build((Expression)e.withPrefix(Space.SINGLE_SPACE)).withBefore(Space.SINGLE_SPACE),
new JavaType.Variable(null, 0, getVariableName(e), null, e.getType(), null)))));

J.Identifier name = varDecl.getVariables().get(0).getName();
statements.add(varDecl.withPrefix(methodStatement.getPrefix().withComments(emptyList())));
lambdaArguments.add(name);
}
lambdaStatement = mi.withArguments(lambdaArguments);
}
return lambdaStatement;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as discussed on Slack this was based in part on an example that could be simplified with an anonymous visitor; putting that reminder here to use when reviewing & polishing, as I don't expect you to know what adjustments to make based on that comment alone.

github-actions[bot]

This comment was marked as spam.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request junit
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

4 participants