Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test ran before ServerStartupEvent #313

Open
hantsy opened this issue Jun 9, 2023 · 0 comments
Open

Test ran before ServerStartupEvent #313

hantsy opened this issue Jun 9, 2023 · 0 comments

Comments

@hantsy
Copy link

hantsy commented Jun 9, 2023

Expected Behavior

I tried to convert one of my micronaut project to use test-resource instead of the original test containers.

There is a simple integration test like this.

//  transactional = true will throw:
//  Transaction mode is not supported when the synchronous transaction manager is created using Reactive transaction manager!
@MicronautTest(application = Application.class, transactional = false)
@Slf4j
class ApplicationTest {

    @Inject
    EmbeddedApplication<?> application;

    @Inject
    @Client("/customers")
    ReactorHttpClient client;

    @Test
    void testItWorks() {
        Assertions.assertTrue(application.isRunning());
    }

    @Test
    public void getAllCustomers() {
        log.debug("get all customers...");
        var request = HttpRequest.GET("");
        client.exchange(request, Argument.listOf(Customer.class))
                .log()
                .as(StepVerifier::create)
                .consumeNextWith(res -> {
                    assertEquals(HttpStatus.OK, res.getStatus());
                    assertThat(res.body()).anyMatch(customer -> customer.name().equals("Jack"));
                })
                .verifyComplete();
    }

}

I use singleton bean to initialize some sample data,

@Singleton
@Requires(notEnv = "mock")
@RequiredArgsConstructor
@Slf4j
public class SampleDataInitializer {
    private final CustomerRepository customers;

    @EventListener()
    public void init(ServerStartupEvent event) {
        customers.deleteAll().then()
                .thenMany(customers.saveAll(
                                List.of(
                                        Customer.of("Jack", 20, Address.of("test", "NY", "210000")),
                                        Customer.of("Rose", 19, Address.of("test", "Boston", "210000"))
                                )
                        )
                )
                .subscribe(
                        data -> log.debug("saved data: {}", data),
                        error -> log.error("error: {}", error),
                        () -> log.debug("done initialization")
                );
    }
}

What I expected is initializing the sample and verify data in the it tests.

Actual Behaviour

Obviously, the tests ran before the ServerStartupEvent.

22:24:09.738 [Test worker] DEBUG com.example.ApplicationTest - get all customers...
22:24:09.806 [Test worker] INFO  reactor.Mono.Next.1 - onSubscribe(MonoNext.NextSubscriber)
22:24:09.811 [Test worker] INFO  reactor.Mono.Next.1 - request(unbounded)
22:24:10.060 [reactor-tcp-nio-1] DEBUG i.m.d.r.o.DefaultR2dbcRepositoryOperations - Transaction: DEFAULT begin for dataSource: default
22:24:10.074 [reactor-tcp-nio-1] DEBUG io.micronaut.data.query - Executing Query: DELETE  FROM "customers" 
22:24:10.085 [reactor-tcp-nio-1] DEBUG i.m.d.r.o.DefaultR2dbcRepositoryOperations - Committing transaction: DEFAULT for dataSource default
22:24:10.095 [reactor-tcp-nio-1] DEBUG i.m.d.r.o.DefaultR2dbcRepositoryOperations - Closing Connection for DataSource: default
...
Expecting any elements of:
  []
to match given predicate but none did.

Steps To Reproduce

No response

Environment Information

Windows 10 pro,
Java 17

Example Application

https://github.com/hantsy/micronaut-sandbox/tree/master/data-r2dbc

Version

4.0.0-M3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant