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
Logcaptor error logs do not contain expected content when reusing instance #24
Comments
Hi @Emprah Thank for reporting this issue and sharing the detailed code snippets! Could you retry with the latest version and tell me if the issue is still present? The latest version is |
I tried again with 2.7.2, but no difference unfortunately... |
I have tried it locally and indeed I am getting the same behaviour which you mentioned. Reusing log captor works and I use it at some other places such as here: unit test example So I was amazed to see this issue. I tried couple of different scenario's with the code snippet you have shared and I noticed that when I don't use SpringBootIntegrationTest annotation and try For now I don't have a fix as I am not able to understand why the spring boot annotation messes logcaptor while using it as a static field. So I would suggest to use the following snippet for now till I have more info regarding this issue. private LogCaptor logcaptor;
@BeforeEach
void setupLogCaptor() {
logcaptor = LogCaptor.forClass(FooService.class);
}
@AfterEach
void closeLogCaptor() {
logcaptor.close();
} You could also use the following setup for the time being: @SpringBootIntegrationTest
class TestClass {
private LogCaptor logcaptor;
private Sut sut;
@Autowired
TestClass(Sut sut) {
this.sut = sut;
this.logcaptor = LogCaptor.forClass(Sut.class);
}
@BeforeEach
void beforeEach() {
logcaptor.clearLogs();
}
private static Stream<List<String>> provideParameters() {
return Stream.of(
null,
List.of()
);
}
@ParameterizedTest
@MethodSource("provideParameters")
void testMethod(List<String> parameters) {
CompletableFuture<void> future = sut.doStuff(parameters);
assertThrows(Exception.class, () -> future.get(10, TimeUnit.SECONDS));
assertThat(logcaptor.getErrorLogs()).containsExactly("Expected error message");
}
} or the following snippet: @SpringBootIntegrationTest
class TestClass {
private LogCaptor logcaptor;
@Autowired
private Sut sut;
@PostConstruct
void init() {
logcaptor = LogCaptor.forClass(Sut.class);
}
@BeforeEach
void beforeEach() {
logcaptor.clearLogs();
}
private static Stream<List<String>> provideParameters() {
return Stream.of(
null,
List.of()
);
}
@ParameterizedTest
@MethodSource("provideParameters")
void testMethod(List<String> parameters) {
CompletableFuture<void> future = sut.doStuff(parameters);
assertThrows(Exception.class, () -> future.get(10, TimeUnit.SECONDS));
assertThat(logcaptor.getErrorLogs()).containsExactly("Expected error message");
}
} |
I got a reply on this issue from someone on stackoverflow and he mentioned the following:
source: https://stackoverflow.com/a/70310499/6777695 This issue is exactly the same as this one: spring-projects/spring-boot#19323 (comment) So I am not able to fix this on my side as it is a spring-boot related issue. So my advice would be to initialise LogCaptor after spring-boot has initialised the application context. This can be done by adding LogCaptor either in the So I am closing this issue as it is spring-boot related. Thank you for sharing this issue as it gave me more insight of how spring-boot is working and how LogCaptor is behaving in different kind of setups. I appreciate your investigation for the different scenario's 😊 |
Sorry for the late answer, i was quite busy - but just wanted to return the thank you for the follow up :) |
@Hakky54 maybe you could introduce something like
for such cases? |
Hi @hoangbnc Thank you for the suggestion, however this won't solve the root issue which is causing this different behavior. Adding a reset function and calling that in the before each is not different than just initializing it at that level. I think the ideal solution for this kind of use case or rather generic use case when using spring with junit 4 or 5 would be creating a junit-extension for logcaptor which would take away all of the verbosity for creating, resetting and disposal of logcaptor in the different test phases such as before all, before each, after each and after all. |
Describe the bug
I use logcaptor in a parameterized test method of a @SpringBootIntegrationTest class to check whether the expected error message was actually logged. I loosely followed the reuse example from the readme (see code snippets below).
For every parameterization, one error message is logged in the sut (verified that via debugger). When looking at the logcaptor.getErrorLogs though, this is not always correctly reflected there.
I tried out some things and it appears as if the following things make a difference:
The possible results for the assertion whether the specific error message has been logged are:
To Reproduce
Expected behavior
I would have expected logCaptor.getErrorLogs to contain the expected error message no matter how the test method is run and no matter how the lagCaptor is defined and (re)used.
Environmental Data:
The text was updated successfully, but these errors were encountered: