Skip to content

Failure generating Assertions.assertDoesNotThrow in AssertJ migration on Apache Shiro #732

@timtebeek

Description

@timtebeek

Recipe name: AssertJ best practices

Recipe id: org.openrewrite.java.testing.assertj.Assertj

Error:

try {
                   realm.checkRoles(principalCollection, new String[] {"role1", "role2"});
               } catch (AuthorizationException ae) {
                   fail("principal doesn't have all roles when it should");
              }

Message:

Expected a template that would generate exactly one statement to replace one statement, but generated 0. Template:
Assertions.assertDoesNotThrow(() -> __P__.<java.lang.Object>/*__p0__*/p(), __P__.<java.lang.String>/*__p1__*/p())
Substitutions:
Substitutions(code=Assertions.assertDoesNotThrow(() -> #{any()}, #{any(String)}), genericTypes=[], parameters=[J.Block(padding=org.openrewrite.java.tree.J$Block$Padding@6c0177a8, id=80000000-0003-a656-0000-000000000000, prefix=Space(comments=<0 comments>, whitespace=<empty>), markers=Markers(id=4970d59c-2ed5-4aa2-ac8d-788c17bb5fc5, markers=[]), statik=JRightPadded(element=false, after=Space(comments=<0 comments>, whitespace=<empty>)), statements=[realm.checkRoles(principalCollection, new String[] {"role1", "role2"})], end=Space(comments=<0 comments>, whitespace='
·₁·₂·₃·₄·₅·₆·₇·₈·₉·₊·₁·₂·₃·₄·₅·₆')), principal doesn't have all roles when it should], propertyPlaceholderHelper=org.openrewrite.internal.PropertyPlaceholderHelper@4843925, typeVariables=[])
Statement:
J.Try(padding=org.openrewrite.java.tree.J$Try$Padding@3945df30, id=80000000-0003-a655-0000-000000000000, prefix=Space(comments=<0 comments>, whitespace='
·₁·₂·₃·₄·₅·₆·₇·₈·₉·₊·₁·₂·₃·₄·₅·₆'), markers=Markers(id=4970d59c-2ed5-4aa2-ac8d-788c17bb5fc5, markers=[]), resources=null, body=J.Block(padding=org.openrewrite.java.tree.J$Block$Padding@5dcca162, id=80000000-0003-a656-0000-000000000000, prefix=Space(comments=<0 comments>, whitespace='·₁'), markers=Markers(id=4970d59c-2ed5-4aa2-ac8d-788c17bb5fc5, markers=[]), statik=JRightPadded(element=false, after=Space(comments=<0 comments>, whitespace=<empty>)), statements=[realm.checkRoles(principalCollection, new String[] {"role1", "role2"})], end=Space(comments=<0 comments>, whitespace='
·₁·₂·₃·₄·₅·₆·₇·₈·₉·₊·₁·₂·₃·₄·₅·₆')), catches=[catch (AuthorizationException ae) {
                  fail("principal doesn't have all roles when it should");
              }], finallie=null)

Detail:

java.lang.IllegalArgumentException: Expected a template that would generate exactly one statement to replace one statement, but generated 0. Template:
Assertions.assertDoesNotThrow(() -> __P__.<java.lang.Object>/*__p0__*/p(), __P__.<java.lang.String>/*__p1__*/p())
Substitutions:
Substitutions(code=Assertions.assertDoesNotThrow(() -> #{any()}, #{any(String)}), genericTypes=[], parameters=[J.Block(padding=org.openrewrite.java.tree.J$Block$Padding@6c0177a8, id=80000000-0003-a656-0000-000000000000, prefix=Space(comments=<0 comments>, whitespace=<empty>), markers=Markers(id=4970d59c-2ed5-4aa2-ac8d-788c17bb5fc5, markers=[]), statik=JRightPadded(element=false, after=Space(comments=<0 comments>, whitespace=<empty>)), statements=[realm.checkRoles(principalCollection, new String[] {"role1", "role2"})], end=Space(comments=<0 comments>, whitespace='
·₁·₂·₃·₄·₅·₆·₇·₈·₉·₊·₁·₂·₃·₄·₅·₆')), principal doesn't have all roles when it should], propertyPlaceholderHelper=org.openrewrite.internal.PropertyPlaceholderHelper@4843925, typeVariables=[])
Statement:
J.Try(padding=org.openrewrite.java.tree.J$Try$Padding@3945df30, id=80000000-0003-a655-0000-000000000000, prefix=Space(comments=<0 comments>, whitespace='
·₁·₂·₃·₄·₅·₆·₇·₈·₉·₊·₁·₂·₃·₄·₅·₆'), markers=Markers(id=4970d59c-2ed5-4aa2-ac8d-788c17bb5fc5, markers=[]), resources=null, body=J.Block(padding=org.openrewrite.java.tree.J$Block$Padding@5dcca162, id=80000000-0003-a656-0000-000000000000, prefix=Space(comments=<0 comments>, whitespace='·₁'), markers=Markers(id=4970d59c-2ed5-4aa2-ac8d-788c17bb5fc5, markers=[]), statik=JRightPadded(element=false, after=Space(comments=<0 comments>, whitespace=<empty>)), statements=[realm.checkRoles(principalCollection, new String[] {"role1", "role2"})], end=Space(comments=<0 comments>, whitespace='
·₁·₂·₃·₄·₅·₆·₇·₈·₉·₊·₁·₂·₃·₄·₅·₆')), catches=[catch (AuthorizationException ae) {
                  fail("principal doesn't have all roles when it should");
              }], finallie=null)
org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.maybeReplaceStatement(JavaTemplateJavaExtension.java:473)
org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitStatement(JavaTemplateJavaExtension.java:452)
org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitStatement(JavaTemplateJavaExtension.java:55)
org.openrewrite.java.JavaVisitor.visitTry(JavaVisitor.java:1190)
org.openrewrite.java.tree.J$Try.acceptJava(J.java:5646)
org.openrewrite.java.tree.J.accept(J.java:63)
org.openrewrite.TreeVisitor.visit(TreeVisitor.java:245)
org.openrewrite.TreeVisitor.visit(TreeVisitor.java:154)
org.openrewrite.java.JavaTemplate.apply(JavaTemplate.java:120)
org.openrewrite.java.testing.junit5.RemoveTryCatchFailBlocks$RemoveTryCatchBlocksFromUnitsTestsVisitor.replaceWithAssertDoesNotThrowWithStringExpression(RemoveTryCatchFailBlocks.java:161)
org.openrewrite.java.testing.junit5.RemoveTryCatchFailBlocks$RemoveTryCatchBlocksFromUnitsTestsVisitor.visitTry(RemoveTryCatchFailBlocks.java:108)
org.openrewrite.java.testing.junit5.RemoveTryCatchFailBlocks$RemoveTryCatchBlocksFromUnitsTestsVisitor.visitTry(RemoveTryCatchFailBlocks.java:59)
org.openrewrite.java.tree.J$Try.acceptJava(J.java:5646)
org.openrewrite.java.tree.J.accept(J.java:63)
org.openrewrite.TreeVisitor.visit(TreeVisitor.java:245)
org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:311)
...

Diff

       * Tests that roles can't be checked while the realm is being loaded.
 */
@Test
-    void testCheckRole() throws InterruptedException {
+    void checkRole() throws InterruptedException {
    setUpForReadConfigurationTest();
    executeTest(new Runnable() {
        public void run() {
            PrincipalCollection principalCollection = new SimplePrincipalCollection("user1", "realm1");
-                try {
+                {{80000000-0000-022d-0000-000000000000}}try {
                realm.checkRoles(principalCollection, new String[] {"role1", "role2"});
            } catch (AuthorizationException ae) {
                fail("principal doesn't have all roles when it should");
-                }
+                }{{80000000-0000-022d-0000-000000000000}}
        }
    });
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions