Skip to content

JUnitAssertThrowsToAssertExceptionType does not convert cases where executable is a variable #511

Description

@timtebeek

What version of OpenRewrite are you using?

What is the smallest, simplest way to reproduce the problem?

import org.junit.jupiter.api.function.Executable;

import static org.junit.jupiter.api.Assertions.assertThrows;

public class SimpleExpectedExceptionTest {
    public void throwsExceptionWithSpecificType() {
        Executable executable = () -> {
            throw new NullPointerException();
        };
        assertThrows(NullPointerException.class, executable);
    }
}

What did you expect to see?

import org.assertj.core.api.ThrowableAssert.ThrowingCallable;

import static org.junit.jupiter.api.Assertions.assertThrows;

public class SimpleExpectedExceptionTest {
    public void throwsExceptionWithSpecificType() {
        ThrowingCallable executable = () -> {
            throw new NullPointerException();
        };
        assertThatExceptionOfType(NullPointerException.class).isThrownBy(executable);
    }
}

What did you see instead?

No change, as the recipe fails to match the case where the argument is a variable.

J executable = mi.getArguments().get(1);
if (executable instanceof J.Lambda) {
executable = ((J.Lambda) executable).withType(THROWING_CALLABLE_TYPE);
} else if (executable instanceof J.MemberReference) {
executable = ((J.MemberReference) executable).withType(THROWING_CALLABLE_TYPE);
} else {
executable = null;

Additional context

As reported on

Metadata

Metadata

Assignees

No one assigned

    Labels

    assertjbugSomething isn't working

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    Status
    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions