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

343
Visualizações
JUnit @Before vs @Rule

I understand that,

  • @Before and @BeforeClass run before each test, or the entire test class, respectively
  • @Rule and @ClassRule wraps each test, or the entire test class, respectively.

Let's say I need to initialize some data before each test method,

How do I decide between using @Before and @Rule? Under what conditions is one preferred over another? The same question also goes for @BeforeClass vs.@ClassRule.

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

0

In order to use @Rule, you require a class that implements TestRule(preferred) or MethodRule, as can be read here. Whereas @Before and @After require a new method to be written in every test case, @Rule does not because it is only an instantiation of already existing code.

So, if you would use @Before and @After for setUp() and tearDown() that you'll be using in many test cases, it is actually a better idea to use @Rule because of code reuse. If you have a test case that requires a unique @Before and/or @After, then these annotations are preferable.

For a bit more elaborate answer with a couple examples, take a look here. Ajit explains it very well.

about 4 years ago · Santiago Trujillo Relatório

0

Indeed, as @Quwin suggested, accoridng to JUnit 4.12 API doc,

TestRule can do everything that could be done previously with methods annotated with @Before, @After, @BeforeClass, or @AfterClass, but TestRules are (1) more powerful, and (2) more easily shared between projects and classes.


Ways that TestRules are more powerful:

There are known implementing classes of the TestRule, which are some usefuls rules you can use out-of-the-box,

For examples of how this can be useful, see these provided TestRules, or write your own:

  • ErrorCollector: collect multiple errors in one test method
  • ExpectedException: make flexible assertions about thrown exceptions
  • ExternalResource: start and stop a server, for example
  • TemporaryFolder: create fresh files, and delete after test
  • TestName: remember the test name for use during the method
  • TestWatcher: add logic at events during method execution
  • Timeout: cause test to fail after a set time
  • Verifier: fail test if object state ends up incorrect

Another benefit of rules, is that multiple rules can be used in a single test case. You may want to use RuleChain to specify the order in which the rules should be run.

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