Java · Essencial · Inglês · Técnico
JUnit
Framework padrão de testes automatizados em Java. A versão 5, o JUnit Jupiter, usa anotações como @Test e @BeforeEach e asserções da classe Assertions.
Onde usar
Aparece em todo projeto: mvn test roda os testes. Em review: 'cadê o teste desse caso?'.
Em código
import org.junit.jupiter.api.*;
class SomaTest {
@Test
void soma() {
Assertions.assertEquals(4, 2 + 2);
}
}Exemplos
Write a JUnit test for the edge case before fixing the bug.
Escreva um teste JUnit para o caso de borda antes de corrigir o bug.
The test never ran because it imported the JUnit 4 annotation.
O teste nunca rodou porque importou a anotação do JUnit 4.
Cuidado
Cuidado ao misturar JUnit 4 e 5: o @Test de pacotes diferentes (org.junit e org.junit.jupiter.api) faz o teste não rodar.
Origem
De Java e unit, teste de unidade.