Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

271
Visualizações
How to test logging in junit5?

I am transitioning from junit4 to junit5 on a project and trying to figure out how to test logs. Previously, I used

@Rule
OutputCapture outputCapture = new OutputCapture();

and would then write an assertion using outputCapture.toString(), for example

assertThat(outputCapture.toString(),containsString("status=200"));

Since @Rule annotation hasn't been implemented yet in junit5, I can't use outputCapture. Any ideas what to do instead? Thanks!

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

There's an extension provided for the same and you can use it as below:

@ExtendWith(OutputCaptureExtension.class)
public class MyTestClass {

    @Test
    void myTestCase(CapturedOutput capturedOutput) {
        assertTrue(capturedOutput.getOut().contains("expected string"));
        assertTrue(capturedOutput.getErr().contains("expected string"));
    }
}
about 4 years ago · Santiago Trujillo Relatório

0

We stumbled upon the same issue during our JUnit5 migration. After some research, I found a technical solution but it didn't seem like someone made a testing library out of it just yet. So that's what I did. It's published to Maven Central so you can use it right away:

https://github.com/netmikey/logunit

You can use it as follows:

public class MyModuleTest {

    @RegisterExtension
    LogCapturer logs = LogCapturer.create().captureForType(MyModule.class);

    @Test
    public void testLogging() {
        // ... do the testing ...

        // Run assertions on the logged messages
        logs.assertContains("Some message");
    }
}

(see the project's README for more examples)

about 4 years ago · Santiago Trujillo Relatório

0

The Migration Tip from the JUnit5 documentation clearly states that -

@Rule and @ClassRule no longer exist; superseded by @ExtendWith; see the following section for partial rule support.

For the purpose of using the existing @Rule support from JUnit 4, there is though a way suggested for method or class level annotations.

As in JUnit 4, Rule-annotated fields as well as methods are supported. By using these class-level extensions on a test class such Rule implementations in legacy codebases can be left unchanged including the JUnit 4 rule import statements.

This limited form of Rule support can be switched on by the class-level annotation org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport


A better option would still be redesigning your test suite to use the Extension Model from JUnit5 if you're using it.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda