6

I'm writing a simple reactive app, using spring5 and mongo reactive repositories. I wanted to test repos, followed tutorials, but still have problem:

java.lang.AssertionError: expectation "assertNext" failed (expected: onNext(); actual: onComplete())

Here is my entity:

@Document(collection = "products")
@TypeAlias("product")
@Getter
@Builder
@AllArgsConstructor
@EqualsAndHashCode
public class Product {

    @Id
    private ObjectId _id;
    private String productName;
    private Integer quantityPerUnit;
    private BigDecimal unitPrice;
    private Integer unitsInStock;
    private Boolean discount;
    private String categoryName;

    private Supplier supplier;

}

repo, nothing special:

public interface ProductRepository extends ReactiveMongoRepository<Product, String> {
}

and finally test:

@DataMongoTest
@RunWith(SpringRunner.class)
public class ProductRepositoryTest {

    @Autowired
    private ProductRepository productRepository;
    @Test
    public void shouldReturnOneProductWithExpected() {
        String expectedId = "54759ab3c090d83494e2d804";
        productRepository.save(first).block();
        Mono<Product> product = productRepository.findById(Mono.just("54759ab3c090d83494e2d804"));

        StepVerifier
                .create(product)
                .assertNext(prod -> {
                    assertNotNull(prod);
                    assertThat(prod.get_id(), is(equalTo(expectedId)));
                })
                .expectComplete()
                .verify();
    }


}

Does anyone have idea why it's not working?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
antonina18
  • 61
  • 1
  • 1
  • 2

3 Answers3

2

I had a similar problems, this one works for me:

    @Test
    fun `should go to end-point and retrieve id`() {
        val generateUniqueId: Mono<String> = idGenerationRepository.generateUniqueId()
        StepVerifier
            .create(generateUniqueId)
            .expectNextMatches { it.isNotEmpty() }
            .verifyComplete()
    }

Not sure, but seems like it is a bit confusing API about verifyX, assertX, expectX

2

I have not seen how you build 'first' but something like this should work:

    import static org.assertj.core.api.Assertions.assertThat;
    ...

    @Test
    public void shouldReturnOneProductWithExpected() {
    String expectedId = "54759ab3c090d83494e2d804";
    // productRepository.save(first).block(); -> no need to block
    Mono<Product> product = productRepository.save(first).map(p -> p.get_id()).flatMap(productRepository::findById);

    StepVerifier
            .create(product)
            .assertNext(prod -> {
                assertThat(prod).isNotNull();
                assertThat(prod.get_id()).isEqualTo(expectedId);
            })
            .verifyComplete();
}
emvidi
  • 1,210
  • 2
  • 14
  • 18
0

Just wait for 1 or 2 seconds after inserting on database, because these are asychrone tasks..

@DataMongoTest
@RunWith(SpringRunner.class)
public class ProductRepositoryTest {

    @Autowired
    private ProductRepository productRepository;
    @Test
    public void shouldReturnOneProductWithExpected() {
        String expectedId = "54759ab3c090d83494e2d804";
        productRepository.save(first).block();
        
        TimeUnit.SECONDS.sleep(2);

        Mono<Product> product = productRepository.findById(Mono.just("54759ab3c090d83494e2d804"));
        StepVerifier
                .create(product)
                .assertNext(prod -> {
                    assertNotNull(prod);
                    assertThat(prod.get_id(), is(equalTo(expectedId)));
                })
                .expectComplete()
                .verify();
    }


}
Abdelhafid
  • 799
  • 7
  • 13
  • 1
    Why not just chain the mono, like `productRepository.save(first).flatMap(res ->productRepository.findById(Mono.just("54759ab3c090d83494e2d804")));` – Xogaz Apr 21 '21 at 09:26