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

@BeforeAll and @BeforeEach behavior in Micronaut tests #753

Open
moliveiraOptiply opened this issue Apr 26, 2023 · 0 comments
Open

@BeforeAll and @BeforeEach behavior in Micronaut tests #753

moliveiraOptiply opened this issue Apr 26, 2023 · 0 comments

Comments

@moliveiraOptiply
Copy link

moliveiraOptiply commented Apr 26, 2023

Issue description

In micronaut tests, annotations @BeforeAll and @BeforeEach exhibit counterintuitive behavior.

For @BeforeAll, in a case such as the one below, the method beforeAll in the nested test class is executed before dependency injection is apparently done, which leads to null pointer exception errors because dslContext is always null. Is this expected behavior?

@MicronautTest(transactional = false)
    class FooTests {

        @Inject
        private FlywayMigration flywayMigration;

        @Inject
        DSLContext dslContext;

        @Inject
        DataSource dataSource;

        @Nested
        @TestInstance(TestInstance.Lifecycle.PER_CLASS)
        class ShouldRunFooTests {

            TestObject testObject;
            String myTestString;

            @BeforeAll
            void beforeAll() {
                Configuration jooqConfiguration = dslContext.configuration();
                jooqConfiguration.settings().setRenderSchema(false);
                jooqConfiguration.settings().setRenderQuotedNames(RenderQuotedNames.NEVER);
                jooqConfiguration.settings().setExecuteWithOptimisticLocking(false);

                flywayMigration.migrate(dataSource);

                if(Objects.isNull(testObject)) {
                    testObject = new TestObject();
                }
            }

            @BeforeEach
            void beforeEach() {
                myTestString = "a test String";
            }

            private Stream<Arguments> getArguments() {
                return Stream.of("foo", myTestString).map(Arguments::of);
            }

            @ParameterizedTest
            @MethodSource("getArguments")
            void shouldProcessParams(String testString) {
                assertTrue(testString.equals(myTestString));
            }
        }
    }

Similarly, in the example below, when using @BeforeEach to initialize parameterized tests using @MethodSource, it seems that the code in the method beforeEach isn't executed before the method getArguments initialized. This leads to a null pointer exception when calling testObject.toString() because testObject is always null. Is this also intended behavior?

    @MicronautTest(transactional = false)
    class FooTests {

        @Inject
        private FlywayMigration flywayMigration;

        @Inject
        DSLContext dslContext;

        @Inject
        DataSource dataSource;

        @Nested
        @TestInstance(TestInstance.Lifecycle.PER_CLASS)
        class ShouldRunFooTests {

            TestObject testObject;
            String myTestString;

            @BeforeEach
            void beforeEach() {
                Configuration jooqConfiguration = dslContext.configuration();
                jooqConfiguration.settings().setRenderSchema(false);
                jooqConfiguration.settings().setRenderQuotedNames(RenderQuotedNames.NEVER);
                jooqConfiguration.settings().setExecuteWithOptimisticLocking(false);

                flywayMigration.migrate(dataSource);

                if(Objects.isNull(testObject)) {
                    testObject = new TestObject();
                }
                
                myTestString = "a test String";
            }

            private Stream<Arguments> getArguments() {
                return Stream.of("foo", testObject.toString());
            }

            @ParameterizedTest
            @MethodSource("getArguments")
            void shouldProcessParams(String testString) {
                assertTrue(testString.equals(myTestString));
            }
        }
    }

If we run similar tests in a spring boot application, no null pointer exception errors are thrown.

For context, I am using:

  • micronaut 3.3.0
  • junit-jupiter 5.9.2
  • micronaut-test-junit5: 3.6.2
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