Class AssertTrueToAssertThat

  • All Implemented Interfaces:
    org.openrewrite.java.JavaSourceVisitor<org.openrewrite.java.tree.J>, org.openrewrite.RefactorVisitor<org.openrewrite.java.tree.J>, org.openrewrite.SourceVisitor<org.openrewrite.java.tree.J>

    public class AssertTrueToAssertThat
    extends org.openrewrite.java.JavaIsoRefactorVisitor
    This is a refactoring visitor that will convert JUnit-style assertTrue() to assertJ's assertThat().isTrue(). This visitor only supports the migration of the following JUnit 5 assertTrue() methods:
         assertTrue(boolean condition) -> assertThat(condition).isTrue()
         assertTrue(boolean condition, String message) -> assertThat(condition).withFailMessage(message).isTrue();
         assertTrue(boolean condition, Supplier messageSupplier) -> assertThat(condition).withFailMessage(messageSupplier).isTrue();
     
    Note: There are three additional method signatures in JUnit that use a BooleanSupplier for the condition. Attempts to map these signatures into assertJ's model obfuscates the original assertion. It would be possible to use a shim method to support these method signatures, however, those shims would need to exist on each compilation unit or in a shared testing utilities library.
    • Field Summary

      • Fields inherited from class org.openrewrite.java.JavaRefactorVisitor

        formatter, treeBuilder
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void maybeAddStaticImport​(java.lang.String fullyQualifiedName, java.lang.String method)
      This method will add a static import method to the compilation unit if there is a reference to the method in the compilation unit.
      org.openrewrite.java.tree.J.CompilationUnit visitCompilationUnit​(org.openrewrite.java.tree.J.CompilationUnit cu)  
      org.openrewrite.java.tree.J.MethodInvocation visitMethodInvocation​(org.openrewrite.java.tree.J.MethodInvocation method)  
      • Methods inherited from class org.openrewrite.java.JavaIsoRefactorVisitor

        visitAnnotatedType, visitAnnotation, visitArrayAccess, visitArrayType, visitAssert, visitAssign, visitAssignOp, visitBinary, visitBlock, visitBreak, visitCase, visitCatch, visitClassDecl, visitContinue, visitDoWhileLoop, visitElse, visitEmpty, visitEnumValue, visitEnumValueSet, visitFieldAccess, visitFinally, visitForEachLoop, visitForLoop, visitIdentifier, visitIf, visitImport, visitInstanceOf, visitLabel, visitLambda, visitLiteral, visitMemberReference, visitMethod, visitMultiCatch, visitMultiVariable, visitNewArray, visitNewClass, visitPackage, visitParameterizedType, visitParentheses, visitPrimitive, visitReturn, visitStatement, visitSwitch, visitSynchronized, visitTernary, visitThrow, visitTry, visitTypeCast, visitTypeName, visitTypeParameter, visitTypeParameters, visitUnary, visitUnparsedSource, visitVariable, visitWhileLoop, visitWildcard
      • Methods inherited from class org.openrewrite.java.JavaRefactorVisitor

        addImport, addImport, maybeAddImport, maybeAddImport, maybeAddImport, maybeRemoveImport, maybeRemoveImport, maybeUnwrapParentheses
      • Methods inherited from class org.openrewrite.AbstractRefactorVisitor

        andThen, andThen, next, refactor, refactor, refactor
      • Methods inherited from class org.openrewrite.AbstractSourceVisitor

        getCursor, setCursoringOn, visit, visitAfter, visitAfter
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface org.openrewrite.java.JavaSourceVisitor

        enclosingBlock, enclosingClass, enclosingCompilationUnit, enclosingMethod, isInSameNameScope, isInSameNameScope, visitExpression
      • Methods inherited from interface org.openrewrite.RefactorVisitor

        defaultTo, generate, isIdempotent, nextCycle
      • Methods inherited from interface org.openrewrite.SourceVisitor

        getCursor, getName, getTags, reduce, validate, visit, visit, visitTree
    • Constructor Detail

      • AssertTrueToAssertThat

        public AssertTrueToAssertThat()
    • Method Detail

      • visitCompilationUnit

        public org.openrewrite.java.tree.J.CompilationUnit visitCompilationUnit​(org.openrewrite.java.tree.J.CompilationUnit cu)
        Specified by:
        visitCompilationUnit in interface org.openrewrite.java.JavaSourceVisitor<org.openrewrite.java.tree.J>
        Overrides:
        visitCompilationUnit in class org.openrewrite.java.JavaIsoRefactorVisitor
      • visitMethodInvocation

        public org.openrewrite.java.tree.J.MethodInvocation visitMethodInvocation​(org.openrewrite.java.tree.J.MethodInvocation method)
        Specified by:
        visitMethodInvocation in interface org.openrewrite.java.JavaSourceVisitor<org.openrewrite.java.tree.J>
        Overrides:
        visitMethodInvocation in class org.openrewrite.java.JavaIsoRefactorVisitor
      • maybeAddStaticImport

        public void maybeAddStaticImport​(java.lang.String fullyQualifiedName,
                                         java.lang.String method)
        This method will add a static import method to the compilation unit if there is a reference to the method in the compilation unit.
        Parameters:
        fullyQualifiedName - Fully-qualified name of the class.
        method - The static method to be imported.