Search code examples
groovyassertj

AssertJ and Groovy not playing nicely together


I'm encountering a weird issue / bug with assert j and groovy with step verifier testing mongo db. I've included some code to help you reproduce this locally and also I have made the test work by replacing the closure with just a string.

I have the following code:

  @Document
    @ToString(includePackage = false, includeFields = true)
    class Image {
        @Id private String id
        private String name

        Image() {
        }

        Image(String id, String name) {
            this.id = id
            this.name = name
        }

        String getId() {
            return id
        }

        void setId(String id) {
            this.id = id
        }

        String getName() {
            return name
        }

        void setName(String name) {
            this.name = name
        }
}       

ImageRepository.groovy

interface ImageRepository extends ReactiveCrudRepository<Image, String>{
    Mono<Image> findByName(String name)
}

and the following test

@RunWith(SpringRunner)
@DataMongoTest
class EmbeddedImageRepositoryTests {

    @Autowired
    ImageRepository repository

    @Autowired
    MongoOperations operations

    @Before
    void setup() {
        operations.dropCollection(Image)
        operations.insert(new Image([
                id  : '1',
                name: 'learning-spring-boot-cover.jpg'
        ]))
        operations.insert(new Image([
                id  : '2',
                name: 'learning-spring-boot-2nd-edition.jpg'
        ]))
        operations.insert(new Image([
                id  : '3',
                name: 'bazinga.png'
        ]))
        operations.findAll(Image).each { println it }
    }

    @Test
    void findAllShouldWork() {
        Flux<Image> images = repository.findAll()
        StepVerifier.create(images)
                .recordWith({ new ArrayList<>() })
                .expectNextCount(3)
                .consumeRecordedWith(
                {
                    assertThat(it).hasSize(3)
                    assertThat(it)
                            .extracting({it.name})
                            .contains(
                            'learning-spring-boot-cover.jpg',
                            'learning-spring-boot-2nd-edition.jpg',
                            'bazinga.png')
                })
                .expectComplete()
                .verify()
    }
}

The test fails and the offending line is this after some

.extracting({it.name})

When it is changed to

.extracting('name')

Then the test passes. Has anybody had the same problem with using assert J and groovy and step verifier?


Solution

  • I haven't used AssertJ with groovy but my guess is Groovy is confused when resolving which extracting methods to use, if I had to pick the ones confusing Groovy I would pick:

    but you have a few more overloaded extracting in https://static.javadoc.io/org.assertj/assertj-core/3.12.2/org/assertj/core/api/AbstractIterableAssert.html#method.summary.