Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-11258

AssertJ collection mapping is badly supported

    XMLWordPrintableJSON

Details

    Description

      The following code will fail with the message

      [Static type checking] - No such property: x for class: java.lang.Object
       @ line 8, column 13.
               .map{ it.x }
                     ^
      
      @groovy.transform.CompileStatic
      class AssertJCollectionMappingTest {
      
        @org.junit.jupiter.api.Test
        void mapping() {
          def texts = somePojos()
          org.assertj.core.api.Assertions.assertThat(texts)
            .map{ it.x }
            .containsExactlyInAnyOrderElementsOf(['a', 'b'])
        }
      
        private static Collection<Pojo> somePojos() {
          return [new Pojo(x: 'a'), new Pojo(x: 'b')]
        }
      
        static class Pojo {
          String x
        }
      }
      

      Changing the line to

      .map({ it.x } as Function<Pojo, String>) 
      

      makes it compile but it is clumsy for more complicated types.

      It would be nice if Groovy was able to resolve the types without hints.

      Attachments

        1. screenshot-1.png
          34 kB
          Eric Milles

        Issue Links

          Activity

            People

              emilles Eric Milles
              jhunovis Jan Hackel
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: