-
Notifications
You must be signed in to change notification settings - Fork 10
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
TestLoggerFactoryExtension
may not work with every test setup
#183
Comments
That's weird, the actual number of changes in the "production" code between @jamietanna Do you have any sample project to share in order to reproduce the issue? |
Oh sorry I meant the old project as in pre fork |
Minimal example: package com.github.valfirst.slf4jtest;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static com.github.valfirst.slf4jtest.Assertions.assertThat;
@ExtendWith(TestLoggerFactoryExtension.class)
class NestedExample {
private static final TestLogger TEST_LOGGER = TestLoggerFactory.getTestLogger(ClassUnderTest.class);
@AfterEach
void tearDown() {
TestLoggerFactory.clear();
}
@Nested
class Something {
@BeforeEach
void setup() {
ClassUnderTest.doTheThing();
}
@Test
void info() {
assertThat(TEST_LOGGER).hasLogged(LoggingEvent.info("The message"));
}
@Test
void error() {
assertThat(TEST_LOGGER).hasLogged(LoggingEvent.error("Oh no!"));
}
}
public static class ClassUnderTest {
private static final Logger LOGGER = LoggerFactory.getLogger(ClassUnderTest.class);
public static void doTheThing() {
LOGGER.info("The message");
LOGGER.error("Oh no!");
}
}
} |
The TestLoggerFactoryExtension is a BeforeTestExecutionCallback, which is executed after the Changing it into a BeforeEachCallback would solve this issue, since such callbacks are called before the |
I was thinking about adding flag to the annotation, which will specify lifecycle step at which clean up should happen |
Could be an idea. If you want it to behave exactly like the JUnit 4 rule, it should implement both |
I've got an issue with a codebase I work on where the registration of the
TestLoggerFactoryExtension
leads to failing tests.The scenario in question incluides a number of
@Nested
tests, with structure like so:I'm currently trying different options for the callback class (after/before) but it doesn't seem to be working, so I'm instead needing to exclude this extension from being auto-registered.
Edit: This could just be a quirk of how we're doing things, but this has definitely worked with the code we've got above, with the old library 🤔
The text was updated successfully, but these errors were encountered: